Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

render multiple components with enzyme shallow

Tags:

reactjs

enzyme

Is it possible to render more tan one component using enzyme's shallow?

I have the following failing test:

  it('should have different ids for different checkboxes', () => {
      const wrapper = shallow(<div><Checkbox {...props} /><Checkbox {...props} /></div>)

    const inputs = wrapper.find('input')
    expect(inputs.length).toBe(1)
  })
like image 759
dagda1 Avatar asked Jul 04 '26 20:07

dagda1


1 Answers

I think you'll want to use enzyme's mount instead of shallow. This is assuming your Checkbox component is what will give you the input that your test is looking for. shallow doesn't render nested components. As its name suggests, it does a shallow render of what you pass to it. mount, on the other hand, will render nested components.

For more details on mount and shallow, check out their documentation: https://github.com/airbnb/enzyme/blob/master/docs/api/mount.md https://github.com/airbnb/enzyme/blob/master/docs/api/shallow.md

All that being said, if this is a unit test then it shouldn't care about the output of Checkbox. There would be unit tests for Checkbox to check its output.

like image 143
MrOBrian Avatar answered Jul 11 '26 15:07

MrOBrian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!