-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
108 lines (96 loc) · 2.78 KB
/
vitest.setup.ts
File metadata and controls
108 lines (96 loc) · 2.78 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
import '@testing-library/jest-dom'
import { cleanup } from '@testing-library/react'
import i18n from 'i18next'
import type { ReactNode } from 'react'
import { initReactI18next } from 'react-i18next'
import { afterEach, vi } from 'vitest'
function createToastMock() {
const toastFn = ((options?: { id?: string }) => ({ id: options?.id ?? 'toast' })) as any
toastFn.close = vi.fn()
toastFn.closeAll = vi.fn()
toastFn.update = vi.fn()
toastFn.isActive = vi.fn(() => false)
return {
ToastProvider: ({ children }: { children: ReactNode }) => children as any,
useToast: () => toastFn,
}
}
vi.mock('~components/Toast', createToastMock)
vi.mock('@vocdoni/react-providers', async (importOriginal) => {
const actual = await importOriginal<typeof import('@vocdoni/react-providers')>()
const { getReactProvidersMock } = await import('./src/test-utils-react-providers-mock')
return {
...actual,
...getReactProvidersMock(),
}
})
if (!i18n.isInitialized) {
i18n.use(initReactI18next).init({
lng: 'en',
fallbackLng: 'en',
defaultNS: 'common',
showSupportNotice: false,
interpolation: { escapeValue: false },
resources: { en: { common: {} } },
})
}
// Cleanup after each test
afterEach(async () => {
cleanup()
const { resetReactProvidersMock } = await import('./src/test-utils-react-providers-mock')
resetReactProvidersMock()
})
// Mock environment variables
process.env.SAAS_URL = 'https://test-api.example.com'
// Mock window.matchMedia for Chakra UI
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: (query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: () => {}, // deprecated
removeListener: () => {}, // deprecated
addEventListener: () => {},
removeEventListener: () => {},
dispatchEvent: () => true,
}),
})
const localStorageStore = new Map<string, string>()
const localStorageMock = {
getItem: vi.fn((key: string) => {
return localStorageStore.has(key) ? localStorageStore.get(key)! : null
}),
setItem: vi.fn((key: string, value: string) => {
localStorageStore.set(key, String(value))
}),
removeItem: vi.fn((key: string) => {
localStorageStore.delete(key)
}),
clear: vi.fn(() => {
localStorageStore.clear()
}),
key: vi.fn((index: number) => {
return Array.from(localStorageStore.keys())[index] ?? null
}),
get length() {
return localStorageStore.size
},
}
Object.defineProperty(window, 'localStorage', {
value: localStorageMock,
writable: true,
})
Object.defineProperty(globalThis, 'localStorage', {
value: localStorageMock,
writable: true,
})
class ResizeObserverMock {
observe() {}
unobserve() {}
disconnect() {}
}
Object.defineProperty(globalThis, 'ResizeObserver', {
value: ResizeObserverMock,
writable: true,
})