Repository navigation
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
97 lines (84 loc) · 3.26 KB
/
Copy pathvitest.setup.ts
File metadata and controls
97 lines (84 loc) · 3.26 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
import '@testing-library/jest-dom';
import { vi, beforeEach, afterEach } from 'vitest';
import 'fake-indexeddb/auto';
(globalThis as any).jest = vi as any;
vi.mock('next/navigation', () => ({
useRouter: vi.fn(() => ({ push: vi.fn(), replace: vi.fn(), prefetch: vi.fn(), back: vi.fn() })),
usePathname: vi.fn(() => '/'),
useSearchParams: vi.fn(() => new URLSearchParams()),
useParams: vi.fn(() => ({})),
redirect: vi.fn(),
}));
vi.mock('next/link', () => ({
default: ({ children, href, className, ...rest }: any) => {
const React = require('react');
return React.createElement('a', { href, className, ...rest }, children);
},
}));
vi.mock('@stellar/freighter-api', () => ({
getPublicKey: vi.fn().mockResolvedValue('GTEST_PUBLIC_KEY'),
signTransaction: vi.fn().mockResolvedValue('signed_xdr'),
signBlob: vi.fn().mockResolvedValue({ signedBlob: '' }),
signAuthEntry: vi.fn().mockResolvedValue({ signedAuthEntry: '' }),
isConnected: vi.fn().mockResolvedValue({ isConnected: false }),
isAllowed: vi.fn().mockResolvedValue({ isAllowed: false }),
setAllowed: vi.fn().mockResolvedValue({ isAllowed: false }),
getNetwork: vi.fn().mockResolvedValue({ networkUrl: '', network: 'TESTNET' }),
getNetworkDetails: vi.fn().mockResolvedValue({
network: 'TESTNET',
networkUrl: '',
networkPassphrase: 'Test SDF Network ; September 2015',
sorobanRpcUrl: '',
}),
WatchWalletChanges: vi.fn().mockReturnValue({ unsubscribe: vi.fn() }),
}));
// ---------------------------------------------------------------------------
// Reliability feature test setup (error handling, retry, offline queue)
// ---------------------------------------------------------------------------
// Track online status so tests can simulate connectivity changes.
let onlineStatus = true;
Object.defineProperty(globalThis.navigator, 'onLine', {
configurable: true,
get: () => onlineStatus,
});
/**
* Simulate a connectivity change. Dispatches the appropriate window event so
* listeners registered by the offline queue / connectivity hooks fire.
*/
export function setOnlineStatus(online: boolean): void {
onlineStatus = online;
const event = new Event(online ? 'online' : 'offline');
globalThis.window?.dispatchEvent(event);
}
(globalThis as any).setOnlineStatus = setOnlineStatus;
// Mock fetch so tests never hit the network. Individual tests can override
// this with vi.mocked(fetch).mockResolvedValueOnce(...) etc.
const fetchMock = vi.fn(async () =>
new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
);
vi.stubGlobal('fetch', fetchMock);
// Reset retry metrics between tests if the metrics module is present.
async function resetRetryMetrics(): Promise<void> {
try {
const mod: any = await import('./lib/reliability/retryMetrics');
if (mod && typeof mod.resetRetryMetrics === 'function') {
mod.resetRetryMetrics();
} else if (mod && mod.retryMetrics && typeof mod.retryMetrics.reset === 'function') {
mod.retryMetrics.reset();
}
} catch {
// Metrics module not present in this test context; nothing to reset.
}
}
beforeEach(async () => {
onlineStatus = true;
fetchMock.mockClear();
await resetRetryMetrics();
});
afterEach(() => {
onlineStatus = true;
vi.clearAllMocks();
});