-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsetup.ts
More file actions
71 lines (60 loc) · 1.74 KB
/
Copy pathsetup.ts
File metadata and controls
71 lines (60 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { beforeEach } from "vitest";
function createLocalStoragePolyfill(): Storage {
const store: Record<string, string> = {};
const storageTarget: Record<string, string> = {};
return new Proxy(storageTarget, {
get(target, prop) {
if (prop === "getItem") {
return (key: string): string | null => store[key] ?? null;
}
if (prop === "setItem") {
return (key: string, value: string): void => {
store[key] = value;
target[key] = value;
};
}
if (prop === "removeItem") {
return (key: string): void => {
delete store[key];
delete target[key];
};
}
if (prop === "clear") {
return (): void => {
Object.keys(store).forEach((key) => delete store[key]);
Object.keys(target).forEach((key) => delete target[key]);
};
}
if (prop === "length") {
return Object.keys(store).length;
}
if (prop === "key") {
return (index: number): string | null => Object.keys(store)[index] ?? null;
}
return target[prop as string];
},
ownKeys() {
return Object.keys(store);
},
getOwnPropertyDescriptor(target, prop) {
if (typeof prop === "string" && prop in store) {
return {
enumerable: true,
configurable: true,
value: store[prop]
};
}
return Object.getOwnPropertyDescriptor(target, prop);
}
}) as Storage;
}
if (typeof localStorage === "undefined" || typeof localStorage.clear !== "function") {
Object.defineProperty(globalThis, "localStorage", {
value: createLocalStoragePolyfill(),
configurable: true,
writable: true
});
}
beforeEach(() => {
localStorage.clear();
});