Skip to content

Commit

Permalink
Merge pull request #148 from smacker/145
Browse files Browse the repository at this point in the history
fix #145 `contains` does not fail on string input
  • Loading branch information
lelandrichardson committed Feb 1, 2016
2 parents 15b47cc + f1e5b91 commit bd375b7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
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' && typeof a !== 'number') {
return leftKeys.length === Object.keys(right).length;
}

return false;
}

// 'click' => 'onClick'
Expand Down
32 changes: 32 additions & 0 deletions src/__tests__/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,38 @@ 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);
expect(wrapper.contains('1')).to.equal(false);
});

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

expect(wrapper.contains('foo')).to.equal(true);
expect(wrapper.contains(<div>foo</div>)).to.equal(true);

expect(wrapper.contains(5)).to.equal(true);
expect(wrapper.contains(<div>{5}</div>)).to.equal(true);
});

});

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

0 comments on commit bd375b7

Please sign in to comment.