Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if child component rendered - Jest, Enzyme

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.

like image 819
darKnight Avatar asked Jun 21 '18 20:06

darKnight


People also ask

How do I test that a child component is rendered?

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();

How do I know if my components are rendered?

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.

Which is better Enzyme or Jest?

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.

How do you test for Jest and enzymes?

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.


5 Answers

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);
  });
});
like image 73
Ekown Avatar answered Oct 01 '22 05:10

Ekown


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';
like image 43
vsync Avatar answered Oct 01 '22 04:10

vsync


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);
});
like image 34
Felipe Suárez Avatar answered Oct 01 '22 04:10

Felipe Suárez


Try this:

describe('Parent Component', () => {
  it('renders Child component', () => {
    const wrapper = shallow(<Parent />);
    expect(wrapper.find(Child)).toHaveLength(1);
  });
});
like image 37
Amarnathrao Sulake Avatar answered Oct 01 '22 04:10

Amarnathrao Sulake


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

like image 30
Dan Patil Avatar answered Oct 01 '22 05:10

Dan Patil