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

[Tests] Extend findWhere null nodes test #1582

Merged
merged 2 commits into from
Jun 29, 2018
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