import { vi, it, expect, beforeEach, afterEach, describe } from "vitest"; describe("syncFile", () => { beforeEach(() => { vi.resetModules(); vi.mock("fs", () => { const existsMock = vi.fn(); const readMock = vi.fn(); const writeMock = vi.fn(); return { existsSync: existsMock, readFileSync: readMock, writeFileSync: writeMock, default: { existsSync: existsMock, readFileSync: readMock, writeFileSync: writeMock, }, }; }); }); afterEach(() => { vi.restoreAllMocks(); }); it("throws if file does not exist", async () => { const fsMock = (await import("fs")) as any; fsMock.existsSync.mockReturnValue(false); const { syncFile } = await import("./syncFile"); expect(() => syncFile({ path: "missing.ts", translations: {} }) ).toThrow("File not found: missing.ts"); expect(fsMock.readFileSync).not.toHaveBeenCalled(); expect(fsMock.writeFileSync).not.toHaveBeenCalled(); }); it("reads file, calls syncIntlFormatMessage, writes updated content and returns it", async () => { const fsMock = (await import("fs")) as any; fsMock.existsSync.mockReturnValue(true); fsMock.readFileSync.mockReturnValue( createMockComponent("myKey", "old message") ); const { syncFile } = await import("./syncFile"); const { fileContent: result } = syncFile({ path: "file.ts", translations: { myKey: "new message" }, }); expect(fsMock.readFileSync).toHaveBeenCalledWith("file.ts", "utf-8"); expect(fsMock.writeFileSync).toHaveBeenCalled(); expect(result).toEqual(createMockComponent("myKey", "new message")); }); it("reads file, calls syncIntlFormatMessage, ignores content if there are no matching keys, writes updated content and returns it", async () => { const fsMock = (await import("fs")) as any; fsMock.existsSync.mockReturnValue(true); fsMock.readFileSync.mockReturnValue( createMockComponent("myKey", "old message") ); const { syncFile } = await import("./syncFile"); const { fileContent: result } = syncFile({ path: "file.ts", translations: { someOtherKey: "not present" }, }); expect(fsMock.readFileSync).toHaveBeenCalledWith("file.ts", "utf-8"); // expect(fsMock.writeFileSync).toHaveBeenCalled(); expect(result).toEqual(createMockComponent("myKey", "old message")); }); it("updates complex components with replacements", async () => { const fsMock = (await import("fs")) as any; fsMock.existsSync.mockReturnValue(true); fsMock.readFileSync.mockReturnValue( createComplexMockComponent( "complexKey", "Yes, I accept the general Booking & Cancellation Terms, and understand that Scandic will process my personal data in accordance with Scandic's Privacy policy." ) ); const { syncFile } = await import("./syncFile"); const { fileContent: result } = syncFile({ path: "file.ts", translations: { complexKey: "replace this text" }, }); expect(fsMock.readFileSync).toHaveBeenCalledWith("file.ts", "utf-8"); // expect(fsMock.writeFileSync).toHaveBeenCalled(); expect(result).toContain("replace this text"); }); }); function createMockComponent(translationId: string, defaultMessage: string) { return `export function TestComponent() { const { intl } = useIntl(); const message = intl.formatMessage({ id: "${translationId}", defaultMessage: "${defaultMessage}", }); return
{message}
; }`; } function createComplexMockComponent( translationId: string, defaultMessage: string ) { return `export function TestComponent() { const intl = useIntl(); return (
{intl.formatMessage( { id: "${translationId}", defaultMessage: "${defaultMessage}", }, { replacement: (str) => {str}, } )}
); }`; }