-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjest.setup.js
50 lines (42 loc) · 1.13 KB
/
jest.setup.js
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
// Import jest-dom additions
require('@testing-library/jest-dom');
// Polyfill setImmediate for the router package
global.setImmediate = (callback) => setTimeout(callback, 0);
// Mock location object
let currentPath = '/';
Object.defineProperty(window, 'location', {
value: {
...window.location,
get pathname() {
return currentPath;
},
set pathname(value) {
currentPath = value;
}
},
writable: true
});
// Mock browser globals that might not be in jsdom
global.window.history.pushState = jest.fn((state, title, url) => {
currentPath = url;
});
global.window.history.replaceState = jest.fn((state, title, url) => {
currentPath = url;
});
global.window.scrollTo = jest.fn();
// Reset mocks between tests
beforeEach(() => {
window.history.pushState.mockClear();
window.history.replaceState.mockClear();
window.scrollTo.mockClear();
// Reset the URL to / before each test
currentPath = '/';
// Reset document body
document.body.innerHTML = '';
// Reset any event listeners
window.removeEventListener = jest.fn();
});
// Clean up after each test
afterEach(() => {
jest.clearAllMocks();
});