Skip to content

Commit

Permalink
fix #145 contains does not fail on string input
Browse files Browse the repository at this point in the history
  • Loading branch information
smacker committed Jan 29, 2016
1 parent a2ffa51 commit 07dcd56
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,13 @@ export default class ShallowWrapper {
* @returns {Boolean}
*/
contains(node) {
return findWhereUnwrapped(this, other => nodeEqual(node, other)).length > 0;
let nodeToCompare = node;

if (typeof nodeToCompare === 'number') {
nodeToCompare = '' + nodeToCompare;
}

return findWhereUnwrapped(this, other => nodeEqual(nodeToCompare, other)).length > 0;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function nodeEqual(a, b) {
if (a === b) return true;
if (!a || !b) return false;
if (a.type !== b.type) return false;

const left = propsOfNode(a);
const leftKeys = Object.keys(left);
const right = propsOfNode(b);
Expand All @@ -80,7 +81,12 @@ export function nodeEqual(a, b) {
return false;
}
}
return leftKeys.length === Object.keys(right).length;

if (typeof a !== 'string') {
return leftKeys.length === Object.keys(right).length;
}

return false;
}

// 'click' => 'onClick'
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ describe('shallow', () => {
expect(wrapper.contains(b)).to.equal(true);
});

it('should work with strings', () => {
const wrapper = shallow(<div>foo</div>);

expect(wrapper.contains('foo')).to.equal(true);
expect(wrapper.contains('bar')).to.equal(false);
});

it('should work with numbers', () => {
const wrapper = shallow(<div>1</div>);

expect(wrapper.contains(1)).to.equal(true);
expect(wrapper.contains(2)).to.equal(false);
});

});

describe('.equals(node)', () => {
Expand Down

0 comments on commit 07dcd56

Please sign in to comment.