-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
195 lines (175 loc) · 4.32 KB
/
jest.setup.js
File metadata and controls
195 lines (175 loc) · 4.32 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import '@testing-library/jest-dom'
import React from 'react'
// Mock IntersectionObserver
global.IntersectionObserver = class IntersectionObserver {
constructor() {}
observe() {
return null
}
disconnect() {
return null
}
unobserve() {
return null
}
}
// Mock ResizeObserver
global.ResizeObserver = class ResizeObserver {
constructor() {}
observe() {
return null
}
disconnect() {
return null
}
unobserve() {
return null
}
}
// Mock 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 localStorage
const localStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
}
global.localStorage = localStorageMock
// Mock next/navigation
jest.mock('next/navigation', () => ({
useRouter() {
return {
push: jest.fn(),
replace: jest.fn(),
prefetch: jest.fn(),
back: jest.fn(),
forward: jest.fn(),
refresh: jest.fn(),
}
},
useSearchParams() {
return new URLSearchParams()
},
usePathname() {
return '/'
},
}))
// Mock DOM methods
Element.prototype.scrollIntoView = jest.fn()
HTMLElement.prototype.scrollIntoView = jest.fn()
// Mock window.scrollTo
global.scrollTo = jest.fn()
// Mock Math.random and Math.floor for deterministic tests
global.Math.random = jest.fn(() => 0.5)
global.Math.floor = jest.fn((x) => x >= 0 ? Math.trunc(x) : Math.trunc(x) - 1)
// Mock framer-motion
jest.mock('framer-motion', () => {
const createMotionComponent = (tag) => ({ children, initial, animate, exit, transition, ...props }) => {
// Filter out framer-motion specific props
const filteredProps = Object.keys(props).reduce((acc, key) => {
if (!['initial', 'animate', 'exit', 'transition', 'variants', 'whileHover', 'whileTap'].includes(key)) {
acc[key] = props[key];
}
return acc;
}, {});
return React.createElement(tag, filteredProps, children);
};
return {
motion: {
div: createMotionComponent('div'),
button: createMotionComponent('button'),
span: createMotionComponent('span'),
p: createMotionComponent('p'),
h1: createMotionComponent('h1'),
h2: createMotionComponent('h2'),
h3: createMotionComponent('h3'),
section: createMotionComponent('section'),
article: createMotionComponent('article'),
aside: createMotionComponent('aside'),
nav: createMotionComponent('nav'),
},
AnimatePresence: ({ children }) => children,
useAnimation: () => ({
start: jest.fn(),
stop: jest.fn(),
set: jest.fn(),
}),
}
})
// Mock lucide-react icons
jest.mock('lucide-react', () => {
const MockIcon = ({ className, 'data-testid': testId, ...props }) =>
React.createElement('svg', {
className,
'data-testid': testId,
'aria-label': props['aria-label'] || 'icon',
...props
})
return {
ChevronLeft: MockIcon,
ChevronRight: MockIcon,
Home: MockIcon,
FolderOpen: MockIcon,
FileText: MockIcon,
MessageSquare: MockIcon,
Terminal: MockIcon,
GitBranch: MockIcon,
Search: MockIcon,
Plus: MockIcon,
Settings: MockIcon,
Send: MockIcon,
Square: MockIcon,
Copy: MockIcon,
MoreVertical: MockIcon,
User: MockIcon,
Bot: MockIcon,
Code: MockIcon,
Download: MockIcon,
Upload: MockIcon,
Trash2: MockIcon,
Edit: MockIcon,
Eye: MockIcon,
EyeOff: MockIcon,
File: MockIcon,
Folder: MockIcon,
Image: MockIcon,
Trash: MockIcon,
X: MockIcon,
Check: MockIcon,
AlertCircle: MockIcon,
Info: MockIcon,
CheckCircle: MockIcon,
XCircle: MockIcon,
Loader: MockIcon,
Menu: MockIcon,
}
})
// Suppress console warnings in tests
const originalError = console.error
beforeAll(() => {
console.error = (...args) => {
if (
typeof args[0] === 'string' &&
args[0].includes('Warning: ReactDOM.render is no longer supported')
) {
return
}
originalError.call(console, ...args)
}
})
afterAll(() => {
console.error = originalError
})