-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
148 lines (138 loc) · 4.54 KB
/
vitest.setup.ts
File metadata and controls
148 lines (138 loc) · 4.54 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import '@testing-library/jest-dom/vitest'
import {cleanup} from '@testing-library/react'
import {afterAll, afterEach, beforeAll, vi} from 'vitest'
import {mockWeatherResponse} from './test-utils/mocks/mockData'
import {server} from './test-utils/msw/server'
// Helper functions to reduce nesting in openmeteo mock
const createCurrentVariables = (index: number) => {
const values = [
mockWeatherResponse.current.temperature_2m,
mockWeatherResponse.current.relative_humidity_2m,
mockWeatherResponse.current.apparent_temperature,
mockWeatherResponse.current.precipitation,
mockWeatherResponse.current.weather_code,
mockWeatherResponse.current.wind_speed_10m,
mockWeatherResponse.current.wind_direction_10m,
mockWeatherResponse.current.wind_gusts_10m,
mockWeatherResponse.current.uv_index,
mockWeatherResponse.current.visibility,
mockWeatherResponse.current.pressure_msl,
mockWeatherResponse.current.dew_point_2m,
mockWeatherResponse.current.cloud_cover,
mockWeatherResponse.current.rain,
mockWeatherResponse.current.showers,
mockWeatherResponse.current.snowfall,
mockWeatherResponse.current.snow_depth
]
return {value: () => values[index]}
}
const createHourlyVariables = (index: number) => {
const arrays = [
mockWeatherResponse.hourly.temperature_2m,
mockWeatherResponse.hourly.apparent_temperature,
mockWeatherResponse.hourly.precipitation_probability,
mockWeatherResponse.hourly.weather_code,
mockWeatherResponse.hourly.wind_speed_10m
]
return {valuesArray: () => new Float32Array(arrays[index])}
}
const createDailyVariables = (index: number) => {
const sunriseTimestamps = mockWeatherResponse.daily.sunrise.map(
(t) => new Date(t).getTime() / 1000
)
const sunsetTimestamps = mockWeatherResponse.daily.sunset.map(
(t) => new Date(t).getTime() / 1000
)
const arrays = [
mockWeatherResponse.daily.weather_code,
mockWeatherResponse.daily.temperature_2m_max,
mockWeatherResponse.daily.temperature_2m_min,
mockWeatherResponse.daily.apparent_temperature_max,
mockWeatherResponse.daily.apparent_temperature_min,
mockWeatherResponse.daily.precipitation_probability_max,
sunriseTimestamps,
sunsetTimestamps,
mockWeatherResponse.daily.uv_index_max
]
return {valuesArray: () => new Float32Array(arrays[index])}
}
// Mock openmeteo SDK
vi.mock('openmeteo', () => ({
fetchWeatherApi: vi.fn(async () => {
// Return mock data in the expected SDK format
return [
{
latitude: () => mockWeatherResponse.latitude,
longitude: () => mockWeatherResponse.longitude,
timezone: () => mockWeatherResponse.timezone,
utcOffsetSeconds: () => 0,
current: () => ({
time: () =>
new Date(mockWeatherResponse.current.time).getTime() / 1000,
variables: createCurrentVariables
}),
hourly: () => ({
time: () =>
new Date(mockWeatherResponse.hourly.time[0]).getTime() / 1000,
timeEnd: () =>
new Date(mockWeatherResponse.hourly.time.at(-1)!).getTime() / 1000,
interval: () => 3600,
variables: createHourlyVariables
}),
daily: () => ({
time: () =>
new Date(mockWeatherResponse.daily.time[0]).getTime() / 1000,
timeEnd: () =>
new Date(mockWeatherResponse.daily.time.at(-1)!).getTime() / 1000,
interval: () => 86400,
variables: createDailyVariables
})
}
]
})
}))
// Establish API mocking before all tests (for placesApi)
beforeAll(() => server.listen({onUnhandledRequest: 'warn'}))
// Reset any request handlers that we may add during tests
afterEach(() => {
server.resetHandlers()
cleanup()
})
// Clean up after tests are finished
afterAll(() => server.close())
// Mock Next.js router
vi.mock('next/navigation', () => ({
useRouter() {
return {
push: vi.fn(),
replace: vi.fn(),
prefetch: vi.fn()
}
},
usePathname() {
return ''
},
useSearchParams() {
return new URLSearchParams()
}
}))
// Mock window.matchMedia
Object.defineProperty(globalThis, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn()
}))
})
// Mock ResizeObserver (required by Mantine)
globalThis.ResizeObserver = class ResizeObserver {
observe = vi.fn()
unobserve = vi.fn()
disconnect = vi.fn()
}