From 7fcbb35d065ccc3b6bfe740a22ef693cb945e672 Mon Sep 17 00:00:00 2001 From: Angelika Tyborska Date: Fri, 16 Mar 2018 11:42:58 +0100 Subject: [PATCH 1/2] [Tests] Extend findWhere null nodes test Test case for issue #1566 without a fix. --- .../test/ReactWrapper-spec.jsx | 49 ++++++++++++++++--- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx index 38ebca1e4..f4f70b243 100644 --- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx @@ -1105,21 +1105,56 @@ describeWithDOM('mount', () => { }); }); - it('should not pass in null or false nodes', () => { + it('does not pass in null or false nodes', () => { const wrapper = mount(( -
+
+
foo bar
{null} {false} -
+
)); const stub = sinon.stub(); - stub.returns(true); - const spy = sinon.spy(stub); - wrapper.findWhere(spy); - expect(spy.callCount).to.equal(2); + wrapper.findWhere(stub); + + const passedNodes = stub.getCalls().map(({ args: [firstArg] }) => firstArg); + const hasDOMNodes = passedNodes.map(n => [n.debug(), n.getDOMNode() && true]); + const expected = [ + [wrapper.debug(), true], // root + ['
', true], // first div + ['
\n foo bar\n
', true], // second div + ['foo bar', null], // second div's contents + ]; + expect(hasDOMNodes).to.eql(expected); + + // the root, plus the 2 renderable children, plus the grandchild text + expect(stub).to.have.property('callCount', 4); }); + it('allows `.text()` to be called on text nodes', () => { + const wrapper = mount(( +
+
+
foo bar
+ {null} + {false} +
+ )); + + const stub = sinon.stub(); + wrapper.findWhere(stub); + + const passedNodes = stub.getCalls().map(({ args: [firstArg] }) => firstArg); + + const textContents = passedNodes.map(n => [n.debug(), n.text()]); + const expected = [ + [wrapper.debug(), 'foo bar'], // root + ['
', ''], // first div + ['
\n foo bar\n
', 'foo bar'], // second div + ['foo bar', null], // second div's contents + ]; + expect(textContents).to.eql(expected); + }); }); describe('.setProps(newProps[, callback])', () => { From d114e6894094cc737ee73dcc7dc4092edbe2e41f Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 28 Jun 2018 18:26:11 -0700 Subject: [PATCH 2/2] [Fix] `mount`: `text()`: null nodes return null --- packages/enzyme/src/ReactWrapper.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/enzyme/src/ReactWrapper.js b/packages/enzyme/src/ReactWrapper.js index db7235169..ba02ec470 100644 --- a/packages/enzyme/src/ReactWrapper.js +++ b/packages/enzyme/src/ReactWrapper.js @@ -532,7 +532,10 @@ class ReactWrapper { */ text() { const adapter = getAdapter(this[OPTIONS]); - return this.single('text', n => adapter.nodeToHostNode(n).textContent); + return this.single('text', (n) => { + const node = adapter.nodeToHostNode(n); + return node && node.textContent; + }); } /**