-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvitest.setup.ts
74 lines (66 loc) · 2.43 KB
/
vitest.setup.ts
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
import '@testing-library/jest-dom'
import { vi } from 'vitest'
import React from 'react'
// Mock the framer-motion
vi.mock('framer-motion', () => ({
motion: {
div: ({ children, ...props }: React.PropsWithChildren<Record<string, unknown>>) =>
React.createElement('div', props, children),
},
AnimatePresence: ({ children }: React.PropsWithChildren) => children,
}))
// Mock any other dependencies as needed
vi.mock('@/lib/hooks/useTransformRes', () => ({
useTransformRes: () => (params: Record<string, unknown>) => params,
}))
// Create a helper for mocking fetch responses
interface MockFetchOptions {
status?: number
statusText?: string
headers?: Record<string, string>
}
// Helper function to create fetch responses
global.createFetchResponse = (data: unknown, options: MockFetchOptions = {}) => {
return {
ok: options.status ? options.status >= 200 && options.status < 300 : true,
status: options.status || 200,
statusText: options.statusText || 'OK',
headers: new Headers(options.headers || { 'Content-Type': 'application/json' }),
json: () => Promise.resolve(data),
text: () => Promise.resolve(JSON.stringify(data)),
redirected: false,
type: 'basic',
url: '',
clone: function () {
return this
},
body: null,
bodyUsed: false,
arrayBuffer: () => Promise.resolve(new ArrayBuffer(0)),
blob: () => Promise.resolve(new Blob([])),
formData: () => Promise.resolve(new FormData()),
} as Response
}
// Add global fetch mock helper
global.mockFetch = (response: unknown, options: MockFetchOptions = {}) => {
global.fetch = vi.fn().mockResolvedValue(createFetchResponse(response, options))
}
// Add global fetch error mock helper
global.mockFetchError = (errorMessage: string) => {
global.fetch = vi.fn().mockRejectedValue(new Error(errorMessage))
}
// Add global fetch network error mock helper
global.mockFetchNetworkError = () => {
global.fetch = vi.fn().mockRejectedValue(new TypeError('Network request failed'))
}
// Add type definitions for the global helpers
declare global {
// eslint-disable-next-line no-var
var createFetchResponse: (data: unknown, options?: MockFetchOptions) => Response
// eslint-disable-next-line no-var
var mockFetch: (response: unknown, options?: MockFetchOptions) => void
// eslint-disable-next-line no-var
var mockFetchError: (errorMessage: string) => void
// eslint-disable-next-line no-var
var mockFetchNetworkError: () => void
}