50 lines
1.1 KiB
TypeScript
50 lines
1.1 KiB
TypeScript
|
|
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();
|
||
|
|
});
|