How to test React component with children using jest and enzyme?

6k Views Asked by At

Component to be tested. this.props.children have child components of example.js

class Example extends component {
  constructor(){
      super(props);
      this.state={};
    }
   render() {
        <div>{this.props.children}</div>
     }
}

test case

it("should render  Example with children", () => {
    const wrapper = shallow(<Example {...props} />);
     expect(wrapper.find("div").text()).toBe(""); //
   });

how to test the child components passed ?

Thanks.

1

There are 1 best solutions below

2
Vishal Nai On BEST ANSWER

You can use like below

describe('Parent Component', () => {
   it('renders Child component', () => {
   const wrapper = shallow(<Parent store={store} />);
   expect(wrapper.find(Child).length).toEqual(1);
  });
});

Also if you want to test only child component div use like this

const wrapper = shallow(<MyComponent />);
expect(wrapper.find('Foo')).to.have.lengthOf(1);