-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
react.spec.ts
118 lines (107 loc) · 3.67 KB
/
react.spec.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
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
import { expect, test } from 'vitest'
import {
editFile,
isBuild,
isServe,
page,
untilBrowserLogAfter,
untilUpdated,
} from '~utils'
test('should render', async () => {
expect(await page.textContent('h1')).toMatch('Hello Vite + React')
})
test('should update', async () => {
expect(await page.textContent('#state-button')).toMatch('count is: 0')
await page.click('#state-button')
expect(await page.textContent('#state-button')).toMatch('count is: 1')
})
test('should hmr', async () => {
editFile('App.jsx', (code) =>
code.replace('Vite + React', 'Vite + React Updated'),
)
await untilUpdated(() => page.textContent('h1'), 'Hello Vite + React Updated')
// preserve state
expect(await page.textContent('#state-button')).toMatch('count is: 1')
editFile('App.jsx', (code) =>
code.replace('Vite + React Updated', 'Vite + React'),
)
await untilUpdated(() => page.textContent('h1'), 'Hello Vite + React')
})
test.runIf(isServe)('should not invalidate when code is invalid', async () => {
editFile('App.jsx', (code) =>
code.replace('<div className="App">', '<div className="App"}>'),
)
await untilUpdated(
() => page.textContent('vite-error-overlay .message-body'),
'Unexpected token',
)
// if import.meta.invalidate happened, the old page won't be shown because the page is reloaded
expect(await page.textContent('h1')).toMatch('Hello Vite + React')
editFile('App.jsx', (code) =>
code.replace('<div className="App"}>', '<div className="App">'),
)
})
test.runIf(isServe)(
'should have annotated jsx with file location metadata',
async () => {
const meta = await page.evaluate(() => {
const button = document.querySelector('#state-button')
const key = Object.keys(button).find(
(key) => key.indexOf('__reactFiber') === 0,
)
return button[key]._debugSource
})
// If the evaluate call doesn't crash, and the returned metadata has
// the expected fields, we're good.
expect(Object.keys(meta).sort()).toEqual([
'columnNumber',
'fileName',
'lineNumber',
])
},
)
if (!isBuild) {
// #9869
test('should only hmr files with exported react components', async () => {
await untilBrowserLogAfter(
() =>
editFile('hmr/no-exported-comp.jsx', (code) =>
code.replace('An Object', 'Updated'),
),
[
'[vite] invalidate /hmr/no-exported-comp.jsx: Could not Fast Refresh. Learn more at https://github.com/vitejs/vite-plugin-react#consistent-components-exports',
'[vite] hot updated: /hmr/no-exported-comp.jsx',
'[vite] hot updated: /hmr/parent.jsx',
'Parent rendered',
],
)
await untilUpdated(() => page.textContent('#parent'), 'Updated')
})
// #3301
test('should hmr react context', async () => {
expect(await page.textContent('#context-button')).toMatch(
'context-based count is: 0',
)
await page.click('#context-button')
expect(await page.textContent('#context-button')).toMatch(
'context-based count is: 1',
)
await untilBrowserLogAfter(
() =>
editFile('context/CountProvider.jsx', (code) =>
code.replace('context provider', 'context provider updated'),
),
[
'[vite] invalidate /context/CountProvider.jsx: Could not Fast Refresh. Learn more at https://github.com/vitejs/vite-plugin-react#consistent-components-exports',
'[vite] hot updated: /context/CountProvider.jsx',
'[vite] hot updated: /App.jsx',
'[vite] hot updated: /context/ContextButton.jsx',
'Parent rendered',
],
)
await untilUpdated(
() => page.textContent('#context-provider'),
'context provider updated',
)
})
}