Repository navigation
Expand file tree
/
Copy pathjest.setup.js
More file actions
175 lines (162 loc) · 5.24 KB
/
Copy pathjest.setup.js
File metadata and controls
175 lines (162 loc) · 5.24 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
/* eslint-disable @typescript-eslint/no-require-imports -- jest env setup */
import '@testing-library/jest-dom';
import React from 'react';
import { queryClient } from './src/app/queries/queryClient';
// Polyfill TextEncoder/TextDecoder for libraries (e.g. viem) in the jsdom env.
// Node provides these; jsdom's global scope may not expose them to bundled code.
if (typeof globalThis.TextEncoder === 'undefined') {
const { TextEncoder, TextDecoder } = require('node:util')
globalThis.TextEncoder = TextEncoder
globalThis.TextDecoder = TextDecoder
}
if (typeof global.Request === 'undefined' && typeof globalThis.Request !== 'undefined') {
global.Request = globalThis.Request;
global.Response = globalThis.Response;
global.Headers = globalThis.Headers;
}
// Mock WebSocket for testing
global.WebSocket = jest.fn(() => ({
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
send: jest.fn(),
close: jest.fn(),
readyState: 1, // OPEN
}));
// Mock fetch globally
global.fetch = jest.fn();
// Mock localStorage with in-memory behavior so tests can observe persisted state.
const storage = new Map();
const localStorageMock = {
getItem: jest.fn((key) => (storage.has(key) ? storage.get(key) : null)),
setItem: jest.fn((key, value) => {
storage.set(String(key), String(value));
}),
removeItem: jest.fn((key) => {
storage.delete(String(key));
}),
clear: jest.fn(() => {
storage.clear();
}),
};
global.localStorage = localStorageMock;
// Mock window.matchMedia
if (typeof window !== 'undefined') {
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 next/navigation
jest.mock('next/navigation', () => ({
useRouter: () => ({
push: jest.fn(),
replace: jest.fn(),
prefetch: jest.fn(),
back: jest.fn(),
forward: jest.fn(),
refresh: jest.fn(),
}),
usePathname: () => '/',
useSearchParams: () => new URLSearchParams(),
useParams: () => ({}),
notFound: jest.fn(),
}));
// Mock RainbowKit
jest.mock('@rainbow-me/rainbowkit', () => ({
getDefaultConfig: jest.fn(() => ({
chains: [
{ id: 10, name: 'OP Mainnet' },
{ id: 11155420, name: 'OP Sepolia' },
],
transports: {},
})),
RainbowKitProvider: ({ children }) => children,
darkTheme: jest.fn(() => ({})),
lightTheme: jest.fn(() => ({})),
ConnectButton: Object.assign(
({ label = 'Connect Wallet' }) => <button type="button">{label}</button>,
{
Custom: ({ children }) =>
children({
account: undefined,
chain: undefined,
openAccountModal: jest.fn(),
openChainModal: jest.fn(),
openConnectModal: jest.fn(),
authenticationStatus: undefined,
mounted: true,
}),
}
),
useConnectModal: () => ({
openConnectModal: jest.fn(),
}),
}));
import { TextEncoder, TextDecoder } from 'util';
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
// Mock Wagmi
jest.mock('wagmi', () => ({
useAccount: () => ({
address: '0x742d35Cc6634C0532925a3b844Bc9e7595f0eB1E',
isConnected: true,
isConnecting: false,
isDisconnected: false,
chainId: 10,
status: 'connected',
}),
useDisconnect: () => ({
disconnect: jest.fn(),
disconnectAsync: jest.fn().mockResolvedValue(undefined),
}),
useChainId: () => 10,
useSwitchChain: () => ({
switchChain: jest.fn(),
}),
usePublicClient: () => ({}),
useWalletClient: () => ({}),
useBlockNumber: jest.fn(() => ({ data: 100n })),
useReadContract: jest.fn(() => ({ data: undefined, isLoading: false })),
useWriteContract: jest.fn(() => ({ writeContractAsync: jest.fn().mockResolvedValue('0x' + '1'.repeat(64)) })),
useWaitForTransactionReceipt: jest.fn(() => ({ data: null, isLoading: false })),
useBalance: jest.fn(() => ({ data: { value: 1000000000000000000n, formatted: '1.0' }, isLoading: false })),
useConnectors: () => [{ id: 'injected', name: 'Injected', type: 'injected' }],
useConnect: () => ({ connect: jest.fn(), connectAsync: jest.fn().mockResolvedValue(undefined) }),
WagmiProvider: ({ children }) => children,
createStorage: jest.fn(() => ({})),
cookieStorage: {},
http: jest.fn(),
}));
jest.mock('wagmi/chains', () => ({
optimism: {
id: 10,
name: 'OP Mainnet',
nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
rpcUrls: { default: { http: ['https://mainnet.optimism.io'] } },
blockExplorers: { default: { name: 'Etherscan', url: 'https://optimistic.etherscan.io' } },
},
optimismSepolia: {
id: 11155420,
name: 'OP Sepolia',
nativeCurrency: { name: 'Sepolia Ether', symbol: 'ETH', decimals: 18 },
rpcUrls: { default: { http: ['https://sepolia.optimism.io'] } },
blockExplorers: { default: { name: 'Etherscan', url: 'https://sepolia-optimism.etherscan.io' } },
},
}));
// Increase timeout for integration test suites
jest.setTimeout(25000);
// Clean up after each test
afterEach(() => {
jest.clearAllMocks();
storage.clear();
queryClient.clear();
});