src/test/i18nMock.ts holds the one fake t of the suite: markedT renders
t(key) or t(key[a=1,b=2]), and i18nMock({ language?, fixture? }) builds
the react-i18next module for vi.mock. The local echo of
useLicenseMachines.test.ts is replaced by markedT.
Refs #389
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
41 lines
1.6 KiB
TypeScript
41 lines
1.6 KiB
TypeScript
// The shared fake `t` (#389): its mark is the convention every assertion reads.
|
|
import { describe, it, expect } from "vitest";
|
|
import { i18nMock, markedT } from "./i18nMock";
|
|
|
|
describe("markedT", () => {
|
|
it("marks a key without options", () => {
|
|
expect(markedT("license.removeKey.confirmValid")).toBe("t(license.removeKey.confirmValid)");
|
|
});
|
|
|
|
it("marks a key with its options, in their order", () => {
|
|
expect(markedT("budget.count", { count: 2, name: "x" })).toBe("t(budget.count[count=2,name=x])");
|
|
});
|
|
|
|
it("never returns the bare key", () => {
|
|
expect(markedT("a.b")).not.toBe("a.b");
|
|
});
|
|
});
|
|
|
|
describe("i18nMock", () => {
|
|
it("hands out the marked t and the French language by default", () => {
|
|
const { t, i18n } = i18nMock().useTranslation();
|
|
expect(t("a.b", { n: 1 })).toBe("t(a.b[n=1])");
|
|
expect(i18n.language).toBe("fr");
|
|
});
|
|
|
|
it("reads a language function at each access", () => {
|
|
let language = "fr";
|
|
const { i18n } = i18nMock({ language: () => language }).useTranslation();
|
|
expect(i18n.language).toBe("fr");
|
|
language = "en";
|
|
expect(i18n.language).toBe("en");
|
|
});
|
|
|
|
it("returns the fixture value for a fixture key, the mark for any other", () => {
|
|
const value = [{ id: "x" }];
|
|
const { t } = i18nMock({ fixture: { "docs.a.subsections": value } }).useTranslation();
|
|
expect(t("docs.a.subsections", { returnObjects: true })).toBe(value);
|
|
expect(t("docs.b.subsections", { returnObjects: true })).toBe("t(docs.b.subsections[returnObjects=true])");
|
|
expect(t("toString")).toBe("t(toString)");
|
|
});
|
|
});
|