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 =>
{props.message}
const {container, rerender} = render( )
expect(container.firstChild).toHaveTextContent('hi')
rerender( )
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( null} />, {
container,
hydrate: true,
})
expect(initialInputElement).toHaveValue(firstValue)
const secondValue = 'goodbye'
rerender( null} />)
expect(initialInputElement).toHaveValue(secondValue)
})
test('re-renders options.wrapper around node when reactStrictMode is true', () => {
configure({reactStrictMode: true})
const WrapperComponent = ({children}) => (
{children}
)
const Greeting = props => {props.message}
const {container, rerender} = render( , {
wrapper: WrapperComponent,
})
expect(container.firstChild).toMatchInlineSnapshot(`
`)
rerender( )
expect(container.firstChild).toMatchInlineSnapshot(`
`)
})
test('re-renders twice when reactStrictMode is true', () => {
configure({reactStrictMode: true})
const spy = jest.fn()
function Component() {
spy()
return null
}
const {rerender} = render( )
expect(spy).toHaveBeenCalledTimes(2)
spy.mockClear()
rerender( )
expect(spy).toHaveBeenCalledTimes(2)
})
})