Permalink
Switch branches/tags
v2.9.1 v2.9.0 v2.8.2 v2.8.1 v2.8.0 v2.7.1 v2.7.0 v2.6.0 v2.5.2 v2.5.1 v2.5.0 v2.4.2 v2.4.1 v2.4.0 v2.3.0 v2.2.0 v2.1.0 v2.0.0 v1.6.0 v1.5.0 v1.4.1 v1.4.0 v1.3.1 v1.3.0 v1.2.0 v1.1.0 v1.0.0 reagent-v1.0.1 reagent-v1.0.0 enzyme@3.3.0 enzyme@3.2.0 enzyme@3.1.1 enzyme@3.1.0 enzyme@3.0.0 enzyme@3.0.0-beta.7 enzyme@3.0.0-beta.6 enzyme@3.0.0-beta.5 enzyme@3.0.0-beta.4 enzyme@3.0.0-beta.3 enzyme@3.0.0-beta.2 enzyme@3.0.0-beta.1 enzyme-test-suite@1.0.0 enzyme-test-suite@1.0.0-beta.7 enzyme-test-suite@1.0.0-beta.6 enzyme-test-suite@1.0.0-beta.5 enzyme-test-suite@1.0.0-beta.4 enzyme-test-suite@1.0.0-beta.3 enzyme-test-suite@1.0.0-beta.2 enzyme-test-suite@1.0.0-beta.1 enzyme-adapter-utils@1.4.0 enzyme-adapter-utils@1.3.0 enzyme-adapter-utils@1.2.0 enzyme-adapter-utils@1.1.1 enzyme-adapter-utils@1.1.0 enzyme-adapter-utils@1.0.1 enzyme-adapter-utils@1.0.0 enzyme-adapter-utils@1.0.0-beta.7 enzyme-adapter-utils@1.0.0-beta.6 enzyme-adapter-utils@1.0.0-beta.5 enzyme-adapter-utils@1.0.0-beta.4 enzyme-adapter-utils@1.0.0-beta.3 enzyme-adapter-utils@1.0.0-beta.2 enzyme-adapter-utils@1.0.0-beta.1 enzyme-adapter-react-helper@1.2.3 enzyme-adapter-react-helper@1.2.2 enzyme-adapter-react-helper@1.2.1 enzyme-adapter-react-helper@1.2.0 enzyme-adapter-react-helper@1.1.0 enzyme-adapter-react-helper@1.0.3 enzyme-adapter-react-helper@1.0.2 enzyme-adapter-react-helper@1.0.1 enzyme-adapter-react-helper@1.0.0 enzyme-adapter-react-16@1.1.1 enzyme-adapter-react-16@1.1.0 enzyme-adapter-react-16@1.0.4 enzyme-adapter-react-16@1.0.3 enzyme-adapter-react-16@1.0.2 enzyme-adapter-react-16@1.0.1 enzyme-adapter-react-16@1.0.0 enzyme-adapter-react-16@1.0.0-beta.7 enzyme-adapter-react-16@1.0.0-beta.6 enzyme-adapter-react-16@1.0.0-beta.5 enzyme-adapter-react-16@1.0.0-beta.4 enzyme-adapter-react-16@1.0.0-beta.3 enzyme-adapter-react-16@1.0.0-beta.2 enzyme-adapter-react-16@1.0.0-beta.1 enzyme-adapter-react-15.4@1.0.6 enzyme-adapter-react-15.4@1.0.5 enzyme-adapter-react-15.4@1.0.4 enzyme-adapter-react-15.4@1.0.3 enzyme-adapter-react-15.4@1.0.2 enzyme-adapter-react-15.4@1.0.1 enzyme-adapter-react-15.4@1.0.0 enzyme-adapter-react-15.4@1.0.0-beta.7 enzyme-adapter-react-15.4@1.0.0-beta.6 enzyme-adapter-react-15.4@1.0.0-beta.5 enzyme-adapter-react-15.4@1.0.0-beta.4 enzyme-adapter-react-15.4@1.0.0-beta.3 enzyme-adapter-react-15.4@1.0.0-beta.2 enzyme-adapter-react-15.4@1.0.0-beta.1
Nothing to show
Find file Copy path
133 lines (86 sloc) 3.25 KB

enzyme Selectors

Many methods in enzyme’s API accept a selector as an argument. Selectors in enzyme can fall into one of the following five categories:

1. A Valid CSS Selector

enzyme supports a subset of valid CSS selectors to find nodes inside a render tree. Support is as follows:

  • class syntax (.foo, .foo-bar, etc.)
  • element syntax (input, div, span, etc.)
  • id syntax (#foo, #foo-bar, etc.)
  • attribute syntax ([href="foo"], [type="text"], etc.)

Further, enzyme supports combining any of those supported syntaxes together to uniquely identify a single node. For instance:

div.foo.bar
input#input-name
a[href="foo"]

enzyme also gives support for the following contextual selectors

.foo .bar
.foo > .bar
.foo + .bar
.foo ~ .bar
.foo input

Want more CSS support?

PRs implementing more support for CSS selectors will be accepted and is an area of development for enzyme that will likely be focused on in the future.

2. Prop Selector

In addition to traditional CSS selectors, enzyme supports using a React prop like an Attribute Selector as if it were an HTML attribute. Strings, Numbers, and Boolean property values are supported.

const wrapper = mount((
  <div>
    <span foo={3} bar={false} title="baz" />
  </div>
));

wrapper.find('[foo=3]');
wrapper.find('[bar=false]');
wrapper.find('[title="baz"]');

The Key and Ref Prop

While in most cases, any React prop can be used, there are exceptions. The key and ref props will never work. This decision comes from how React uses these props internally, which means they should not be relied upon.

3. A React Component Constructor

enzyme allows you to find components based on their constructor. You can pass in the reference to the component’s constructor:

function MyComponent() {
  return <div />;
}

// find instances of MyComponent
const myComponents = wrapper.find(MyComponent);

4. A React Component’s displayName

enzyme allows you to find components based on a component’s displayName. If a component exists in a render tree where its displayName is set and has its first character as a capital letter, a string can be used to find it:

function MyComponent() {
  return <div />;
}
MyComponent.displayName = 'My Component';

// find instances of MyComponent
const myComponents = wrapper.find('My Component');

NOTE: This will only work if the selector (and thus the component’s displayName) is a string starting with a capital letter. Strings starting with lower case letters will assume it is a CSS selector using the tag syntax.

5. Object Property Selector

enzyme allows you to find components and nodes based on a subset of their properties:

const wrapper = mount((
  <div>
    <span foo={3} bar={false} title="baz" />
  </div>
));

wrapper.find({ foo: 3 });
wrapper.find({ bar: false });
wrapper.find({ title: 'baz' });

Undefined Properties

Undefined properties are not allowed in the object property selector and will cause an error:

wrapper.find({ foo: 3, bar: undefined });
// => TypeError: Enzyme::Props can't have 'undefined' values. Try using 'findWhere()' instead.

If you have to search by undefined property value, use .findWhere().