Files
MetonaToast/tests/ssr.test.ts
T
tianhao 5f8b9d2f75
CI / test (18.x) (push) Successful in 10m8s
CI / test (20.x) (push) Successful in 10m10s
CI / test (22.x) (push) Successful in 10m4s
CI / test (24.x) (push) Successful in 10m2s
chore: 版本回退至 0.5.0 并重建产物(含 CJS 兼容修复)
2026-08-08 15:40:04 +08:00

59 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 环境隔离测试 — 需要切换 document/window/localStorage 环境并调用 jest.resetModules()。
* 独立成文件:resetModules 会清空模块缓存,若与其他测试同文件会导致
* 后续 require 拿到新模块实例、与 import 时绑定的实例状态分裂。
* 注:jsdom 以 getter 暴露 document/window,普通赋值无效,须用 defineProperty 覆盖。
* @module tests
* @version 0.5.0
*/
/** 临时移除全局对象,返回恢复函数 */
const hideGlobal = (key: string): (() => void) => {
const desc = Object.getOwnPropertyDescriptor(global, key);
Object.defineProperty(global, key, { value: undefined, configurable: true, writable: true });
return () => {
if (desc) Object.defineProperty(global, key, desc);
else delete (global as Record<string, unknown>)[key];
};
};
describe('环境隔离(SSR / localStorage 异常路径)', () => {
test('escapeHTML SSR 路径(无 document', () => {
const restore = hideGlobal('document');
jest.resetModules();
const { escapeHTML } = require('../src/utils.js');
const result = escapeHTML('<script>alert("xss")</script>');
expect(result).not.toContain('<script>');
expect(result).toContain('&lt;script&gt;');
restore();
});
test('prefersDark 无 window 时返回 false', () => {
const restore = hideGlobal('window');
jest.resetModules();
const { prefersDark } = require('../src/utils.js');
expect(prefersDark()).toBe(false);
restore();
});
test('saveTheme localStorage 异常被捕获', () => {
const orig = (global as Record<string, unknown>).localStorage;
(global as Record<string, unknown>).localStorage = undefined;
jest.resetModules();
const { saveTheme } = require('../src/themes.js');
expect(() => saveTheme('dark')).not.toThrow();
(global as Record<string, unknown>).localStorage = orig;
});
test('loadLocale localStorage 异常返回 default', () => {
const orig = (global as Record<string, unknown>).localStorage;
(global as Record<string, unknown>).localStorage = {
getItem: () => { throw new Error('quota'); },
};
jest.resetModules();
const { loadLocale } = require('../src/i18n.js');
expect(typeof loadLocale()).toBe('string');
(global as Record<string, unknown>).localStorage = orig;
});
});