I'm having a problem testing my components that are wrapped in HOCs. I have a class component that's wrapped by 2 HOC's(Redux Form and Connect). I don't want to test the connected components. I want to test the methods inside of the class.
I read on the Redux docs that you can test the class component separately by exporting it from the component file and importing the named component in the test file. I've done that and am still unable to test class methods. I also tried to use the enzyme dive() method to bypass the HOCs which then gave me an error:
Invariant Violation: Could not find "store" in either the context or props of "Connect(Form(MyComponent))"
Component file:
export class MyComponent extends Component {
getThing() {
return thing;
}
render() {
<Form >
...
</Form>
}
}
MyComponent = reduxForm({
...
})(MyComponent)
export default connect(
mapStateToProps,
mapDispatchToProps
)(MyComponent);
Test file:
import React from 'react';
import { shallow } from 'enzyme';
import { MyComponent } from '../MyComponent';
let wrapper;
describe('MyComponent tests', () => {
beforeEach(() => {
wrapper = shallow(<MyComponent />).dive().dive()
it('has a getThing method', () => {
const instance = wrapper.instance();
expect(instance.getThing).toBeDefined();
}); //Invariant Violation: Could not find "store" in either the context or props of "Connect(Form(MyComponent))"
});
I expect the method to be defined but I'm still getting the Invariant Violation error. I also tried not using dive() and expected the method to be defined but received undefined. Any insight on what I'm doing wrong?