Files
paseo/packages/app/src/test/window-local-storage.ts
T

39 lines
769 B
TypeScript

class MemoryLocalStorage {
private readonly map = new Map<string, string>();
get length(): number {
return this.map.size;
}
clear(): void {
this.map.clear();
}
getItem(key: string): string | null {
return this.map.has(key) ? (this.map.get(key) ?? null) : null;
}
key(index: number): string | null {
return Array.from(this.map.keys())[index] ?? null;
}
removeItem(key: string): void {
this.map.delete(key);
}
setItem(key: string, value: string): void {
this.map.set(key, value);
}
}
if (typeof window === "undefined") {
const storage = new MemoryLocalStorage();
Object.defineProperty(globalThis, "window", {
value: {
localStorage: storage,
},
configurable: true,
writable: true,
});
}