Skip to content

Easier way to access child refs #361

@joshma

Description

@joshma

Once in awhile we'll have a test that renders a component P with child C:

<P><C>
  <div></div>
  <div></div>
</C></P>

To get the second <div> in a test, we'll add a ref to it:

<P><C>
  <div></div>
  <div ref={'__testDiv'}></div>
</C></P>
//
TestUtils.findRenderedComponentWithType(p, C).refs.testDiv

With enzyme, though, it doesn't seem to be easy to get to that ref

pWrapper.find(C).ref('__testDiv') // Error: ReactWrapper::ref(refname) can only be called on the root

The only alternative has to use css classes, which isn't ideal since that shows up in the final markup. The div itself is pretty hard to select for - is there an easier way I'm missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions