Having some issues using spyOn for testing a method call inside my compose() block for my Reactjs app using recompose, redux, etc.
Basic layout is this:
// index.jsx
import { foo, baz } from './lib';
const enhance = compose(
foo(),
lifecycle({
componentDidMount() {
baz();
}
});
);
export const MyComp = (...);
const mapStateToProps = state => (...);
export connect(mapStateToProps)(enhance(MyComp));
// lib.js
export const foo = () => {
lifecycle({
componentDidMount() {
bar();
}
});
}
export const bar = () => {};
export const baz = () => {};
//index.test.jsx
import * as lib from '.libs';
describe('Test', () => {
const didMountSpy = jest.spyOn(MyComp.prototype, 'componentDidMount');
const fooSpy = jest.spyOn(lib, 'foo');
const barSpy = jest.spyOn(lib, 'bar');
const bazSpy = jest.spyOn(lib, 'baz');
const wrapper = mount(<MyComp ... />);
expect(didMountSpy).toHaveBeenCalledTimes(1); // PASS
expect(bazSpy).toHaveBeenCalledTimes(1); // PASS
expect(fooSpy).toHaveBeenCalledTimes(1); // FAIL
expect(barSpy).toHaveBeenCalledTimes(1); // FAIL
});
Strange part for me is if you look at baz(), I'm able to successfully expect the function call when it's not wrapped in another method in compse(). But I'm not able to expect foo(), bar(). My suspicion is that there is some weirdness with how enzyme/jest mocks the Reactjs lifecycle methods.
Has anyone one run into anything similar and have solutions to get spyOn to work for the nested methods under lifecycle.componentDidMount()?
Thanks!