-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupTests.ts
More file actions
161 lines (150 loc) · 4.13 KB
/
Copy pathsetupTests.ts
File metadata and controls
161 lines (150 loc) · 4.13 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
149
150
151
152
153
154
155
156
157
158
159
160
161
// setupTests.ts
import '@testing-library/jest-dom'
import { vi } from 'vitest'
// jsdom does not implement the Web Animations API; stub it so Svelte 5
// transitions don't crash in tests.
Element.prototype.animate = vi.fn(() => ({
onfinish: null,
cancel: vi.fn(),
finish: vi.fn(),
}))
// jsdom does not lay out elements, so clientWidth/clientHeight stay 0 and ECharts
// logs "[ECharts] Can't get DOM width or height" when chart components mount.
const CHART_DOM_WIDTH = 400
const CHART_DOM_HEIGHT = 300
for (const dimension of ['clientWidth', 'clientHeight', 'offsetWidth', 'offsetHeight'] as const) {
const value = dimension.includes('Width') ? CHART_DOM_WIDTH : CHART_DOM_HEIGHT
Object.defineProperty(HTMLElement.prototype, dimension, {
configurable: true,
get() {
return value
},
})
}
HTMLElement.prototype.getBoundingClientRect = vi.fn(function getBoundingClientRect(
this: HTMLElement,
) {
return {
width: CHART_DOM_WIDTH,
height: CHART_DOM_HEIGHT,
top: 0,
left: 0,
bottom: CHART_DOM_HEIGHT,
right: CHART_DOM_WIDTH,
x: 0,
y: 0,
toJSON: () => ({}),
}
})
// jsdom's HTMLCanvasElement.getContext returns null; stub a minimal 2d context
// so ECharts (zrender) can initialise without throwing during tests.
HTMLCanvasElement.prototype.getContext = vi.fn(() => ({
clearRect: vi.fn(),
fillRect: vi.fn(),
strokeRect: vi.fn(),
beginPath: vi.fn(),
closePath: vi.fn(),
moveTo: vi.fn(),
lineTo: vi.fn(),
arc: vi.fn(),
fill: vi.fn(),
stroke: vi.fn(),
clip: vi.fn(),
save: vi.fn(),
restore: vi.fn(),
translate: vi.fn(),
scale: vi.fn(),
rotate: vi.fn(),
transform: vi.fn(),
setTransform: vi.fn(),
drawImage: vi.fn(),
createLinearGradient: vi.fn(() => ({ addColorStop: vi.fn() })),
createRadialGradient: vi.fn(() => ({ addColorStop: vi.fn() })),
measureText: vi.fn(() => ({ width: 0 })),
fillText: vi.fn(),
strokeText: vi.fn(),
putImageData: vi.fn(),
getImageData: vi.fn(() => ({ data: [] })),
createPattern: vi.fn(),
createImageData: vi.fn(),
setLineDash: vi.fn(),
getLineDash: vi.fn(() => []),
quadraticCurveTo: vi.fn(),
bezierCurveTo: vi.fn(),
arcTo: vi.fn(),
rect: vi.fn(),
canvas: { width: 300, height: 150 },
}))
import type { Navigation, Page } from '@sveltejs/kit'
import { readable } from 'svelte/store'
import * as environment from '$app/environment'
import * as navigation from '$app/navigation'
import * as stores from '$app/stores'
vi.mock('$lib/stores/media', () => ({
media: readable({
sm: true,
md: true,
lg: true,
xl: true,
'2xl': true,
}),
}))
// Mock SvelteKit runtime module $app/environment
vi.mock('$app/environment', (): typeof environment => ({
browser: false,
dev: true,
building: false,
version: 'any',
}))
// Mock SvelteKit runtime module $app/navigation
vi.mock('$app/navigation', (): typeof navigation => ({
afterNavigate: () => vi.fn(),
beforeNavigate: () => vi.fn(),
disableScrollHandling: () => vi.fn(),
goto: () => Promise.resolve(),
invalidate: () => Promise.resolve(),
invalidateAll: () => Promise.resolve(),
preloadData: () => Promise.resolve(),
preloadCode: () => Promise.resolve(),
}))
// Mock SvelteKit runtime module $app/stores
vi.mock('$app/stores', (): typeof stores => {
const getStores: typeof stores.getStores = () => {
const navigating = readable<Navigation | null>(null)
const page = readable<Page>({
url: new URL('http://localhost'),
params: {},
route: {
id: null,
},
status: 200,
error: null,
data: {},
form: undefined,
})
const updated = { subscribe: readable(false).subscribe, check: async () => false }
return { navigating, page, updated }
}
const page: typeof stores.page = {
subscribe(fn) {
return getStores().page.subscribe(fn)
},
}
const navigating: typeof stores.navigating = {
subscribe(fn) {
return getStores().navigating.subscribe(fn)
},
}
const updated: typeof stores.updated = {
subscribe(fn) {
return getStores().updated.subscribe(fn)
},
check: async () => false,
}
return {
getStores,
navigating,
page,
updated,
}
})