Merged in feat/lokalise-rebuild (pull request #2993)
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
This commit is contained in:
92
scripts/i18n/syncDefaultMessage/syncFile.test.ts
Normal file
92
scripts/i18n/syncDefaultMessage/syncFile.test.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
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>;
|
||||
}`;
|
||||
}
|
||||
Reference in New Issue
Block a user