Skip to content
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
49 changes: 42 additions & 7 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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((
<div>
<section>
<div className="foo bar" />
<div>foo bar</div>
{null}
{false}
</div>
</section>
));
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
['<div className="foo bar" />', true], // first div
['<div>\n foo bar\n</div>', 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((
<section>
<div className="foo bar" />
<div>foo bar</div>
{null}
{false}
</section>
));

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
['<div className="foo bar" />', ''], // first div
['<div>\n foo bar\n</div>', 'foo bar'], // second div
['foo bar', null], // second div's contents
];
expect(textContents).to.eql(expected);
});
});

describe('.setProps(newProps[, callback])', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}

/**
Expand Down