Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/frontend/tests/locales.test.mjs
blob: 0b87174f3cb6426e9188a90f31f84d0eb3bae1f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1

import assert from "node:assert/strict";
import { test } from "node:test";
import {
  browserLocale,
  fallbackLocale,
  forwardArrow,
  initialLocale,
  isLocale,
  isVisitorChoice,
  localeLabels,
  supportedLocales,
  textDirection,
} from "../src/locales.ts";

test("four languages are published and English is the fallback", () => {
  assert.deepEqual(supportedLocales, ["en-US", "de-DE", "ru-RU", "he-IL"]);
  assert.equal(fallbackLocale, "en-US");
  for (const locale of supportedLocales) {
    assert.equal(isLocale(locale), true, locale);
    assert.match(localeLabels[locale], /^[A-Z]{2}$/);
  }
  for (const value of ["en", "he", "tr-TR", "", null, undefined, 7]) {
    assert.equal(isLocale(value), false, String(value));
  }
});

test("the browser language selects the page language", () => {
  const cases = [
    [["de-AT", "en"], "de-DE"],
    [["ru"], "ru-RU"],
    [["he-IL", "en-US"], "he-IL"],
    [["iw"], "he-IL"],
    [["HE"], "he-IL"],
    [["en-GB"], "en-US"],
    // An unpublished first choice falls through to the next preference.
    [["tr-TR", "de"], "de-DE"],
  ];
  for (const [languages, expected] of cases) {
    assert.equal(browserLocale(languages), expected, languages.join(","));
  }
});

test("a browser language that is not published falls back to English", () => {
  for (const languages of [[], ["tr-TR"], ["tr-TR", "fr", "ja"], [""], ["constructor"]]) {
    assert.equal(browserLocale(languages), "en-US", languages.join(","));
  }
});

test("a language the visitor chose wins over the browser language", () => {
  assert.equal(initialLocale("de-DE", ["he-IL"]), "de-DE");
  assert.equal(initialLocale("en-US", ["ru"]), "en-US");
  assert.equal(initialLocale(null, ["he-IL"]), "he-IL");
  // A stored value that is not a published locale is not a choice.
  assert.equal(initialLocale("tr-TR", ["ru"]), "ru-RU");
  assert.equal(initialLocale("", []), "en-US");
});

test("only Hebrew text runs right to left", () => {
  for (const locale of supportedLocales) {
    const expected = locale === "he-IL" ? "rtl" : "ltr";
    assert.equal(textDirection(locale), expected, locale);
    assert.equal(forwardArrow(locale), expected === "rtl" ? "←" : "→", locale);
  }
});

test("a language change is remembered only after the visitor acted on the page", () => {
  assert.equal(isVisitorChoice({ hasBeenActive: true }), true);
  assert.equal(isVisitorChoice({ hasBeenActive: false }), false);
  // A browser that does not report activation cannot tell; the change counts.
  assert.equal(isVisitorChoice(undefined), true);
});