Repository navigation
Expand file tree
/
Copy pathjest.setup.js
More file actions
174 lines (160 loc) · 4.48 KB
/
Copy pathjest.setup.js
File metadata and controls
174 lines (160 loc) · 4.48 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
// jest.setup.js
import "@testing-library/jest-dom";
import { TextEncoder, TextDecoder } from "util";
// Polyfill TextEncoder/TextDecoder for @noble/curves and Solana libs
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
// Polyfill Request/Response/Headers for Next.js route handlers
// Required when testing Next.js API routes directly
if (typeof global.Request === "undefined") {
// Create minimal polyfills for Request/Response/Headers
class MockHeaders {
constructor(init = {}) {
this._headers = new Map();
if (init && typeof init === "object") {
Object.entries(init).forEach(([key, value]) => {
this._headers.set(key.toLowerCase(), value);
});
}
}
get(name) {
return this._headers.get(name.toLowerCase()) || null;
}
set(name, value) {
this._headers.set(name.toLowerCase(), value);
}
has(name) {
return this._headers.has(name.toLowerCase());
}
delete(name) {
this._headers.delete(name.toLowerCase());
}
forEach(callback) {
this._headers.forEach((value, key) => callback(value, key, this));
}
entries() {
return this._headers.entries();
}
}
class MockRequest {
constructor(input, init = {}) {
this.url = typeof input === "string" ? input : input.url;
this.method = init.method || "GET";
this.headers = new MockHeaders(init.headers);
this._body = init.body;
}
async json() {
if (typeof this._body === "string") {
return JSON.parse(this._body);
}
return this._body;
}
async text() {
return String(this._body || "");
}
}
class MockResponse {
constructor(body, init = {}) {
this._body = body;
this.status = init.status || 200;
this.statusText = init.statusText || "";
this.headers = new MockHeaders(init.headers);
this.ok = this.status >= 200 && this.status < 300;
}
async json() {
if (typeof this._body === "string") {
return JSON.parse(this._body);
}
return this._body;
}
async text() {
return String(this._body || "");
}
}
global.Request = MockRequest;
global.Response = MockResponse;
global.Headers = MockHeaders;
}
// Mock next/navigation
jest.mock("next/navigation", () => ({
useRouter: () => ({
push: jest.fn(),
replace: jest.fn(),
prefetch: jest.fn(),
back: jest.fn(),
}),
useSearchParams: () => new URLSearchParams(),
usePathname: () => "/",
}));
// Mock Solana wallet adapter
jest.mock("@solana/wallet-adapter-react", () => ({
useWallet: () => ({
publicKey: null,
connected: false,
connecting: false,
disconnect: jest.fn(),
connect: jest.fn(),
signTransaction: jest.fn(),
signAllTransactions: jest.fn(),
}),
useConnection: () => ({
connection: {
getBalance: jest.fn().mockResolvedValue(1000000000),
getLatestBlockhash: jest.fn().mockResolvedValue({
blockhash: "mock-blockhash",
lastValidBlockHeight: 100,
}),
},
}),
}));
// Mock Phaser (heavy library, not needed for unit tests)
jest.mock("phaser", () => ({
Game: jest.fn(),
Scene: jest.fn(),
AUTO: "AUTO",
}));
// Mock window.matchMedia
Object.defineProperty(window, "matchMedia", {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
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(),
};
Object.defineProperty(window, "localStorage", { value: localStorageMock });
// Polyfill Response.json static method for NextResponse.json() in API route tests
// jsdom provides Response but lacks the static .json() factory method
if (typeof Response !== "undefined" && typeof Response.json !== "function") {
Response.json = function (data, init = {}) {
const body = JSON.stringify(data);
return new Response(body, {
...init,
headers: {
"content-type": "application/json",
...(init.headers || {}),
},
});
};
}
// Mock fetch for API tests
global.fetch = jest.fn();
// Reset mocks between tests
beforeEach(() => {
jest.clearAllMocks();
localStorageMock.getItem.mockReturnValue(null);
});
// Suppress console errors in tests (optional - comment out for debugging)
// console.error = jest.fn();