|
| 1 | +import fs from "fs"; |
| 2 | +import matter from "gray-matter"; |
| 3 | +import { exportFundings, loadAllFundings, Funding } from "./fundingExport"; |
| 4 | + |
| 5 | +jest.mock("fs"); |
| 6 | +jest.mock("gray-matter"); |
| 7 | + |
| 8 | +const mockFs = fs as jest.Mocked<typeof fs>; |
| 9 | +const mockMatter = matter as jest.MockedFunction<typeof matter>; |
| 10 | + |
| 11 | +const generateMockFundingExportData = (overrides?: Partial<Omit<Funding, "contentMd">>): Omit<Funding, "contentMd"> => ({ |
| 12 | + id: "test-id", |
| 13 | + name: "Test Funding", |
| 14 | + filename: "funding1", |
| 15 | + urlSlug: "test-funding", |
| 16 | + callToActionLink: "https://example.com", |
| 17 | + callToActionText: "Apply Now", |
| 18 | + fundingType: "Grant", |
| 19 | + programPurpose: "Business Support", |
| 20 | + agency: ["Test Agency"], |
| 21 | + agencyContact: "contact@test.com", |
| 22 | + publishStageArchive: "", |
| 23 | + openDate: "2024-01-01", |
| 24 | + dueDate: "2024-12-31", |
| 25 | + status: "Open", |
| 26 | + programFrequency: "Annual", |
| 27 | + businessStage: "Early Stage", |
| 28 | + employeesRequired: "0", |
| 29 | + homeBased: "Yes", |
| 30 | + certifications: [], |
| 31 | + preferenceForOpportunityZone: "", |
| 32 | + county: "", |
| 33 | + sector: [], |
| 34 | + ...overrides, |
| 35 | +}); |
| 36 | + |
| 37 | +describe("fundingExport", () => { |
| 38 | + beforeEach(() => { |
| 39 | + jest.clearAllMocks(); |
| 40 | + }); |
| 41 | + |
| 42 | + describe("loadAllFundings", () => { |
| 43 | + it("loads all funding files from the directory", () => { |
| 44 | + const mockFileNames = ["funding1.md", "funding2.md"]; |
| 45 | + const mockFundingData = generateMockFundingExportData(); |
| 46 | + |
| 47 | + (mockFs.readdirSync as jest.Mock).mockReturnValue(mockFileNames); |
| 48 | + mockFs.readFileSync.mockReturnValue("mock file content"); |
| 49 | + mockMatter.mockReturnValue({ |
| 50 | + data: mockFundingData, |
| 51 | + content: "Test content", |
| 52 | + excerpt: "", |
| 53 | + matter: "", |
| 54 | + stringify: jest.fn(), |
| 55 | + orig: Buffer.from(""), |
| 56 | + language: "", |
| 57 | + }); |
| 58 | + |
| 59 | + const fundings = loadAllFundings(); |
| 60 | + |
| 61 | + expect(fundings).toHaveLength(2); |
| 62 | + expect(mockFs.readdirSync).toHaveBeenCalledTimes(1); |
| 63 | + expect(mockFs.readFileSync).toHaveBeenCalledTimes(2); |
| 64 | + }); |
| 65 | + |
| 66 | + it("converts funding markdown with escaped quotes", () => { |
| 67 | + const mockFileNames = ["funding1.md"]; |
| 68 | + const mockFundingData = generateMockFundingExportData({ |
| 69 | + id: "test-id", |
| 70 | + name: 'Test "Funding"', |
| 71 | + filename: "funding1", |
| 72 | + }); |
| 73 | + |
| 74 | + (mockFs.readdirSync as jest.Mock).mockReturnValue(mockFileNames); |
| 75 | + mockFs.readFileSync.mockReturnValue("mock file content"); |
| 76 | + mockMatter.mockReturnValue({ |
| 77 | + data: mockFundingData, |
| 78 | + content: 'Content with "quotes"', |
| 79 | + excerpt: "", |
| 80 | + matter: "", |
| 81 | + stringify: jest.fn(), |
| 82 | + orig: Buffer.from(""), |
| 83 | + language: "", |
| 84 | + }); |
| 85 | + |
| 86 | + const fundings = loadAllFundings(); |
| 87 | + |
| 88 | + expect(fundings[0].contentMd).toBe('Content with ""quotes""'); |
| 89 | + expect(fundings[0].filename).toBe("funding1"); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe("exportFundings", () => { |
| 94 | + it("exports fundings to CSV file", () => { |
| 95 | + const mockFileNames = ["funding1.md"]; |
| 96 | + const mockFundingData = generateMockFundingExportData({ |
| 97 | + id: "test-id", |
| 98 | + name: "Test Funding", |
| 99 | + filename: "funding1", |
| 100 | + urlSlug: "test-funding", |
| 101 | + callToActionLink: "https://example.com", |
| 102 | + callToActionText: "Apply Now", |
| 103 | + fundingType: "Grant", |
| 104 | + programPurpose: "Business Support", |
| 105 | + agency: ["Test Agency"], |
| 106 | + agencyContact: "contact@test.com", |
| 107 | + publishStageArchive: "", |
| 108 | + openDate: "2024-01-01", |
| 109 | + dueDate: "2024-12-31", |
| 110 | + status: "Open", |
| 111 | + programFrequency: "Annual", |
| 112 | + businessStage: "Early Stage", |
| 113 | + employeesRequired: "0", |
| 114 | + homeBased: "Yes", |
| 115 | + certifications: [], |
| 116 | + preferenceForOpportunityZone: "", |
| 117 | + county: "", |
| 118 | + sector: [], |
| 119 | + }); |
| 120 | + |
| 121 | + (mockFs.readdirSync as jest.Mock).mockReturnValue(mockFileNames); |
| 122 | + mockFs.readFileSync.mockReturnValue("test content"); |
| 123 | + mockMatter.mockReturnValue({ |
| 124 | + data: mockFundingData, |
| 125 | + content: "Test content", |
| 126 | + excerpt: "", |
| 127 | + matter: "", |
| 128 | + stringify: jest.fn(), |
| 129 | + orig: Buffer.from(""), |
| 130 | + language: "", |
| 131 | + }); |
| 132 | + mockFs.writeFileSync.mockImplementation(() => { |
| 133 | + // Mock implementation |
| 134 | + }); |
| 135 | + |
| 136 | + exportFundings(); |
| 137 | + |
| 138 | + expect(mockFs.writeFileSync).toHaveBeenCalledWith("fundings.csv", expect.stringContaining("id,name,filename")); |
| 139 | + expect(mockFs.writeFileSync).toHaveBeenCalledWith( |
| 140 | + "fundings.csv", |
| 141 | + expect.stringContaining('"test-id","Test Funding"') |
| 142 | + ); |
| 143 | + }); |
| 144 | + |
| 145 | + it("trims content markdown in CSV output", () => { |
| 146 | + const mockFileNames = ["funding1.md"]; |
| 147 | + const mockFundingData = generateMockFundingExportData({ |
| 148 | + id: "test-id", |
| 149 | + name: "Test Funding", |
| 150 | + filename: "funding1", |
| 151 | + urlSlug: "test-funding", |
| 152 | + callToActionLink: "", |
| 153 | + callToActionText: "", |
| 154 | + fundingType: "", |
| 155 | + programPurpose: "", |
| 156 | + agency: [], |
| 157 | + agencyContact: "", |
| 158 | + publishStageArchive: "", |
| 159 | + openDate: "", |
| 160 | + dueDate: "", |
| 161 | + status: "", |
| 162 | + programFrequency: "", |
| 163 | + businessStage: "", |
| 164 | + employeesRequired: "", |
| 165 | + homeBased: "", |
| 166 | + certifications: [], |
| 167 | + preferenceForOpportunityZone: "", |
| 168 | + county: "", |
| 169 | + sector: [], |
| 170 | + }); |
| 171 | + |
| 172 | + (mockFs.readdirSync as jest.Mock).mockReturnValue(mockFileNames); |
| 173 | + mockFs.readFileSync.mockReturnValue("test content"); |
| 174 | + mockMatter.mockReturnValue({ |
| 175 | + data: mockFundingData, |
| 176 | + content: " Test content with whitespace ", |
| 177 | + excerpt: "", |
| 178 | + matter: "", |
| 179 | + stringify: jest.fn(), |
| 180 | + orig: Buffer.from(""), |
| 181 | + language: "", |
| 182 | + }); |
| 183 | + mockFs.writeFileSync.mockImplementation(() => { |
| 184 | + // Mock implementation |
| 185 | + }); |
| 186 | + |
| 187 | + exportFundings(); |
| 188 | + |
| 189 | + const csvCall = mockFs.writeFileSync.mock.calls[0][1] as string; |
| 190 | + expect(csvCall).toContain('Test content with whitespace"'); |
| 191 | + expect(csvCall).not.toContain(' Test content with whitespace "'); |
| 192 | + }); |
| 193 | + }); |
| 194 | +}); |
0 commit comments