-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathjest.setup.js
More file actions
91 lines (80 loc) · 2.14 KB
/
jest.setup.js
File metadata and controls
91 lines (80 loc) · 2.14 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import '@testing-library/jest-dom';
import 'whatwg-fetch';
// Mock IntersectionObserver
global.IntersectionObserver = class IntersectionObserver {
constructor() {}
disconnect() {}
observe() {}
unobserve() {}
};
// Mock ResizeObserver
global.ResizeObserver = class ResizeObserver {
constructor() {}
disconnect() {}
observe() {}
unobserve() {}
};
// Mock window-specific globals (only in browser/jsdom environment)
if (typeof window !== 'undefined') {
// Mock window.matchMedia
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
// Mock window.scrollTo
Object.defineProperty(window, 'scrollTo', {
writable: true,
value: jest.fn(),
});
// Mock sessionStorage
const localStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
};
Object.defineProperty(window, 'sessionStorage', {
value: localStorageMock,
});
Object.defineProperty(window, 'localStorage', {
value: localStorageMock,
});
// Mock window.open for OAuth testing
Object.defineProperty(window, 'open', {
writable: true,
value: jest.fn(() => ({
close: jest.fn(),
closed: false,
})),
});
}
// Mock TextEncoder and TextDecoder for Edge runtime compatibility
global.TextEncoder = class TextEncoder {
encode(string) {
return new Uint8Array(Buffer.from(string, 'utf8'));
}
};
global.TextDecoder = class TextDecoder {
decode(bytes, options = {}) {
return Buffer.from(bytes).toString('utf8');
}
};
// Reset all mocks before each test
beforeEach(() => {
jest.clearAllMocks();
if (typeof window !== 'undefined' && window.localStorage) {
window.localStorage.getItem.mockClear();
window.localStorage.setItem.mockClear();
window.localStorage.removeItem.mockClear();
window.localStorage.clear.mockClear();
}
});