From dcc8ab10fde06a963364f6cc79b89aa967d9bef2 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 29 Jul 2018 23:11:30 -0700 Subject: [PATCH] [New] `mount`: add `.equals()` --- .../test/ReactWrapper-spec.jsx | 104 ++++++++++++++++++ packages/enzyme/src/ReactWrapper.js | 16 +++ 2 files changed, 120 insertions(+) diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx index 5251147d2..075f0c8d3 100644 --- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx @@ -415,6 +415,110 @@ describeWithDOM('mount', () => { }); }); + describe('.equals(node)', () => { + it('should allow matches on the root node', () => { + const a =
; + const b =
; + const c =
; + expect(mount(a).equals(b)).to.equal(true); + expect(mount(a).equals(c)).to.equal(false); + }); + + it('should NOT allow matches on a nested node', () => { + const wrapper = mount(( +
+
+
+ )); + const b =
; + expect(wrapper.equals(b)).to.equal(false); + }); + + it('should match composite components', () => { + class Foo extends React.Component { + render() { return
; } + } + const wrapper = mount(( +
+ +
+ )); + const b =
; + expect(wrapper.equals(b)).to.equal(true); + }); + + it.skip('should not expand `node` content', () => { + class Bar extends React.Component { + render() { return
; } + } + + class Foo extends React.Component { + render() { return ; } + } + + expect(mount().equals()).to.equal(true); + expect(mount().equals()).to.equal(false); + }); + + describeIf(is('> 0.13'), 'stateless function components (SFCs)', () => { + it('should match composite SFCs', () => { + const Foo = () => ( +
+ ); + + const wrapper = mount(( +
+ +
+ )); + const b =
; + expect(wrapper.equals(b)).to.equal(true); + }); + + it.skip('should not expand `node` content', () => { + const Bar = () => ( +
+ ); + + const Foo = () => ( + + ); + + expect(mount().equals()).to.equal(true); + expect(mount().equals()).to.equal(false); + }); + }); + + it.skip('flattens arrays of children to compare', () => { + class TwoChildren extends React.Component { + render() { + return ( +
+
+
+
+ ); + } + } + + class TwoChildrenOneArrayed extends React.Component { + render() { + return ( +
+
+ {[
]} +
+ ); + } + } + const twoChildren = mount(); + const twoChildrenOneArrayed = mount(); + + expect(twoChildren.equals(twoChildrenOneArrayed.getElement())).to.equal(true); + expect(twoChildrenOneArrayed.equals(twoChildren.getElement())).to.equal(true); + }); + }); + describe('.hostNodes()', () => { it('should strip out any non-hostNode', () => { class Foo extends React.Component { diff --git a/packages/enzyme/src/ReactWrapper.js b/packages/enzyme/src/ReactWrapper.js index a61184149..d7125dd0b 100644 --- a/packages/enzyme/src/ReactWrapper.js +++ b/packages/enzyme/src/ReactWrapper.js @@ -452,6 +452,22 @@ class ReactWrapper { return Array.isArray(nodes) && nodes.some(node => this.containsMatchingElement(node)); } + /** + * Whether or not a given react element exists in the current render tree. + * + * Example: + * ``` + * const wrapper = mount(); + * expect(wrapper.contains(
)).to.equal(true); + * ``` + * + * @param {ReactElement} node + * @returns {Boolean} + */ + equals(node) { + return this.single('equals', () => nodeEqual(this.getNodeInternal(), node)); + } + /** * Finds every node in the render tree of the current wrapper that matches the provided selector. *