|
1 | 1 | import * as React from 'react'
|
2 | 2 | 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 | +}) |
4 | 11 |
|
5 | 12 | test('renders div into document', () => {
|
6 | 13 | const ref = React.createRef()
|
@@ -134,3 +141,30 @@ test('can be called multiple times on the same container', () => {
|
134 | 141 |
|
135 | 142 | expect(container).toBeEmptyDOMElement()
|
136 | 143 | })
|
| 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