File size: 2,585 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 |
import * as React from 'react'
import {render, configure} from '../'
describe('rerender 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('rerender will re-render the element', () => {
const Greeting = props => <div>{props.message}</div>
const {container, rerender} = render(<Greeting message="hi" />)
expect(container.firstChild).toHaveTextContent('hi')
rerender(<Greeting message="hey" />)
expect(container.firstChild).toHaveTextContent('hey')
})
test('hydrate will not update props until next render', () => {
const initialInputElement = document.createElement('input')
const container = document.createElement('div')
container.appendChild(initialInputElement)
document.body.appendChild(container)
const firstValue = 'hello'
initialInputElement.value = firstValue
const {rerender} = render(<input value="" onChange={() => null} />, {
container,
hydrate: true,
})
expect(initialInputElement).toHaveValue(firstValue)
const secondValue = 'goodbye'
rerender(<input value={secondValue} onChange={() => null} />)
expect(initialInputElement).toHaveValue(secondValue)
})
test('re-renders options.wrapper around node when reactStrictMode is true', () => {
configure({reactStrictMode: true})
const WrapperComponent = ({children}) => (
<div data-testid="wrapper">{children}</div>
)
const Greeting = props => <div>{props.message}</div>
const {container, rerender} = render(<Greeting message="hi" />, {
wrapper: WrapperComponent,
})
expect(container.firstChild).toMatchInlineSnapshot(`
<div
data-testid=wrapper
>
<div>
hi
</div>
</div>
`)
rerender(<Greeting message="hey" />)
expect(container.firstChild).toMatchInlineSnapshot(`
<div
data-testid=wrapper
>
<div>
hey
</div>
</div>
`)
})
test('re-renders twice when reactStrictMode is true', () => {
configure({reactStrictMode: true})
const spy = jest.fn()
function Component() {
spy()
return null
}
const {rerender} = render(<Component />)
expect(spy).toHaveBeenCalledTimes(2)
spy.mockClear()
rerender(<Component />)
expect(spy).toHaveBeenCalledTimes(2)
})
})
|