-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
81 lines (73 loc) · 1.78 KB
/
Copy pathjest.setup.js
File metadata and controls
81 lines (73 loc) · 1.78 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
// Jest setup file
import '@testing-library/jest-dom';
import { enableFetchMocks } from 'jest-fetch-mock';
// Polyfill for TextEncoder/TextDecoder
const { TextEncoder, TextDecoder } = require('util');
// Mock globals
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
// Mock window and document
global.window = {};
global.document = {
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
createElement: jest.fn(() => ({
setAttribute: jest.fn(),
classList: {
add: jest.fn(),
remove: jest.fn(),
toggle: jest.fn(),
contains: jest.fn(),
},
appendChild: jest.fn(),
removeChild: jest.fn(),
style: {},
})),
getElementById: jest.fn(() => ({
classList: {
add: jest.fn(),
remove: jest.fn(),
toggle: jest.fn(),
},
style: {},
appendChild: jest.fn(),
removeChild: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
})),
querySelector: jest.fn(() => null),
querySelectorAll: jest.fn(() => []),
createTextNode: jest.fn((text) => ({
nodeValue: text,
})),
};
// Mock fetch
global.fetch = jest.fn();
// Mock localStorage
const localStorageMock = (() => {
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 = {};
}),
};
})();
Object.defineProperty(window, 'localStorage', {
value: localStorageMock,
});
// Mock global fetch
global.fetch = jest.fn();
// Mock window.location
const mockLocation = new URL('http://localhost/');
delete window.location;
window.location = mockLocation;
Object.defineProperty(window, 'open', {
value: jest.fn(),
});