parent
9257e0642f
commit
78021a6ab0
@ -1,45 +0,0 @@
|
||||
import React from 'react'
|
||||
import {render, fireEvent} from '../'
|
||||
|
||||
test('render calls useEffect immediately', () => {
|
||||
const effectCb = jest.fn()
|
||||
function MyUselessComponent() {
|
||||
React.useEffect(effectCb)
|
||||
return null
|
||||
}
|
||||
render(<MyUselessComponent />)
|
||||
expect(effectCb).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
test('findByTestId returns the element', async () => {
|
||||
const ref = React.createRef()
|
||||
const {findByTestId} = render(<div ref={ref} data-testid="foo" />)
|
||||
expect(await findByTestId('foo')).toBe(ref.current)
|
||||
})
|
||||
|
||||
test('fireEvent triggers useEffect calls', () => {
|
||||
const effectCb = jest.fn()
|
||||
function Counter() {
|
||||
React.useEffect(effectCb)
|
||||
const [count, setCount] = React.useState(0)
|
||||
return <button onClick={() => setCount(count + 1)}>{count}</button>
|
||||
}
|
||||
const {
|
||||
container: {firstChild: buttonNode},
|
||||
} = render(<Counter />)
|
||||
|
||||
effectCb.mockClear()
|
||||
fireEvent.click(buttonNode)
|
||||
expect(buttonNode).toHaveTextContent('1')
|
||||
expect(effectCb).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
test('calls to hydrate will run useEffects', () => {
|
||||
const effectCb = jest.fn()
|
||||
function MyUselessComponent() {
|
||||
React.useEffect(effectCb)
|
||||
return null
|
||||
}
|
||||
render(<MyUselessComponent />, {hydrate: true})
|
||||
expect(effectCb).toHaveBeenCalledTimes(1)
|
||||
})
|
@ -1,28 +1,33 @@
|
||||
import React from 'react'
|
||||
import angular from 'angular'
|
||||
import 'angular-mocks'
|
||||
import {render, cleanup} from '../'
|
||||
|
||||
beforeEach(() => {
|
||||
angular.module('atl', [])
|
||||
angular.mock.module('atl')
|
||||
})
|
||||
|
||||
test('cleans up the document', () => {
|
||||
const spy = jest.fn()
|
||||
const divId = 'my-div'
|
||||
angular.module('atl').component('atlCleanup', {
|
||||
template: `<div id="{{$ctrl.divId}}"></div>`,
|
||||
controller: class {
|
||||
divId = 'my-div'
|
||||
|
||||
class Test extends React.Component {
|
||||
componentWillUnmount() {
|
||||
expect(document.getElementById(divId)).toBeInTheDocument()
|
||||
$onDestroy() {
|
||||
expect(document.getElementById(this.divId)).toBeInTheDocument()
|
||||
spy()
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
render() {
|
||||
return <div id={divId} />
|
||||
}
|
||||
}
|
||||
|
||||
render(<Test />)
|
||||
render(`<atl-cleanup></atl-cleanup>`)
|
||||
cleanup()
|
||||
expect(document.body.innerHTML).toBe('')
|
||||
expect(spy).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
test('cleanup does not error when an element is not a child', () => {
|
||||
render(<div />, {container: document.createElement('div')})
|
||||
render(`<div></div>`, {container: document.createElement('div')})
|
||||
cleanup()
|
||||
})
|
||||
|
Loading…
Reference in new issue