I'm trying to test a component which change state after a TweenMax animation. Everything's fine on browser, but I cannot understand how to write a test for it.
The problem is Jest doesn't wait for the animation to complete, therefore state doesn't change.
I also tried with jest.runAllTicks() and jest.runAllTimers()
Here some code that would eventually simulate what I'm working on:
Component
class HelloWorld extends React.Component {
constructor(props) {
super(props);
this.state = { done: false };
this.p;
}
componentDiDMount() {
TweenMax.to(ReactDOM.findDOMNOde(this.p), 1, {
onComplete: () => {
this.setState({ done: true })
}
})
}
renderMessage() {
if (this.state.done) {
return "Hello World";
} else {
return "Loading...";
}
}
render () {
return <p ref={p => this.p = p}>{this.renderMessage()}</p>;
}
}
Test (basic structure)
describe("test",()=>{
it("works", ()=>{
const component = mount(<HelloWorld />);
// ...wait some time (or pretend to)
expect(component.find(p).text()).toEqual("Hello World");
})
})