-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathjest.setup.js
More file actions
150 lines (139 loc) · 4.55 KB
/
Copy pathjest.setup.js
File metadata and controls
150 lines (139 loc) · 4.55 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
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
}
// 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
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 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();
});