MCPcopy Create free account
hub / github.com/contextgo/contextgo / ensureLocalStorage

Function ensureLocalStorage

tests/unit/chatLayoutHooks.dom.test.ts:55–73  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

53
54// Provide a working localStorage mock if the environment lacks one
55function ensureLocalStorage() {
56 if (typeof globalThis.localStorage === 'undefined' || typeof globalThis.localStorage.getItem !== 'function') {
57 const store = new Map<string, string>();
58 const mock = {
59 getItem: (key: string) => store.get(key) ?? null,
60 setItem: (key: string, value: string) => store.set(key, String(value)),
61 removeItem: (key: string) => store.delete(key),
62 clear: () => store.clear(),
63 get length() {
64 return store.size;
65 },
66 key: (index: number) => [...store.keys()][index] ?? null,
67 };
68 Object.defineProperty(globalThis, 'localStorage', { value: mock, writable: true });
69 if (typeof window !== 'undefined') {
70 Object.defineProperty(window, 'localStorage', { value: mock, writable: true });
71 }
72 }
73}
74ensureLocalStorage();
75
76// Import hooks after mocks are set up

Callers 1

Calls 4

deleteMethod · 0.80
getMethod · 0.65
setMethod · 0.65
clearMethod · 0.65

Tested by

no test coverage detected