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

fix #145 contains does not fail on string input #148

Merged
merged 3 commits into from
Feb 1, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb I just noticed this, but should we use a lib for Object.keys instead of assuming it's available?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's pretty safe to assume it's available.

Object.keys has really wide support. On the browser side you'd have to use less than IE9, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

On node side, it's been included since at least 0.10. I'm not sure exactly when it was added to V8, but I think being in 0.10 makes it more than safe to use natively

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's part of ES5 - node 0.8 and higher, and IE 9 and higher have it.

However, if you want to support older engines (which I strongly recommend!) you can use the https://npmjs.com/object-keys module :-)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose that since react requires ES5 and above, it is safe to conclude that at least an es5-shim will be loaded in any environment enzyme will be used...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

}

return false;
}

// 'click' => 'onClick'
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ 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', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a counter-test here, that in <div>1</div>, wrapper.contains('1') is false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh. Actually I did wrong comparison with numbers. Fixed it.

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);
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add another test or two that make sure this works properly for multiple nested levels?

const wrapper = shallow(
  <div>
    <div>
      <div>{5}</div>
    </div>
  <div>
);

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


});

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