forked from Invoice-Liquidity-Network/ILN-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
103 lines (95 loc) · 2.5 KB
/
Copy pathvitest.setup.ts
File metadata and controls
103 lines (95 loc) · 2.5 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
import { vi } from 'vitest';
import '@testing-library/jest-dom';
// Mock matchMedia for testing components that use prefers-reduced-motion
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
// Mock react-query
vi.mock('@tanstack/react-query', () => ({
useQuery: vi.fn(() => ({
data: [],
isLoading: false,
error: null,
dataUpdatedAt: Date.now(),
refetch: vi.fn(),
})),
useMutation: vi.fn(() => ({
mutate: vi.fn(),
isPending: false,
})),
useQueryClient: vi.fn(() => ({
invalidateQueries: vi.fn(),
cancelQueries: vi.fn(),
setQueryData: vi.fn(),
getQueryData: vi.fn(),
})),
QueryClient: vi.fn(),
QueryClientProvider: ({ children }: { children: React.ReactNode }) => children,
}));
// Mock next/navigation
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(() => ({})),
}));
// Mock localStorage
const localStorageMock = (() => {
let store: Record<string, string> = {};
return {
getItem: (key: string) => store[key] || null,
setItem: (key: string, value: string) => {
store[key] = value.toString();
},
clear: () => {
store = {};
},
removeItem: (key: string) => {
delete store[key];
},
get length() {
return Object.keys(store).length;
},
key: (index: number) => Object.keys(store)[index] || null,
};
})();
Object.defineProperty(window, 'localStorage', { value: localStorageMock });
Object.defineProperty(global, 'localStorage', { value: localStorageMock });
// Mock react 'use' hook for Next.js params
vi.mock('react', async () => {
const actual = await vi.importActual('react') as any;
return {
...actual,
use: vi.fn((input) => {
if (input && typeof input.then === 'function') {
if (input._resolvedValue) return input._resolvedValue;
return input;
}
return input;
}),
};
});
// Initialize i18n
import './src/i18n';
// Mock global fetch
global.fetch = vi.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve([]),
} as Response)
);