test(@websnacksjs/i18n): fix broken tests missing translation messages (#5)

This commit is contained in:
M. George Hansen 2025-08-20 08:57:15 +12:00
parent 2867c2f875
commit d6696a150b
Signed by: mgeorgehansen
SSH key fingerprint: SHA256:JlIGiQLPyQ2RHTH3a2oVlb20Xkh9Glr8DUF4YTXHJxM
10 changed files with 35 additions and 65 deletions

View file

@ -1 +1 @@
!/tests/messages/*/*.json
!/tests/fixtures/**/*.json

View file

@ -1,61 +0,0 @@
import assert from "node:assert/strict";
import { beforeEach, describe, it } from "node:test";
import type I18n from "@websnacksjs/i18n";
import { type Fixtures, withFixture } from "../fixtures.js";
describe("i18n.supportedLocales()", () => {
let i18n: I18n<Fixtures["base"]>;
beforeEach(() => {
i18n = withFixture("base", {
supportedLocales: ["en", "fr", "fr-Arab"],
});
});
it("returns maximized locales for all declared, supported locales", () => {
assert.deepEqual(i18n.supportedLocales(), [
"en-Latn-US",
"fr-Latn-FR",
"fr-Arab-FR",
]);
});
});
describe("i18n.loadMessages(...)", () => {
let i18n: I18n<Fixtures["base"]>;
beforeEach(() => {
i18n = withFixture("base");
});
it("guesses region of locales w/o region tags", async () => {
const t = await i18n.loadMessages({
locale: "fr-Latn",
});
assert.equal(t.locale(), "fr-Latn-FR");
});
it("guesses script of locales w/ region tags", async () => {
const t = await i18n.loadMessages({
locale: "fr-FR",
});
assert.equal(t.locale(), "fr-Latn-FR");
});
it("guesses script & region of bare language locales", async () => {
const t = await i18n.loadMessages({
locale: "fr",
});
assert.equal(t.locale(), "fr-Latn-FR");
});
it("does NOT fallback to bare language locales", async () => {
await assert.rejects(
i18n.loadMessages({
locale: "en-Arab",
}),
{
message:
'no declared locale matches requested locale of "en-Arab-US" (maximized from "en-Arab")',
},
);
});
});

View file

@ -2,8 +2,8 @@ import I18n, { type I18nOptions } from "@websnacksjs/i18n";
export type Fixtures = {
base: {
common: typeof import("./fixtures/base/messages/en/common.json");
drama: typeof import("./fixtures/base/messages/en/drama.json");
common: typeof import("./fixtures/base/en/common.json");
drama: typeof import("./fixtures/base/en/drama.json");
};
};
@ -17,7 +17,7 @@ export const withFixture = <F extends keyof Fixtures>(
supportedLocales: ["en", "fr", "fr-Arab"],
namespaces: ["drama"],
messagesUrlTemplate: new URL(
"./fixtures/base/messages/:locale/:namespace.json",
"./fixtures/base/:locale/:namespace.json",
import.meta.url,
),
...(overrides as Partial<I18nOptions<Fixtures["base"]>>),

View file

@ -0,0 +1,7 @@
{
"oh hai": "Oh hai {{name}}!",
"denial": "I did not hit her. I did not.",
"flower shop": {
"doggy": "hello doggy!"
}
}

View file

@ -0,0 +1,3 @@
{
"tearing me apart": "You're tearing me apart, Lisa!"
}

View file

@ -0,0 +1,7 @@
{
"oh hai": "أوه سالو، \u2066{{name}}\u2069!",
"denial": "جُ ن لي با فرابيه. جُ ن لي با.",
"flower shop": {
"doggy": "سالو توتو!"
}
}

View file

@ -0,0 +1,3 @@
{
"tearing me apart": "تو م ديشير، ليزا!"
}

View file

@ -0,0 +1,7 @@
{
"oh hai": "Oh salut, Mark !",
"denial": "Je ne l'ai pas frappée. Je ne l'ai pas.",
"flower shop": {
"doggy": "Salut toutou !"
}
}

View file

@ -0,0 +1,3 @@
{
"tearing me apart": "Tu me déchires, Lisa !"
}

View file

@ -2,6 +2,7 @@
"extends": "../tsconfig.json",
"compilerOptions": {
"resolveJsonModule": true,
"allowImportingTsExtensions": true,
"noEmit": true,
"rootDir": "."
},