I am trying to write a test case for a functional component which uses hooks for state update and gets data from a mock promise call.
// Functional Component
const MockPage = () => {
const [mockData, setMockData] = React.useState<IMock[]>([]);
const mockService = new mockService();
React.useEffect(() => {
mockService.getMockData().then((res) => {
setMockData(res);
});
}, []);
return (
<div>
<MockTable mocktData={mockData} />
</div>
);
}
Here is the test case that I have written which PASSES :
it('renders without crashing', () => {
mount(<MockPage />);
});
Here are the test cases that I have written which FAILS :
it('renders without crashing', () => {
const wrapper = mount(<MockPage />);
expect(wrapper.is(MountTable)).toBeDefined();
});
it('renders without crashing', () => {
const wrapper = mount(<MockPage />);
expect(wrapper.find(MountTable).prop('mockData').toHaveLenght(2);
}); // assusming the mockData has 2 objects in an array
What is the mistake(s) am committing here? Is there something am missing?
Note: I tried doing with act() too , but was not successful.
console.log('wrapper', wrapper.debug());Also on the last test you're missing a parenthesis before.toHaveLenght(2).