matchesElement(node) · Enzyme (original) (raw)

.matchesElement(patternNode) => Boolean

Returns whether or not a given react element patternNode matches the wrapper's render tree. It must be a single-node wrapper, and only the root node is checked.

The patternNode acts like a wildcard. For it to match a node in the wrapper:

Arguments

  1. patternNode (ReactElement): The node whose presence you are detecting in the wrapper's single node.

Returns

Boolean: whether or not the current wrapper match the one passed in.

Example

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    // ...
  }

  render() {
    return (
      <button type="button" onClick={this.handleClick} className="foo bar">Hello</button>
    );
  }
}

const wrapper = mount(<MyComponent />);
expect(wrapper.matchesElement(<button>Hello</button>)).to.equal(true);
expect(wrapper.matchesElement(<button className="foo bar">Hello</button>)).to.equal(true);

Common Gotchas