Enzyme: Can't not find component if it is passed down as a property in shallow rendering

Created on 22 Sep 2016  路  2Comments  路  Source: enzymejs/enzyme

The user case is like bellow:

I have a simple Toolbar Component, it just provides some style customization for the passed element list by tools

export default function Toolbar(props) {
  return <div>{props.tools}</div>;
}

Toolbar.propTypes = {
  tools: React.PropTypes.array,
};

And I got another component using Toolbar:

import React from 'react';
import Toolbar from './toolbar';
import SelectField from './selectField';

export default class MyList extends React.Component {
  getTools() {
    return [
      (<SelectField key="a" data-ref="a">...</SelectField>),
      (<SelectField key="b" data-ref="b">...</SelectField>),
      (<SelectField key="c" data-ref="c">...</SelectField>),
      (<SelectField key="d" data-ref="d">...</SelectField>),
    ];
  }

  render() {
    return (
      <div>
        <Toolbar tools={this.getTools()} />
        ....
      </div>);
  }
}

And now I want to test <MyList />:

import SelectField from './selectField';

const wrapper = shallow(<MyList />);
const selelctA = wrapper.find({ 'data-ref': 'a' });
const SelectList = wrapper.find(SelectField);
expect(selectA.length).to.equal(1); // will fail, result is 0
expect(SelectList.length).to.equal(4); // will fail anyway, result is 0

Most helpful comment

Since you're shallow rendering, wrapper only has a div and a Toolbar in it. You have to .find(Toolbar).shallow() in order to see what's inside Toolbar.

All 2 comments

Since you're shallow rendering, wrapper only has a div and a Toolbar in it. You have to .find(Toolbar).shallow() in order to see what's inside Toolbar.

@ljharb it works!!! Thank you so much! This problem has bothered me for months!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

potapovDim picture potapovDim  路  3Comments

mattkauffman23 picture mattkauffman23  路  3Comments

rexonms picture rexonms  路  3Comments

aweary picture aweary  路  3Comments

timhonders picture timhonders  路  3Comments