test: add frontend test suite
Some checks failed
Deploy to Frontend Servers / deploy (push) Failing after 43s

This commit is contained in:
TerryM
2026-05-16 18:21:37 +08:00
parent f59d1e8e2a
commit a29ec8ed92
17 changed files with 1624 additions and 12 deletions

49
src/test/setup.ts Normal file
View File

@@ -0,0 +1,49 @@
import "@testing-library/jest-dom/vitest";
import { afterEach, beforeEach, vi } from "vitest";
import { cleanup } from "@testing-library/react";
function createLocalStorageMock(): Storage {
let store: Record<string, string> = {};
return {
get length() {
return Object.keys(store).length;
},
clear() {
store = {};
},
getItem(key: string) {
return Object.prototype.hasOwnProperty.call(store, key)
? store[key]
: null;
},
key(index: number) {
return Object.keys(store)[index] ?? null;
},
removeItem(key: string) {
delete store[key];
},
setItem(key: string, value: string) {
store[key] = String(value);
},
};
}
beforeEach(() => {
const storage = createLocalStorageMock();
Object.defineProperty(globalThis, "localStorage", {
value: storage,
configurable: true,
});
Object.defineProperty(window, "localStorage", {
value: storage,
configurable: true,
});
});
afterEach(() => {
cleanup();
globalThis.localStorage.clear();
vi.restoreAllMocks();
vi.unstubAllGlobals();
vi.unstubAllEnvs();
});