UNPKG

1.01 kBPlain TextView Raw
1export default class Storage {
2 getItem = jest.fn(this.unmockedGetItem);
3
4 setItem = jest.fn(this.unmockedSetItem);
5
6 removeItem = jest.fn(this.unmockedRemoveItem);
7
8 clear = jest.fn(this.unmockedClearItem);
9
10 private store: {
11 [key: string]: string;
12 } = Object.create(null);
13
14 restore() {
15 this.getItem.mockClear();
16 this.getItem.mockImplementation(this.unmockedGetItem);
17
18 this.setItem.mockClear();
19 this.setItem.mockImplementation(this.unmockedSetItem);
20
21 this.removeItem.mockClear();
22 this.removeItem.mockImplementation(this.unmockedRemoveItem);
23
24 this.clear.mockClear();
25 this.clear.mockImplementation(this.unmockedClearItem);
26
27 this.clear();
28 }
29
30 private unmockedGetItem(key: string) {
31 return this.store[key] || null;
32 }
33
34 private unmockedSetItem(key: string, value: any) {
35 this.store[key] = value.toString();
36 }
37
38 private unmockedRemoveItem(key: string) {
39 delete this.store[key];
40 }
41
42 private unmockedClearItem() {
43 this.store = {};
44 }
45}