I have a react component (16.2):
import externalLibrary from 'wherever';
class MyComponent extends React.Component {
componentDidMount() {
externalLibrary.doStuff().then((data) => {
this.setState({ ...this.state,
data: data
});
});
}
render() {
return (
<div>
{this.state.data.map((d) => <SubComponent key={d.id}
data={d} />)}
</div>
);
}
}
I want to unit test this (using Jest), lets say I want to write a test that checks that checks that a "SubComponent" is rendered for each item in the array "data". How can I do this? All of React's code samples for async code assume I want to test the async code, but in this case I want to test that it rendered correctly. You can assume that "externalLibrary" is mocked correctly, and we know doStuff() will resolve a data array with three items in it. How do I tell the Jest code to wait until the promise inside componentDidMount (doStuff()) resolves, then check the rendered component and count the elements?
(I'm using Enzyme too if that changes anything)
import { shallow } from 'enzyme';
import MyComponent from './MyComponent';
import SubComponent from './SubComponent';
describe("<MyComponent />", () => {
it("Renders the correct number of SubComponents", done => {
wrapper = shallow(<MyComponent />);
// This fails because it runs before the subcomponents are rendered
expect(wrapper.find(SubComponent)).toHaveLength(3);
});
});
wrapper = await shallow(<MyComponent />);- anoop