Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enzyme is not finding component by props

Tags:

I've got a component I'm testing with Enzyme that looks like the following:

<RichTextEditor name="name" onChange={[Function]} value="<p>what</p>" focus={false}>
  <div className="rich-text-editor">
  <div className="btn-group" role="group">
  <StyleButton active={false} icon="fa-list-ul" label="UL" onToggle={[Function]} style="unordered-list-item">
  // ...

I'm trying to detect the presence of the StyleButton component there like so:

mount(<RichTextEditor />).find('StyleButton[label="UL"]')

But no components are returned. I can find all the StyleButtons by just searching for the string "StyleButton" but I can't find by property, including just by using the property selector on its own.

The first code block I pasted up there is from the debug output of mounting the RichTextEditor, so the StyleButton is definitely there.

Any ideas?

Thanks.

like image 501
whalabi Avatar asked Nov 24 '16 00:11

whalabi


People also ask

Can components be passed as props?

You can pass a component as props in React by using the built-in children prop. All elements you pass between the opening and closing tags of a component get assigned to the children prop.

What is a component prop?

The simplest way to explain component props would be to say that they function similarly to HTML attributes. In other words, props provide configuration values for the component.


2 Answers

In the docs there is no option to mix name of component with props:

  • CSS Selectors
  • Component Constructors
  • Component Display Name
  • Object Property Selector

You can use findWhere:

 wrapper.findWhere(n => n.name() === 'StyleButton' && n.prop('label') === 'UL')
like image 156
Lucas Katayama Avatar answered Oct 07 '22 20:10

Lucas Katayama


Since find returns another ReactWrapper you could chain them that way: mount(<RichTextEditor />).find({label:"UL"}).find(StyleButton)

Note: the order matters.

Inspired by @romanlv's comment, but I find this syntax clearer.

like image 22
TMG Avatar answered Oct 07 '22 21:10

TMG