I have a React component like the following:
Component1.js
import React from 'react';
import Component2 from './Component2.js';
const Component1 = () => (<div data-test="component1-component"><Component2 /></div>);
Component2.js
import React from 'react';
import { connect } from 'react-redux';
const Component2 = (props) => {
return(
<div data-test="component2-component">
<h1>{`I am Component #2. Key is ${props.key}`}</h1>
</div>
);
};
const mapStateToProps = state => ({ key: state.key });
export default connect(mapStateToProps)(Component2);
As you can see, Component1
renders Component2
as a child component and this child component is connected to Redux store. I want to write a Jest-Enzyme test case for Component1
that tests to see whether it renders Component2
as its child without connecting Component2
to Redux mock store. I am looking for something like the following:
describe('Component1', () => {
const wrapper = mount(<Component1 />);
const component1Component = wrapper.find({ "data-test": "component1-component" });
const component2Component = wrapper.find({ "data-test": "component2-component" });
it('renders the ~Component2~ as a child component', () => {
expect(component2Component.exists()).toBeTruthy();
});
});
Currently, I am getting an error saying that the child component is not provided with the store. How do I go about testing this?
Component1
with aProvider
in your test =><Provider store={store}> <Component1 /> </Provider>
– Olivier Boissé