-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbrowserMocks.ts
56 lines (44 loc) · 1.13 KB
/
browserMocks.ts
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
import fetch from 'isomorphic-fetch';
class LocalStorageMock implements Storage {
private store: Record<string, string> = {};
public get length() {
return Object.keys(this.store).length;
}
public getItem(key: string) {
return this.store[key] || null;
}
public setItem(key: string, value: string) {
this.store[key] = value;
}
public removeItem(key: string) {
delete this.store[key];
}
public key(index: number): string | null {
return Object.keys(this.store)[index] ?? null;
}
public clear() {
this.store = {};
}
public replaceWith(newStore: Record<string, string>) {
this.store = newStore;
}
}
Object.defineProperty(window, 'localStorage', {
value: new LocalStorageMock(),
});
// Adjust the console logging behaviour during test runs.
/* eslint-disable no-console */
Object.defineProperty(window, 'console', {
value: {
log: console.log,
error: console.error,
warn: jest.fn(), // warnings are surpressed.
info: console.info,
debug: console.debug,
},
});
/* eslint-enable no-console */
Object.defineProperty(window, 'fetch', {
value: fetch,
});
export {};