This repository was archived by the owner on Mar 17, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjest.setup.js
More file actions
216 lines (195 loc) · 4.82 KB
/
Copy pathjest.setup.js
File metadata and controls
216 lines (195 loc) · 4.82 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Setup file for Jest tests
import "@testing-library/jest-dom";
import React from "react";
// Jest setup file
import "@testing-library/jest-dom";
// Mock fetch for tests
global.fetch = jest.fn();
// Mock Web APIs for Next.js API routes
import { TextEncoder, TextDecoder } from "util";
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
// Mock NextResponse properly
jest.mock("next/server", () => ({
NextRequest: class MockNextRequest {
constructor(url, options = {}) {
this.url = url;
this.method = options.method || "GET";
this.headers = new Map(Object.entries(options.headers || {}));
this._body = options.body;
}
async json() {
return JSON.parse(this._body || "{}");
}
async text() {
return this._body || "";
}
},
NextResponse: {
json: (data, options = {}) => ({
json: async () => data,
status: options.status || 200,
headers: new Map(),
}),
},
}));
// Mock URL for parsing search params
global.URL = class MockURL {
constructor(url) {
this.href = url;
const [, search] = url.split("?");
this.searchParams = {
get: (key) => {
if (!search) return null;
const params = new URLSearchParams(search);
return params.get(key);
},
};
}
};
// 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 IntersectionObserver
global.IntersectionObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
}));
// Mock ResizeObserver
global.ResizeObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
}));
// Mock performance.now for tests
global.performance = {
...global.performance,
now: jest.fn(() => Date.now()),
};
// Mock next/headers
jest.mock("next/headers", () => ({
cookies: jest.fn(() => ({
get: jest.fn(),
set: jest.fn(),
delete: jest.fn(),
})),
headers: jest.fn(() => ({
get: jest.fn(),
set: jest.fn(),
delete: jest.fn(),
})),
}));
// Mock framer-motion
jest.mock("framer-motion", () => ({
motion: {
div: ({ children, ...props }) => <div {...props}>{children}</div>,
span: ({ children, ...props }) => <span {...props}>{children}</span>,
button: ({ children, ...props }) => <button {...props}>{children}</button>,
},
AnimatePresence: ({ children }) => children,
}));
// Mock Prisma
jest.mock("@/lib/prisma", () => ({
__esModule: true,
default: {
tasks: {
findMany: jest.fn(),
findUnique: jest.fn(),
create: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
},
userSettings: {
findUnique: jest.fn(),
create: jest.fn(),
update: jest.fn(),
},
},
}));
// Mock Supabase
jest.mock("@supabase/ssr", () => ({
createServerClient: jest.fn(),
}));
// Mock next/navigation
jest.mock("next/navigation", () => ({
useRouter: jest.fn(() => ({
push: jest.fn(),
replace: jest.fn(),
back: jest.fn(),
forward: jest.fn(),
refresh: jest.fn(),
prefetch: jest.fn(),
})),
useSearchParams: jest.fn(() => ({
get: jest.fn(),
set: jest.fn(),
delete: jest.fn(),
})),
usePathname: jest.fn(() => "/"),
}));
// Mock next/headers
jest.mock("next/headers", () => ({
cookies: () => ({
get: jest.fn(() => ({ value: "mock-cookie" })),
set: jest.fn(),
}),
}));
// Mock Supabase
jest.mock("@supabase/ssr", () => ({
createServerClient: jest.fn(() => ({
auth: {
getSession: jest.fn(() =>
Promise.resolve({
data: {
session: {
user: { id: "test-user-id" },
},
},
})
),
},
})),
}));
// Mock Prisma
jest.mock("@/lib/prisma", () => ({
__esModule: true,
default: {
tasks: {
findMany: jest.fn(),
create: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
},
userSettings: {
findUnique: jest.fn(),
create: jest.fn(),
update: jest.fn(),
},
},
}));
// Mock Framer Motion
jest.mock("framer-motion", () => ({
motion: {
div: ({ children, ...props }) =>
React.createElement("div", props, children),
button: ({ children, ...props }) =>
React.createElement("button", props, children),
},
AnimatePresence: ({ children }) => children,
}));
// Mock environment variables
process.env.NEXT_PUBLIC_SUPABASE_URL = "http://localhost:54321";
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY = "test-anon-key";
process.env.DATABASE_URL = "postgresql://test:test@localhost:5432/test";