Files
beacon-web/tests/setup.ts
T
MrAlders0n 88918db48c perf(i18n): load only the active language
English stays in the main bundle as the fallback; French and Swedish become
their own chunks, fetched by an i18next backend the first time they're used.
The runtime-env chunk drops from 147.00 kB (43.93 kB gzip) to 83.37 kB
(27.51 kB gzip). The picker's native names come from the catalogs at build
time, so listing a language doesn't load it.

changeLanguage() loads the catalog before it switches, so the UI goes from one
complete language to the next. A saved non-English choice delays the first
render until its catalog is in rather than painting English and switching.

App: no change; it ships its locales as bundled assets read from rootBundle
and has no web image.
2026-10-27 15:25:31 -04:00

49 lines
1.9 KiB
TypeScript

import "@testing-library/jest-dom/vitest";
import { cleanup } from "@testing-library/react";
import { afterEach, beforeAll, vi } from "vitest";
import i18n, { languages } from "../src/i18n";
// jsdom doesn't implement matchMedia. Provide a default stub so components that read media queries
// mount as "desktop" by default — a hover-capable pointer, not below the mobile width. Individual
// tests can override window.matchMedia with their own controllable mock to drive a query.
// (hover: …) → true keeps hover-driven UI (tooltips, the path popover) in hover mode by default;
// width queries → false keep the desktop layout.
if (!window.matchMedia) {
window.matchMedia = vi.fn().mockImplementation((query: string) => ({
matches: /hover/.test(query),
media: query,
onchange: null,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
addListener: vi.fn(),
removeListener: vi.fn(),
dispatchEvent: vi.fn(),
}));
}
// Node 26 exposes `localStorage` as undefined without `--localstorage-file`, shadowing jsdom's.
if (!globalThis.localStorage) {
const store = new Map<string, string>();
Object.defineProperty(globalThis, "localStorage", {
value: {
getItem: (key: string) => (store.has(key) ? store.get(key)! : null),
setItem: (key: string, value: string) => store.set(key, String(value)),
removeItem: (key: string) => store.delete(key),
clear: () => store.clear(),
key: (index: number) => Array.from(store.keys())[index] ?? null,
get length() { return store.size; },
},
configurable: true,
writable: true,
});
}
// The app loads a non-English catalog on first use; tests may read any language without switching to it.
beforeAll(() => i18n.loadLanguages(languages.map(({ code }) => code)));
afterEach(async () => {
cleanup();
await i18n.changeLanguage("en");
try { localStorage.clear(); } catch { /* stubbed per-test */ }
});