-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbun-test-setup.ts
More file actions
71 lines (67 loc) · 1.96 KB
/
Copy pathbun-test-setup.ts
File metadata and controls
71 lines (67 loc) · 1.96 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
import { mock } from 'bun:test';
import { GlobalWindow } from 'happy-dom';
const win = new GlobalWindow();
global.window = win as any;
global.document = win.document as any;
global.navigator = win.navigator as any;
global.localStorage = win.localStorage as any;
global.addEventListener = win.addEventListener.bind(win) as any;
global.removeEventListener = win.removeEventListener.bind(win) as any;
global.dispatchEvent = win.dispatchEvent.bind(win) as any;
global.StorageEvent = win.StorageEvent as any;
// Mock dependencies that cause side effects or fail on static assets imports
mock.module('@/services/api', () => ({
api: {},
}));
class MockRequestError extends Error {
status?: number;
constructor(message: string, status?: number) {
super(message);
this.name = 'RequestError';
this.status = status;
}
}
const mockSetToken = mock(() => {});
const mockMarkCookieSession = mock(() => {});
const mockClearSession = mock(() => {});
mock.module('@/services/request', () => ({
getToken: () => '',
hasSession: () => false,
usesCookieSession: () => false,
setToken: mockSetToken,
markCookieSession: mockMarkCookieSession,
clearSession: mockClearSession,
RequestError: MockRequestError,
default: {},
}));
mock.module('@/assets/logo-h.svg', () => ({
default: 'logo',
ReactComponent: () => null,
}));
mock.module('@/assets/logo.png', () => ({
default: 'logo.png',
}));
mock.module('react-router-dom', () => ({
createHashRouter: () => ({
state: { location: { search: '', pathname: '' } },
navigate: () => {},
}),
redirect: () => {},
useNavigate: () => {},
useRouteError: () => {},
isRouteErrorResponse: () => false,
Link: () => null,
NavLink: () => null,
Navigate: () => null,
Outlet: () => null,
useLocation: () => ({
pathname: '',
search: '',
hash: '',
state: null,
key: 'default',
}),
useSearchParams: () => [new URLSearchParams(), () => {}],
useLoaderData: () => null,
useActionData: () => null,
}));