Skip to content

Commit 8cf1357

Browse files
committed
test: hydrate + rerender behavior
1 parent 68d2a23 commit 8cf1357

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/__tests__/render.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import * as React from 'react'
22
import ReactDOM from 'react-dom'
3-
import {render, screen} from '../'
3+
import ReactDOMServer from 'react-dom/server'
4+
import {fireEvent, render, screen} from '../'
5+
6+
afterEach(() => {
7+
if (console.error.mockRestore !== undefined) {
8+
console.error.mockRestore()
9+
}
10+
})
411

512
test('renders div into document', () => {
613
const ref = React.createRef()
@@ -134,3 +141,30 @@ test('can be called multiple times on the same container', () => {
134141

135142
expect(container).toBeEmptyDOMElement()
136143
})
144+
145+
test('hydrate will make the UI interactive', () => {
146+
jest.spyOn(console, 'error').mockImplementation(() => {})
147+
function App() {
148+
const [clicked, handleClick] = React.useReducer(n => n + 1, 0)
149+
150+
return (
151+
<button type="button" onClick={handleClick}>
152+
clicked:{clicked}
153+
</button>
154+
)
155+
}
156+
const ui = <App />
157+
const container = document.createElement('div')
158+
document.body.appendChild(container)
159+
container.innerHTML = ReactDOMServer.renderToString(ui)
160+
161+
expect(container).toHaveTextContent('clicked:0')
162+
163+
render(ui, {container, hydrate: true})
164+
165+
expect(console.error).not.toHaveBeenCalled()
166+
167+
fireEvent.click(container.querySelector('button'))
168+
169+
expect(container).toHaveTextContent('clicked:1')
170+
})

0 commit comments

Comments
 (0)