Skip to content

Commit

Permalink
Add unit test for l10n
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Oct 17, 2024
1 parent 84d6775 commit ebd1848
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/web/l10n.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class L10n {
}

constructor(language) {
this.language = language || "en-US";
this.language = language || "en";
this.ready = this.load().then(() => true);
}

Expand All @@ -42,7 +42,7 @@ export class L10n {
const [locale, fallbackLocale, defaultLocale] = await Promise.all([
L10n.loadLocale(this.language),
L10n.loadLocale(this.language.split("-")[0]),
L10n.loadLocale("en-US"),
L10n.loadLocale("en"),
]);
this.locale = locale;
this.fallbackLocale = fallbackLocale;
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/locales/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"effectiveMessage": "Message with effective content",
"blankMessage": "Blank message",
"messageWithPlaceholders": "Message with placeholders: ${one}, ${two}",
"missingFallbackMessage": "Message defined in fallback locales",
"missingMessage": "Message not defined in non-default locales"
}
7 changes: 7 additions & 0 deletions tests/fixtures/locales/ja-JP.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"effectiveMessage": "JP:意味ある内容を含むメッセージ",
"blankMessage": "",
"messageWithPlaceholders": "JP:プレースホルダーを含むメッセージ:${one}, ${two}, ${three}",
"_missingFallbackMessage": "JP:フォールバック先で定義されているメッセージ",
"_missingMessage": "JP:未定義のメッセージ"
}
7 changes: 7 additions & 0 deletions tests/fixtures/locales/ja.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"effectiveMessage": "意味ある内容を含むメッセージ",
"blankMessage": "空のメッセージ",
"messageWithPlaceholders": "プレースホルダーを含むメッセージ:${one}, ${two}",
"missingFallbackMessage": "フォールバック先で定義されているメッセージ",
"_missingMessage": "未定義のメッセージ"
}
50 changes: 50 additions & 0 deletions tests/unit/test-l10n.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
'use strict';

import "./l10n.mjs";
import { L10n } from "../../src/web/l10n.mjs";
import { assert } from "tiny-esm-test-runner";
const { is } = assert;

let l10n;

export async function setUp() {
L10n.clearCache();
L10n.baseUrl = (new URL(`${import.meta.url}/../../fixtures/`)).toString();
l10n = new L10n("ja-JP");
await l10n.ready;
}

test_get.parameters = {
effective: {
key: "effectiveMessage",
expected: "JP:意味ある内容を含むメッセージ",
},
blank: {
key: "blankMessage",
expected: "",
},
withPlaceholders: {
key: "messageWithPlaceholders",
params: {
one: "One",
two: "Two",
},
expected: "JP:プレースホルダーを含むメッセージ:One, Two, ${three}",
},
fallbackToGeneralLocale: {
key: "missingFallbackMessage",
expected: "フォールバック先で定義されているメッセージ",
},
fallbackToDefaultLocale: {
key: "missingMessage",
expected: "Message not defined in non-default locales",
},
};
export function test_get({ key, params, expected }) {
is(expected, l10n.get(key, params || null));
}

0 comments on commit ebd1848

Please sign in to comment.