I'm trying to write a unit test with Jest
for a React
component that utilises Transition
from react-transition-group
. I need to mock the Transition
so my tests don't have to wait for the animation to complete. However, in addition to 'skipping' the animation, I need the onExited
callback to fire on my mocked Transition
component.
Here's how my Component.js
is using Transition
:
...
return (
<Transition
timeout={1500}
in={this.state.show}
onExited={handleExited}>
{status =>
<button onClick={this.setState({show: false}) className={`component-${status}`}>button</button>
}
</Transition>
)
And here's my Component.test.js
:
import React from 'react'
import {render, fireEvent} from 'react-testing-library'
import Component from '../Component'
test('check', () => {
const handleCompletion = jest.fn()
const {getByText} = render(
<Component
onButtonClick={handleCompletion}
/>
)
const button = getByText('button')
fireEvent.click(button)
expect(handleCompletion).toHaveBeenCalledTimes(1)
})
The idea is that once a button
is clicked, the component animates and then, on completion, fires a callback.
How do I mock the Transition
correctly so it skips the animation but still fires the onExited
callback?