Debian Codegraph @

web/src/test/setup.js

raw
1 /* eslint-env node, es2021 */
2 // Minimal browser-global stubs so the pure-logic and API modules import + run under the node
3 // environment, without pulling in jsdom. utils.js -> config.js does `const { config } = window;`
4 // at import time, and config.js falls back to window.location.origin when base_url is empty.
5 const config = { base_url: "https://ntfy.sh", disallowed_topics: ["app", "account", "settings"] };
6
7 globalThis.window = {
8 location: { origin: "https://ntfy.sh" },
9 config,
10 atob: globalThis.atob, // urlB64ToUint8Array uses window.atob; Node provides global atob
11 };
12
13 // In the browser, `window` IS the global object, so `window.config` is also reachable as a bare
14 // `config` identifier -- Api.js/AccountApi.js/UserManager.js/SubscriptionManager.js rely on that.
15 // Node's `window` is just a plain object, so expose the same object as a real global too.
16 globalThis.config = config;
17
18 // utils.js -> Prefs.js -> db.js -> Session.username() reads localStorage at module load.
19 // Node has no localStorage; an in-memory stand-in is enough for the pure-logic tests.
20 const store = new Map();
21 globalThis.localStorage = {
22 getItem: (key) => (store.has(key) ? store.get(key) : null),
23 setItem: (key, value) => store.set(key, String(value)),
24 removeItem: (key) => store.delete(key),
25 clear: () => store.clear(),
26 };