Selectors Object

Functions for locating DOM nodes and component instances.

Static Methods

    find(selector: String, options: Object) -> HTMLElement

    Locate an element inside the component.

    Parameters (4)

    selector
    String

    A document.querySelector or jQuery.find() selector.

    options
    Object
    options.container
    HTMLElement
    options.assert
    Boolean

    If true, the method will throw an error if the node is not valid or could not be found in the DOM.

    findAll(selector: String, options: Object) -> HTMLElement

    Locate an element inside the component.

    Parameters (3)

    selector
    String

    A document.querySelector or jQuery.find() selector.

    options
    Object
    options.container
    HTMLElement

    The container in which to look for the node.

    findAllByType(
      component: React.Component,
      type: React.Class,
      cond: Function
    )
    -> Array.<HTMLElement>

    Find all DOM nodes that belong to a certain React type within a component.

    Parameters (3)

    component
    React.Component
    type
    React.Class

    The type of the component you're looking for.

    cond
    Function

    An optional matcher. ").

    findComponentByType(
      subject: React.Component,
      type: React.Class,
      cond: Function
    )
    -> React.Component

    Low-level helper for finding a rendered component instance of a certain type, optionally matching some condition on its DOM node.

    Parameters (3)

    subject
    React.Component

    The component instance that is expected to contain the target.

    type
    React.Class

    The type of the target.

    cond
    Function

    An optional matcher. ") function.

    Return Value

    React.Component

    The rendered instance, if found.

      Examples

      Asserting on an <Icon /> rendered by a <ComboBoxItem />

      const Subject = require('../SomeComponentThatRendersComboBoxItems');
      const ComboBoxItem = require('components/ComboBoxItem');
      const Icon = require('components/Icon');
      
      it('should render a "banana" icon', function() {
        const subject = React.render(Subject, document.body);
      
        // We locate the instance we want first
        const comboBoxItem = findComponentByType(
          subject, ComboBoxItem, ':contains("Option A")'
        );
      
        // Then we can find things inside of it:
        const iconInsideComboBox = findByType(comboBoxItem, Icon);
      
        // Now, we're assured our assertions are running on the <Icon /> we
        // want; no need to use :nth-of-type(), '.combo-box--item .icon', or
        // whatever.
        assert.equal(iconInsideComboBox.className, 'some-icon');
      });

    findComponentsByType(
      component: React.Component,
      type: React.Class,
      cond: Function
    )
    -> Array.<React.Component>

    Find all rendered components of a certain type.

    Parameters (3)

    component
    React.Component
    type
    React.Class
    cond
    Function

    An optional matcher. ").

    setRootNode(node: HTMLElement)DEPRECATED

    Deprecated

    This interface is part of the legacy API and will be removed in the future. You should instead use scopes for selecting nodes.

    Parameters (1)

    node
    HTMLElement

    The DOM node of mounted instance of the component being tested. All exports will be operating on this node and its children.