From 8d3e5c3f85d85d34fd4217c17a935acf1648f36d Mon Sep 17 00:00:00 2001 From: Julien Muetton Date: Sun, 17 Jan 2016 20:28:03 +0100 Subject: [PATCH] Add `equals` method to ShallowWrapper. --- docs/api/ShallowWrapper/equals.md | 35 ++++++++++++++++++++ src/ShallowWrapper.js | 16 ++++++++++ src/__tests__/ShallowWrapper-spec.js | 48 ++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 docs/api/ShallowWrapper/equals.md diff --git a/docs/api/ShallowWrapper/equals.md b/docs/api/ShallowWrapper/equals.md new file mode 100644 index 000000000..ab862f67a --- /dev/null +++ b/docs/api/ShallowWrapper/equals.md @@ -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(); +expect(wrapper.equals(
)).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. + diff --git a/src/ShallowWrapper.js b/src/ShallowWrapper.js index 620b2c7de..9e800f062 100644 --- a/src/ShallowWrapper.js +++ b/src/ShallowWrapper.js @@ -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(); + * expect(wrapper.contains(
)).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. * diff --git a/src/__tests__/ShallowWrapper-spec.js b/src/__tests__/ShallowWrapper-spec.js index 4ee1b8873..4f1899c3a 100644 --- a/src/__tests__/ShallowWrapper-spec.js +++ b/src/__tests__/ShallowWrapper-spec.js @@ -85,6 +85,54 @@ describe('shallow', () => { }); + describe('.equals(node)', () => { + + it('should allow matches on the root node', () => { + const a =
; + const b =
; + const c =
; + 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( +
+
+
+ ); + const b =
; + expect(wrapper.equals(b)).to.equal(false); + }); + + it('should match composite components', () => { + class Foo extends React.Component { + render() { return
; } + } + const wrapper = shallow( +
+ +
+ ); + const b =
; + expect(wrapper.equals(b)).to.equal(true); + }); + + it('should not expand `node` content', () => { + class Bar extends React.Component { + render() { return
; } + } + + class Foo extends React.Component { + render() { return ; } + } + + expect(shallow().equals()).to.equal(true); + expect(shallow().equals()).to.equal(false); + }); + + }); + describe('.find(selector)', () => { it('should be able to match the root DOM element', () => {