File size: 8,772 Bytes
1e92f2d |
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
import * as React from 'react'
import ReactDOM from 'react-dom'
import ReactDOMServer from 'react-dom/server'
import {fireEvent, render, screen, configure} from '../'
const isReact18 = React.version.startsWith('18.')
const isReact19 = React.version.startsWith('19.')
const testGateReact18 = isReact18 ? test : test.skip
const testGateReact19 = isReact19 ? test : test.skip
describe('render API', () => {
let originalConfig
beforeEach(() => {
// Grab the existing configuration so we can restore
// it at the end of the test
configure(existingConfig => {
originalConfig = existingConfig
// Don't change the existing config
return {}
})
})
afterEach(() => {
configure(originalConfig)
})
test('renders div into document', () => {
const ref = React.createRef()
const {container} = render(<div ref={ref} />)
expect(container.firstChild).toBe(ref.current)
})
test('works great with react portals', () => {
class MyPortal extends React.Component {
constructor(...args) {
super(...args)
this.portalNode = document.createElement('div')
this.portalNode.dataset.testid = 'my-portal'
}
componentDidMount() {
document.body.appendChild(this.portalNode)
}
componentWillUnmount() {
this.portalNode.parentNode.removeChild(this.portalNode)
}
render() {
return ReactDOM.createPortal(
<Greet greeting="Hello" subject="World" />,
this.portalNode,
)
}
}
function Greet({greeting, subject}) {
return (
<div>
<strong>
{greeting} {subject}
</strong>
</div>
)
}
const {unmount} = render(<MyPortal />)
expect(screen.getByText('Hello World')).toBeInTheDocument()
const portalNode = screen.getByTestId('my-portal')
expect(portalNode).toBeInTheDocument()
unmount()
expect(portalNode).not.toBeInTheDocument()
})
test('returns baseElement which defaults to document.body', () => {
const {baseElement} = render(<div />)
expect(baseElement).toBe(document.body)
})
test('supports fragments', () => {
class Test extends React.Component {
render() {
return (
<div>
<code>DocumentFragment</code> is pretty cool!
</div>
)
}
}
const {asFragment} = render(<Test />)
expect(asFragment()).toMatchSnapshot()
})
test('renders options.wrapper around node', () => {
const WrapperComponent = ({children}) => (
<div data-testid="wrapper">{children}</div>
)
const {container} = render(<div data-testid="inner" />, {
wrapper: WrapperComponent,
})
expect(screen.getByTestId('wrapper')).toBeInTheDocument()
expect(container.firstChild).toMatchInlineSnapshot(`
<div
data-testid=wrapper
>
<div
data-testid=inner
/>
</div>
`)
})
test('renders options.wrapper around node when reactStrictMode is true', () => {
configure({reactStrictMode: true})
const WrapperComponent = ({children}) => (
<div data-testid="wrapper">{children}</div>
)
const {container} = render(<div data-testid="inner" />, {
wrapper: WrapperComponent,
})
expect(screen.getByTestId('wrapper')).toBeInTheDocument()
expect(container.firstChild).toMatchInlineSnapshot(`
<div
data-testid=wrapper
>
<div
data-testid=inner
/>
</div>
`)
})
test('renders twice when reactStrictMode is true', () => {
configure({reactStrictMode: true})
const spy = jest.fn()
function Component() {
spy()
return null
}
render(<Component />)
expect(spy).toHaveBeenCalledTimes(2)
})
test('flushes useEffect cleanup functions sync on unmount()', () => {
const spy = jest.fn()
function Component() {
React.useEffect(() => spy, [])
return null
}
const {unmount} = render(<Component />)
expect(spy).toHaveBeenCalledTimes(0)
unmount()
expect(spy).toHaveBeenCalledTimes(1)
})
test('can be called multiple times on the same container', () => {
const container = document.createElement('div')
const {unmount} = render(<strong />, {container})
expect(container).toContainHTML('<strong></strong>')
render(<em />, {container})
expect(container).toContainHTML('<em></em>')
unmount()
expect(container).toBeEmptyDOMElement()
})
test('hydrate will make the UI interactive', () => {
function App() {
const [clicked, handleClick] = React.useReducer(n => n + 1, 0)
return (
<button type="button" onClick={handleClick}>
clicked:{clicked}
</button>
)
}
const ui = <App />
const container = document.createElement('div')
document.body.appendChild(container)
container.innerHTML = ReactDOMServer.renderToString(ui)
expect(container).toHaveTextContent('clicked:0')
render(ui, {container, hydrate: true})
fireEvent.click(container.querySelector('button'))
expect(container).toHaveTextContent('clicked:1')
})
test('hydrate can have a wrapper', () => {
const wrapperComponentMountEffect = jest.fn()
function WrapperComponent({children}) {
React.useEffect(() => {
wrapperComponentMountEffect()
})
return children
}
const ui = <div />
const container = document.createElement('div')
document.body.appendChild(container)
container.innerHTML = ReactDOMServer.renderToString(ui)
render(ui, {container, hydrate: true, wrapper: WrapperComponent})
expect(wrapperComponentMountEffect).toHaveBeenCalledTimes(1)
})
testGateReact18('legacyRoot uses legacy ReactDOM.render', () => {
expect(() => {
render(<div />, {legacyRoot: true})
}).toErrorDev(
[
"Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot",
],
{withoutStack: true},
)
})
testGateReact19('legacyRoot throws', () => {
expect(() => {
render(<div />, {legacyRoot: true})
}).toThrowErrorMatchingInlineSnapshot(
`\`legacyRoot: true\` is not supported in this version of React. If your app runs React 19 or later, you should remove this flag. If your app runs React 18 or earlier, visit https://react.dev/blog/2022/03/08/react-18-upgrade-guide for upgrade instructions.`,
)
})
testGateReact18('legacyRoot uses legacy ReactDOM.hydrate', () => {
const ui = <div />
const container = document.createElement('div')
container.innerHTML = ReactDOMServer.renderToString(ui)
expect(() => {
render(ui, {container, hydrate: true, legacyRoot: true})
}).toErrorDev(
[
"Warning: ReactDOM.hydrate is no longer supported in React 18. Use hydrateRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot",
],
{withoutStack: true},
)
})
testGateReact19('legacyRoot throws even with hydrate', () => {
const ui = <div />
const container = document.createElement('div')
container.innerHTML = ReactDOMServer.renderToString(ui)
expect(() => {
render(ui, {container, hydrate: true, legacyRoot: true})
}).toThrowErrorMatchingInlineSnapshot(
`\`legacyRoot: true\` is not supported in this version of React. If your app runs React 19 or later, you should remove this flag. If your app runs React 18 or earlier, visit https://react.dev/blog/2022/03/08/react-18-upgrade-guide for upgrade instructions.`,
)
})
test('reactStrictMode in renderOptions has precedence over config when rendering', () => {
const wrapperComponentMountEffect = jest.fn()
function WrapperComponent({children}) {
React.useEffect(() => {
wrapperComponentMountEffect()
})
return children
}
const ui = <div />
configure({reactStrictMode: false})
render(ui, {wrapper: WrapperComponent, reactStrictMode: true})
expect(wrapperComponentMountEffect).toHaveBeenCalledTimes(2)
})
test('reactStrictMode in config is used when renderOptions does not specify reactStrictMode', () => {
const wrapperComponentMountEffect = jest.fn()
function WrapperComponent({children}) {
React.useEffect(() => {
wrapperComponentMountEffect()
})
return children
}
const ui = <div />
configure({reactStrictMode: true})
render(ui, {wrapper: WrapperComponent})
expect(wrapperComponentMountEffect).toHaveBeenCalledTimes(2)
})
})
|