Skip to content

Commit

Permalink
Add equals method to ShallowWrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
themouette committed Jan 21, 2016
1 parent 7429b19 commit 8d3e5c3
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docs/api/ShallowWrapper/equals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# `.equals(node) => Boolean`

Returns whether or not the current wrapper root node render tree looks like the one passed in.


#### Arguments

1. `node` (`ReactElement`): The node whose presence you are detecting in the current instance's
render tree.



#### Returns

`Boolean`: whether or not the current wrapper has a node anywhere in it's render tree that looks
like the one passed in.



#### Example


```jsx
const wrapper = shallow(<MyComponent />);
expect(wrapper.equals(<div className="foo bar" />)).to.equal(true);
```


#### Common Gotchas

- `.equals()` expects a ReactElement, not a selector (like many other methods). Make sure that
when you are calling it you are calling it with a ReactElement or a JSX expression.
- Keep in mind that this method determines equality based on the equality of the node's children as
well.

16 changes: 16 additions & 0 deletions src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ export default class ShallowWrapper {
return findWhereUnwrapped(this, other => nodeEqual(node, other)).length > 0;
}

/**
* Whether or not a given react element exists in the shallow render tree.
*
* Example:
* ```
* const wrapper = shallow(<MyComponent />);
* expect(wrapper.contains(<div className="foo bar" />)).to.equal(true);
* ```
*
* @param {ReactElement} node
* @returns {Boolean}
*/
equals(node) {
return this.single(() => nodeEqual(this.node, node));
}

/**
* Finds every node in the render tree of the current wrapper that matches the provided selector.
*
Expand Down
48 changes: 48 additions & 0 deletions src/__tests__/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,54 @@ describe('shallow', () => {

});

describe('.equals(node)', () => {

it('should allow matches on the root node', () => {
const a = <div className="foo" />;
const b = <div className="foo" />;
const c = <div className="bar" />;
expect(shallow(a).equals(b)).to.equal(true);
expect(shallow(a).equals(c)).to.equal(false);
});

it('should NOT allow matches on a nested node', () => {
const wrapper = shallow(
<div>
<div className="foo" />
</div>
);
const b = <div className="foo" />;
expect(wrapper.equals(b)).to.equal(false);
});

it('should match composite components', () => {
class Foo extends React.Component {
render() { return <div />; }
}
const wrapper = shallow(
<div>
<Foo />
</div>
);
const b = <div><Foo /></div>;
expect(wrapper.equals(b)).to.equal(true);
});

it('should not expand `node` content', () => {
class Bar extends React.Component {
render() { return <div />; }
}

class Foo extends React.Component {
render() { return <Bar />; }
}

expect(shallow(<Foo />).equals(<Bar />)).to.equal(true);
expect(shallow(<Foo />).equals(<Foo />)).to.equal(false);
});

});

describe('.find(selector)', () => {

it('should be able to match the root DOM element', () => {
Expand Down

0 comments on commit 8d3e5c3

Please sign in to comment.