Skip to content

Commit 37dbbd0

Browse files
committed
test: add test for react context hmr
1 parent 4fae6a8 commit 37dbbd0

File tree

4 files changed

+71
-7
lines changed

4 files changed

+71
-7
lines changed

playground/react/App.jsx

+18-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { useState } from 'react'
22
import Button from 'jsx-entry'
33
import Dummy from './components/Dummy?qs-should-not-break-plugin-react'
44
import Parent from './hmr/parent'
5+
import { CountProvider } from './context/CountProvider'
6+
import { ContextButton } from './context/ContextButton'
57

68
function App() {
79
const [count, setCount] = useState(0)
@@ -10,10 +12,16 @@ function App() {
1012
<header className="App-header">
1113
<h1>Hello Vite + React</h1>
1214
<p>
13-
<button onClick={() => setCount((count) => count + 1)}>
15+
<button
16+
id="state-button"
17+
onClick={() => setCount((count) => count + 1)}
18+
>
1419
count is: {count}
1520
</button>
1621
</p>
22+
<p>
23+
<ContextButton />
24+
</p>
1725
<p>
1826
Edit <code>App.jsx</code> and save to test HMR updates.
1927
</p>
@@ -34,4 +42,12 @@ function App() {
3442
)
3543
}
3644

37-
export default App
45+
function AppWithProviders() {
46+
return (
47+
<CountProvider>
48+
<App />
49+
</CountProvider>
50+
)
51+
}
52+
53+
export default AppWithProviders

playground/react/__tests__/react.spec.ts

+30-5
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ test('should render', async () => {
66
})
77

88
test('should update', async () => {
9-
expect(await page.textContent('button')).toMatch('count is: 0')
10-
await page.click('button')
11-
expect(await page.textContent('button')).toMatch('count is: 1')
9+
expect(await page.textContent('#state-button')).toMatch('count is: 0')
10+
await page.click('#state-button')
11+
expect(await page.textContent('#state-button')).toMatch('count is: 1')
1212
})
1313

1414
test('should hmr', async () => {
1515
editFile('App.jsx', (code) => code.replace('Vite + React', 'Updated'))
1616
await untilUpdated(() => page.textContent('h1'), 'Hello Updated')
1717
// preserve state
18-
expect(await page.textContent('button')).toMatch('count is: 1')
18+
expect(await page.textContent('#state-button')).toMatch('count is: 1')
1919
})
2020

2121
// #9869
@@ -37,7 +37,7 @@ test.runIf(isServe)(
3737
'should have annotated jsx with file location metadata',
3838
async () => {
3939
const meta = await page.evaluate(() => {
40-
const button = document.querySelector('button')
40+
const button = document.querySelector('#state-button')
4141
const key = Object.keys(button).find(
4242
(key) => key.indexOf('__reactFiber') === 0
4343
)
@@ -52,3 +52,28 @@ test.runIf(isServe)(
5252
])
5353
}
5454
)
55+
56+
test('should hmr react context', async () => {
57+
browserLogs.length = 0
58+
expect(await page.textContent('#context-button')).toMatch(
59+
'context-based count is: 0'
60+
)
61+
await page.click('#context-button')
62+
expect(await page.textContent('#context-button')).toMatch(
63+
'context-based count is: 1'
64+
)
65+
editFile('context/CountProvider.jsx', (code) =>
66+
code.replace('context provider', 'context provider updated')
67+
)
68+
await untilUpdated(
69+
() => page.textContent('#context-provider'),
70+
'context provider updated'
71+
)
72+
expect(browserLogs).toMatchObject([
73+
'[vite] hot updated: /context/CountProvider.jsx',
74+
'[vite] hot updated: /App.jsx',
75+
'[vite] hot updated: /context/ContextButton.jsx',
76+
'Parent rendered'
77+
])
78+
browserLogs.length = 0
79+
})
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { useContext } from 'react'
2+
import { CountContext } from './CountProvider'
3+
4+
export function ContextButton() {
5+
const { count, setCount } = useContext(CountContext)
6+
return (
7+
<button id="context-button" onClick={() => setCount((count) => count + 1)}>
8+
context-based count is: {count}
9+
</button>
10+
)
11+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { createContext, useState } from 'react'
2+
export const CountContext = createContext()
3+
4+
export const CountProvider = ({ children }) => {
5+
const [count, setCount] = useState(0)
6+
return (
7+
<CountContext.Provider value={{ count, setCount }}>
8+
{children}
9+
<div id="context-provider">context provider</div>
10+
</CountContext.Provider>
11+
)
12+
}

0 commit comments

Comments
 (0)