From 07dcd56adce211e707ea04b289254e1f8d22bf21 Mon Sep 17 00:00:00 2001 From: Sukharev Maxim Date: Fri, 29 Jan 2016 09:56:27 +0700 Subject: [PATCH 1/3] fix #145 `contains` does not fail on string input --- src/ShallowWrapper.js | 8 +++++++- src/Utils.js | 8 +++++++- src/__tests__/ShallowWrapper-spec.js | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/ShallowWrapper.js b/src/ShallowWrapper.js index 9e800f062..7e3ea0d2a 100644 --- a/src/ShallowWrapper.js +++ b/src/ShallowWrapper.js @@ -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; } /** diff --git a/src/Utils.js b/src/Utils.js index dc1401d85..b92b7b053 100644 --- a/src/Utils.js +++ b/src/Utils.js @@ -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); @@ -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' diff --git a/src/__tests__/ShallowWrapper-spec.js b/src/__tests__/ShallowWrapper-spec.js index 5a4698c3a..411ed74a3 100644 --- a/src/__tests__/ShallowWrapper-spec.js +++ b/src/__tests__/ShallowWrapper-spec.js @@ -83,6 +83,20 @@ describe('shallow', () => { expect(wrapper.contains(b)).to.equal(true); }); + it('should work with strings', () => { + const wrapper = shallow(
foo
); + + expect(wrapper.contains('foo')).to.equal(true); + expect(wrapper.contains('bar')).to.equal(false); + }); + + it('should work with numbers', () => { + const wrapper = shallow(
1
); + + expect(wrapper.contains(1)).to.equal(true); + expect(wrapper.contains(2)).to.equal(false); + }); + }); describe('.equals(node)', () => { From baddf12987cfd5b50a82165f4963a632fa57d621 Mon Sep 17 00:00:00 2001 From: Sukharev Maxim Date: Fri, 29 Jan 2016 11:40:57 +0700 Subject: [PATCH 2/3] fix numbers --- src/ShallowWrapper.js | 8 +------- src/Utils.js | 2 +- src/__tests__/ShallowWrapper-spec.js | 3 ++- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/ShallowWrapper.js b/src/ShallowWrapper.js index 7e3ea0d2a..9e800f062 100644 --- a/src/ShallowWrapper.js +++ b/src/ShallowWrapper.js @@ -202,13 +202,7 @@ export default class ShallowWrapper { * @returns {Boolean} */ contains(node) { - let nodeToCompare = node; - - if (typeof nodeToCompare === 'number') { - nodeToCompare = '' + nodeToCompare; - } - - return findWhereUnwrapped(this, other => nodeEqual(nodeToCompare, other)).length > 0; + return findWhereUnwrapped(this, other => nodeEqual(node, other)).length > 0; } /** diff --git a/src/Utils.js b/src/Utils.js index b92b7b053..f89897240 100644 --- a/src/Utils.js +++ b/src/Utils.js @@ -82,7 +82,7 @@ export function nodeEqual(a, b) { } } - if (typeof a !== 'string') { + if (typeof a !== 'string' && typeof a !== 'number') { return leftKeys.length === Object.keys(right).length; } diff --git a/src/__tests__/ShallowWrapper-spec.js b/src/__tests__/ShallowWrapper-spec.js index 411ed74a3..24b365fd9 100644 --- a/src/__tests__/ShallowWrapper-spec.js +++ b/src/__tests__/ShallowWrapper-spec.js @@ -91,10 +91,11 @@ describe('shallow', () => { }); it('should work with numbers', () => { - const wrapper = shallow(
1
); + const wrapper = shallow(
{1}
); expect(wrapper.contains(1)).to.equal(true); expect(wrapper.contains(2)).to.equal(false); + expect(wrapper.contains('1')).to.equal(false); }); }); From f1e5b917df0a6f4df2221b2d2ff7950834fade64 Mon Sep 17 00:00:00 2001 From: Sukharev Maxim Date: Mon, 1 Feb 2016 09:41:12 +0700 Subject: [PATCH 3/3] Add tests for nested string and numbers --- src/__tests__/ShallowWrapper-spec.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/__tests__/ShallowWrapper-spec.js b/src/__tests__/ShallowWrapper-spec.js index 24b365fd9..718df00c0 100644 --- a/src/__tests__/ShallowWrapper-spec.js +++ b/src/__tests__/ShallowWrapper-spec.js @@ -98,6 +98,23 @@ describe('shallow', () => { expect(wrapper.contains('1')).to.equal(false); }); + it('should work with nested strings & numbers', () => { + const wrapper = shallow( +
+
+
{5}
+
+
foo
+
+ ); + + expect(wrapper.contains('foo')).to.equal(true); + expect(wrapper.contains(
foo
)).to.equal(true); + + expect(wrapper.contains(5)).to.equal(true); + expect(wrapper.contains(
{5}
)).to.equal(true); + }); + }); describe('.equals(node)', () => {