Files
web/scripts/i18n/syncDefaultMessage/syncIntlFormatMessage.test.ts
Joakim Jäderberg bf6ed7778e Merged in feat/syncDefaultMessage (pull request #3022)
Sync defaultMessage from lokalise

* Enhance translation sync functionality and tests

- Added logging for found component files during sync.
- Introduced tests for handling complex components with replacements.
- Updated regex in syncIntlFormatMessage to support optional second arguments.
- Removed unused test files.

* feat(syncDefaultMessage): add script for syncing default message with lokalise

* feat(syncDefaultMessage): add script for syncing default message with lokalise


Approved-by: Matilda Landström
2025-10-30 08:38:50 +00:00

137 lines
4.7 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { syncIntlFormatMessage } from "./syncIntlFormatMessage";
describe("syncIntlFormatMessage", () => {
it("updated false when given empty file and no translations", () => {
expect(
syncIntlFormatMessage({ fileContent: "", translations: {} })
).toEqual({ updated: false, fileContent: "" });
});
it("updates int.formatMessage components", () => {
expect(
syncIntlFormatMessage({
fileContent:
'intl.formatMessage({ id: "myKey", defaultMessage: "old message" })',
translations: { myKey: "new message" },
})
).toEqual({
updated: true,
fileContent:
'intl.formatMessage({ id: "myKey", defaultMessage: "new message" })',
});
});
it("updates multiline int.formatMessage", () => {
expect(
syncIntlFormatMessage({
fileContent: `intl.formatMessage({\n\tid: "myKey",\n\tdefaultMessage: "old message"\n })`,
translations: { myKey: "new message" },
})
).toEqual({
updated: true,
fileContent: `intl.formatMessage({\n\tid: "myKey",\n\tdefaultMessage: "new message"\n })`,
});
});
it("updates multiple int.formatMessage components", () => {
expect(
syncIntlFormatMessage({
fileContent:
'intl.formatMessage({ id: "myKey", defaultMessage: "old message" })' +
'intl.formatMessage({ id: "anotherKey", defaultMessage: "another old message" })',
translations: {
myKey: "new message",
anotherKey: "another new message",
},
})
).toEqual({
updated: true,
fileContent:
'intl.formatMessage({ id: "myKey", defaultMessage: "new message" })' +
'intl.formatMessage({ id: "anotherKey", defaultMessage: "another new message" })',
});
});
it("updates nothing if no key was found", () => {
const fileContent =
'intl.formatMessage({ id: "myKey", defaultMessage: "old message" })' +
'intl.formatMessage({ id: "anotherKey", defaultMessage: "another old message" })';
expect(
syncIntlFormatMessage({
fileContent,
translations: {
unusedKey: "new message",
},
})
).toEqual({
updated: false,
fileContent,
});
});
it("updates nothing if not using intl.formatMessage", () => {
const fileContent =
'formatMessage({ id: "myKey", defaultMessage: "old message" })';
expect(
syncIntlFormatMessage({
fileContent,
translations: {
myKey: "new message",
},
})
).toEqual({
updated: false,
fileContent,
});
});
it("updates nothing if no defaultMessage is present", () => {
const fileContent = 'formatMessage({ id: "myKey" })';
expect(
syncIntlFormatMessage({
fileContent,
translations: {
myKey: "new message",
},
})
).toEqual({
updated: false,
fileContent,
});
});
it("handles formatMessage with replacements", () => {
const fileContent =
'intl.formatMessage({ id: "myKey", defaultMessage: "<stuff>old message</stuff>" }, { stuff: (str) => <span>{str}</span> } })';
expect(
syncIntlFormatMessage({
fileContent,
translations: {
myKey: "new message",
},
})
).toEqual({
updated: true,
fileContent:
'intl.formatMessage({ id: "myKey", defaultMessage: "new message" }, { stuff: (str) => <span>{str}</span> } })',
});
});
it("handles formatMessage with replacements", () => {
const fileContent =
'intl.formatMessage({ id: "myKey", defaultMessage: "<stuff>old message</stuff>" }, { stuff: (str) => <span>{str}</span> } })';
expect(
syncIntlFormatMessage({
fileContent,
translations: {
myKey: "<stuff>new message</stuff>",
},
})
).toEqual({
updated: true,
fileContent:
'intl.formatMessage({ id: "myKey", defaultMessage: "<stuff>new message</stuff>" }, { stuff: (str) => <span>{str}</span> } })',
});
});
});