Ported from babyboo and trimmed to back-office labels: list with search and the Missing Chinese filter, edit panel with placeholder checks and an unsaved-changes guard, export, import with preview and summary, and the Languages page with guarded switches. A blank translation is no longer treated as an old placeholder mismatch, so Save blocks a broken new one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
34 lines
1.7 KiB
TypeScript
34 lines
1.7 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { buildContent, buildImportPreview, parseContent, placeholderMismatch } from "./translationText";
|
|
|
|
describe("translationText", () => {
|
|
it("parses content and never throws on bad JSON", () => {
|
|
expect(parseContent('{"en_US":"Leads","zh_CN":"线索"}')).toEqual({ en_US: "Leads", zh_CN: "线索" });
|
|
expect(parseContent("not json")).toEqual({});
|
|
expect(parseContent("[1]")).toEqual({});
|
|
expect(parseContent(null)).toEqual({});
|
|
});
|
|
|
|
it("applies edits, drops blanks and keeps languages the form did not show", () => {
|
|
const stored = '{"en_US":"Leads","ms_MY":"Petunjuk","zh_CN":"旧"}';
|
|
expect(JSON.parse(buildContent(stored, { en_US: " Leads ", zh_CN: "线索" }))).toEqual({ en_US: "Leads", ms_MY: "Petunjuk", zh_CN: "线索" });
|
|
expect(JSON.parse(buildContent(stored, { zh_CN: " " }))).toEqual({ en_US: "Leads", ms_MY: "Petunjuk" });
|
|
});
|
|
|
|
it("compares placeholders as sets", () => {
|
|
expect(placeholderMismatch("Hi {name}, {count} new", "{count} 条新,{name}")).toEqual({ missing: [], extra: [] });
|
|
expect(placeholderMismatch("Hi {name}", "你好 {nam}")).toEqual({ missing: ["{name}"], extra: ["{nam}"] });
|
|
});
|
|
|
|
it("previews create or update by code ignoring case, ignoring ids", () => {
|
|
const existing = [{ id: 7, code: "Leads", content: '{"en_US":"Leads"}' }];
|
|
expect(buildImportPreview([
|
|
{ id: 99, code: "leads", content: '{"en_US":"Leads!"}' },
|
|
{ code: "NewOne", content: '{"en_US":"New"}' },
|
|
], existing)).toEqual([
|
|
{ code: "leads", action: "update", currentEn: "Leads", incomingEn: "Leads!" },
|
|
{ code: "NewOne", action: "create", incomingEn: "New" },
|
|
]);
|
|
});
|
|
});
|