enzyme: Can't assert that methods are being called
Describe the bug
I’m trying to check if two methods are being called from componentDidMount. The first method gets called and it’s successful, but the second one fails. If I try testing just the second method it fails as well. I have to mention that I’m not using arrow functions and I’m binding in the constructor.
The following is an example of the test I wrote
it("calls handleAlertsRefetch and handleRefetch on mount", () => {
const handleAlertsRefetch = jest.spyOn(
Component.prototype,
"handleAlertsRefetch"
);
const handleRefetch = jest.spyOn(
Component.prototype,
"handleRefetch"
);
const wrapper = createContext(BottomOverview, props);
expect(handleAlertsRefetch).toHaveBeenCalled();
expect(handleRefetch).toHaveBeenCalled();
});
The createContext
is a method that puts the component in an Apollo and react-router context:
export const createContext = (Component, props) => {
Component.contextTypes = {
router: PropTypes.object
};
const context = createRouterContext();
const childContextTypes = {
router: PropTypes.object
};
const wrapper = mount(
<MockedProvider>
<Component {...props} />
</MockedProvider>,
{
context,
childContextTypes
}
);
return wrapper;
};
Expected behavior Both methods should be called and tests should be pass.
Additional context I use React 16 with the CRA boilerplate.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 17 (9 by maintainers)
hey buddy I fixed the component code and now it works, thanks a lot for your help. Where can I find more information about this aspect of asynchronicity?
Hey, I removed everything from the body of those methods, still the same issue with it. Thank you for your response!