/
react.spec.ts
89 lines (83 loc) · 2.59 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
import { expect, test } from 'vitest'
import {
browserLogs,
editFile,
isBuild,
isServe,
page,
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', 'Updated'))
await untilUpdated(() => page.textContent('h1'), 'Hello Updated')
// preserve state
expect(await page.textContent('#state-button')).toMatch('count is: 1')
})
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 () => {
browserLogs.length = 0
editFile('hmr/no-exported-comp.jsx', (code) =>
code.replace('An Object', 'Updated')
)
await untilUpdated(() => page.textContent('#parent'), 'Updated')
expect(browserLogs).toMatchObject([
'[vite] hot updated: /hmr/no-exported-comp.jsx',
'[vite] hot updated: /hmr/parent.jsx',
'Parent rendered'
])
browserLogs.length = 0
})
// #3301
test('should hmr react context', async () => {
browserLogs.length = 0
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'
)
editFile('context/CountProvider.jsx', (code) =>
code.replace('context provider', 'context provider updated')
)
await untilUpdated(
() => page.textContent('#context-provider'),
'context provider updated'
)
expect(browserLogs).toMatchObject([
'[vite] hot updated: /context/CountProvider.jsx',
'[vite] hot updated: /App.jsx',
'[vite] hot updated: /context/ContextButton.jsx',
'Parent rendered'
])
browserLogs.length = 0
})
}