mirror of
https://github.com/multica-ai/multica.git
synced 2026-06-17 03:38:32 +02:00
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import "@testing-library/jest-dom/vitest";
|
|
|
|
function createMemoryStorage(): Storage {
|
|
const values = new Map<string, string>();
|
|
|
|
return {
|
|
get length() {
|
|
return values.size;
|
|
},
|
|
clear: () => values.clear(),
|
|
getItem: (key: string) => values.get(key) ?? null,
|
|
key: (index: number) => Array.from(values.keys())[index] ?? null,
|
|
removeItem: (key: string) => {
|
|
values.delete(key);
|
|
},
|
|
setItem: (key: string, value: string) => {
|
|
values.set(key, value);
|
|
},
|
|
};
|
|
}
|
|
|
|
const localStorageIsUsable =
|
|
typeof globalThis.localStorage?.getItem === "function" &&
|
|
typeof globalThis.localStorage?.setItem === "function" &&
|
|
typeof globalThis.localStorage?.removeItem === "function" &&
|
|
typeof globalThis.localStorage?.clear === "function";
|
|
|
|
if (!localStorageIsUsable) {
|
|
const storage = createMemoryStorage();
|
|
Object.defineProperty(globalThis, "localStorage", {
|
|
configurable: true,
|
|
value: storage,
|
|
});
|
|
Object.defineProperty(window, "localStorage", {
|
|
configurable: true,
|
|
value: storage,
|
|
});
|
|
}
|