Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add equals method to ShallowWrapper. #124

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});

});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add 2 tests to this?

say we have a composite componente <Foo /> which renders `...

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


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

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