-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjest.setup.js
More file actions
66 lines (58 loc) · 1.37 KB
/
Copy pathjest.setup.js
File metadata and controls
66 lines (58 loc) · 1.37 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
// Add any global setup for Jest tests here
// Mock fetch
global.fetch = jest.fn();
// Mock localStorage
const localStorageMock = (function() {
let store = {};
return {
getItem: jest.fn(key => store[key] || null),
setItem: jest.fn((key, value) => {
store[key] = value.toString();
}),
removeItem: jest.fn(key => {
delete store[key];
}),
clear: jest.fn(() => {
store = {};
}),
length: 0,
key: jest.fn(index => null)
};
})();
Object.defineProperty(window, 'localStorage', {
value: localStorageMock
});
// Mock IndexedDB
const indexedDBMock = {
open: jest.fn()
};
Object.defineProperty(window, 'indexedDB', {
value: indexedDBMock
});
// Mock window.URL.createObjectURL
Object.defineProperty(window.URL, 'createObjectURL', {
value: jest.fn()
});
// Mock window.URL.revokeObjectURL
Object.defineProperty(window.URL, 'revokeObjectURL', {
value: jest.fn()
});
// Add custom matchers
expect.extend({
toBeWithinRange(received, floor, ceiling) {
const pass = received >= floor && received <= ceiling;
if (pass) {
return {
message: () =>
`expected ${received} not to be within range ${floor} - ${ceiling}`,
pass: true,
};
} else {
return {
message: () =>
`expected ${received} to be within range ${floor} - ${ceiling}`,
pass: false,
};
}
},
});