In my unit test, I want to test whether the parent component is successfully rendering its child component. Here is the code:
describe('Parent Component', () => {
it('renders Child component', () => {
const wrapper = shallow(<Parent store={store} />);
expect(wrapper.find(Child).length).toEqual(1);
});
});
Parent:
const Parent = observer(({ store }) => {
const bookList = toJS(store.planets);
return (
<div>
<div className={style.planet_container}>
{bookList.map(book => {
return <Child key={book.name} name={book.name} />;
})}
</div>
</div>
);
});
Above code is taken from here, but it's not working. I am getting the following error:
Expected 1, Received 0'
Where am I going wrong? I am using Enzyme 3.3 with Jest 23.1.0.
By using enzyme you can check if a parent component rendered its child component. For this use containsMatchingElement(). Now you can test it like this: const wrapper = shallow();
There's a checkbox well hidden in the React DevTools settings that allows you to visually highlight the components that rerendered. To enable it, go to "Profiler" >> click the "Cog wheel" on the right side of the top bar >> "General" tab >> Check the "Highlight updates when components render." checkbox.
Many people choose to use Jest and Enzyme together to test their React web applications. They use Jest as a test runner and assertion library, then use Enzyme to build the tests for their UI. This results in slimmer, cleaner testing code that's also easier to debug when a test breaks.
Both Jest and Enzyme are meant to test the react applications. Jest can be used with any other Javascript framework, but Enzyme is meant to run on react only. Jest can be used without Enzyme, and snapshots can be created and tested perfectly fine. But the Enzyme adds additional functionality to it.
You can check whether a parent component has rendered its child component using containsMatchingElement()
.
Based on Enzyme docs:
Returns whether or not a patternNode react element matches any element in the render tree.
Your test should look like this:
describe('Parent Component', () => {
it('renders Child component', () => {
const wrapper = shallow(<Parent store={store} />);
expect(wrapper.containsMatchingElement(<Child />)).toEqual(true);
});
});
Enzyme allows finding by a component's displayName:
From Enzyme API documentation:
const wrapper = shallow(<MyComponent />);
expect(wrapper.find('Foo')).to.have.lengthOf(1);
So, in order to test sure the Child component has been rendered you can simply reference it by its displayName.
You can print the wrapper's HTML string: console.log( wrapper.debug() )
In case your component is dynamic you can set its displayName to a fixed one, so the test would be more bulletproof:
const MyComponent = props.dynamicChildComponent;
MyComponent.displayName = 'Foo';
Child is not rendering because you are not mocking the props (in this case the prop "store") correctly.
Try this:
it('renders Child component', () => {
const wrapper = shallow( < Parent / > );
wrapper.setProps({
store: {
planets: [{
name: "name 1"
}
]
}
});
expect(wrapper.find(Child)).toHaveLength(1);
});
Try this:
describe('Parent Component', () => {
it('renders Child component', () => {
const wrapper = shallow(<Parent />);
expect(wrapper.find(Child)).toHaveLength(1);
});
});
If you want to check child component elements
let wrapper1 = mount(<PersonalInfo {...personalInfodata} />);
expect(wrapper1.find('ChildComponent').children(0).find('.description').text().length).toBeGreaterThan(0);
you can do something like this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With