Feat/lokalise rebuild * chore(lokalise): update translation ids * chore(lokalise): easier to switch between projects * chore(lokalise): update translation ids * . * . * . * . * . * . * chore(lokalise): update translation ids * chore(lokalise): update translation ids * . * . * . * chore(lokalise): update translation ids * chore(lokalise): update translation ids * . * . * chore(lokalise): update translation ids * chore(lokalise): update translation ids * chore(lokalise): new translations * merge * switch to errors for missing id's * merge * sync translations Approved-by: Linus Flood
93 lines
3.0 KiB
TypeScript
93 lines
3.0 KiB
TypeScript
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"));
|
|
});
|
|
});
|
|
|
|
function createMockComponent(translationId: string, defaultMessage: string) {
|
|
return `export function TestComponent() {
|
|
const { intl } = useIntl();
|
|
|
|
const message = intl.formatMessage({
|
|
id: "${translationId}",
|
|
defaultMessage: "${defaultMessage}",
|
|
});
|
|
return <div>{message}</div>;
|
|
}`;
|
|
}
|