From 7fcd96e0c11fee4f4fe21131c62919f58b7dc168 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 29 Jun 2018 07:54:36 +0200 Subject: [PATCH] [New] `shallow`/`mount`: allow `.exists()` to take an optional selector --- docs/api/ReactWrapper/exists.md | 14 +++++++-- docs/api/ShallowWrapper/exists.md | 16 +++++++--- docs/api/mount.md | 4 +-- docs/api/shallow.md | 4 +-- .../test/ReactWrapper-spec.jsx | 26 +++++++++++++--- .../test/ShallowWrapper-spec.jsx | 30 +++++++++++++++---- packages/enzyme/src/ReactWrapper.js | 9 ++++-- packages/enzyme/src/ShallowWrapper.js | 9 ++++-- 8 files changed, 87 insertions(+), 25 deletions(-) diff --git a/docs/api/ReactWrapper/exists.md b/docs/api/ReactWrapper/exists.md index 961febf00..78dfe0d01 100644 --- a/docs/api/ReactWrapper/exists.md +++ b/docs/api/ReactWrapper/exists.md @@ -1,11 +1,18 @@ -# `.exists() => Boolean` +# `.exists([selector]) => Boolean` + +Returns whether or not the current node exists. Or, if a selector is passed in, whether that selector has any matching results. + + + +#### Arguments + +1. `selector` ([`EnzymeSelector`](../selector.md) [optional]): The selector to check existence for. -Returns whether or not the current node exists. #### Returns -`Boolean`: whether or not the current node exists. +`Boolean`: whether or not the current node exists, or the selector had any results. @@ -14,5 +21,6 @@ Returns whether or not the current node exists. ```jsx const wrapper = mount(
); +expect(wrapper.exists('.some-class')).to.equal(true); expect(wrapper.find('.other-class').exists()).to.equal(false); ``` diff --git a/docs/api/ShallowWrapper/exists.md b/docs/api/ShallowWrapper/exists.md index 74ba91dcc..78dfe0d01 100644 --- a/docs/api/ShallowWrapper/exists.md +++ b/docs/api/ShallowWrapper/exists.md @@ -1,11 +1,18 @@ -# `.exists() => Boolean` +# `.exists([selector]) => Boolean` + +Returns whether or not the current node exists. Or, if a selector is passed in, whether that selector has any matching results. + + + +#### Arguments + +1. `selector` ([`EnzymeSelector`](../selector.md) [optional]): The selector to check existence for. -Returns whether or not the current node exists. #### Returns -`Boolean`: whether or not the current node exists. +`Boolean`: whether or not the current node exists, or the selector had any results. @@ -13,6 +20,7 @@ Returns whether or not the current node exists. ```jsx -const wrapper = shallow(
); +const wrapper = mount(
); +expect(wrapper.exists('.some-class')).to.equal(true); expect(wrapper.find('.other-class').exists()).to.equal(false); ``` diff --git a/docs/api/mount.md b/docs/api/mount.md index 4f8ca6f24..5c6f4aae8 100644 --- a/docs/api/mount.md +++ b/docs/api/mount.md @@ -90,8 +90,8 @@ Returns whether or not the current root node has the given class name or not. #### [`.is(selector) => Boolean`](ReactWrapper/is.md) Returns whether or not the current node matches a provided selector. -#### [`.exists() => Boolean`](ReactWrapper/exists.md) -Returns whether or not the current node exists. +#### [`.exists([selector]) => Boolean`](ReactWrapper/exists.md) +Returns whether or not the current node exists, or, if given a selector, whether that selector has any matching results. #### [`.isEmpty() => Boolean`](ReactWrapper/isEmpty.md) *Deprecated*: Use [.exists()](ReactWrapper/exists.md) instead. diff --git a/docs/api/shallow.md b/docs/api/shallow.md index a998639b5..fa1389505 100644 --- a/docs/api/shallow.md +++ b/docs/api/shallow.md @@ -97,8 +97,8 @@ Returns whether or not the current node has the given class name or not. #### [`.is(selector) => Boolean`](ShallowWrapper/is.md) Returns whether or not the current node matches a provided selector. -#### [`.exists() => Boolean`](ShallowWrapper/exists.md) -Returns whether or not the current node exists. +#### [`.exists([selector]) => Boolean`](ShallowWrapper/exists.md) +Returns whether or not the current node exists, or, if given a selector, whether that selector has any matching results. #### [`.isEmpty() => Boolean`](ShallowWrapper/isEmpty.md) *Deprecated*: Use [.exists()](ShallowWrapper/exists.md) instead. diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx index 7725288c4..adba61aa1 100644 --- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx @@ -2943,10 +2943,28 @@ describeWithDOM('mount', () => { }); describe('.exists()', () => { - it('should return true if node exists in wrapper', () => { - const wrapper = mount(
); - expect(wrapper.find('.bar').exists()).to.equal(false); - expect(wrapper.find('.foo').exists()).to.equal(true); + it('has no required arguments', () => { + expect(ReactWrapper.prototype.exists).to.have.lengthOf(0); + }); + + describe('without arguments', () => { + it('should return true if node exists in wrapper', () => { + const wrapper = mount(
); + expect(wrapper.find('.bar').exists()).to.equal(false); + expect(wrapper.find('.foo').exists()).to.equal(true); + }); + }); + describe('with argument', () => { + it('should return .find(arg).exists() instead', () => { + const wrapper = mount(
); + const fakeFindExistsReturnVal = Symbol('fake .find(arg).exists() return value'); + const fakeSelector = '.someClass'; + wrapper.find = sinon.stub().returns({ exists: () => fakeFindExistsReturnVal }); + const existsResult = wrapper.exists(fakeSelector); + expect(wrapper.find.callCount).to.equal(1); + expect(wrapper.find.firstCall.args[0]).to.equal(fakeSelector); + expect(existsResult).to.equal(fakeFindExistsReturnVal); + }); }); }); diff --git a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx index 15a0e8779..8157d6bb8 100644 --- a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx @@ -2858,12 +2858,30 @@ describe('shallow', () => { }); describe('.exists()', () => { - it('should return true if node exists in wrapper', () => { - const wrapper = shallow(( -
- )); - expect(wrapper.find('.bar').exists()).to.equal(false); - expect(wrapper.find('.foo').exists()).to.equal(true); + it('has no required arguments', () => { + expect(ShallowWrapper.prototype.exists).to.have.lengthOf(0); + }); + + describe('without argument', () => { + it('should return true if node exists in wrapper', () => { + const wrapper = shallow(( +
+ )); + expect(wrapper.find('.bar').exists()).to.equal(false); + expect(wrapper.find('.foo').exists()).to.equal(true); + }); + }); + describe('with argument', () => { + it('should return .find(arg).exists() instead', () => { + const wrapper = shallow(
); + const fakeFindExistsReturnVal = Symbol('fake .find(arg).exists() return value'); + const fakeSelector = '.someClass'; + wrapper.find = sinon.stub().returns({ exists: () => fakeFindExistsReturnVal }); + const existsResult = wrapper.exists(fakeSelector); + expect(wrapper.find.callCount).to.equal(1); + expect(wrapper.find.firstCall.args[0]).to.equal(fakeSelector); + expect(existsResult).to.equal(fakeFindExistsReturnVal); + }); }); }); diff --git a/packages/enzyme/src/ReactWrapper.js b/packages/enzyme/src/ReactWrapper.js index ba02ec470..e1f2acf4b 100644 --- a/packages/enzyme/src/ReactWrapper.js +++ b/packages/enzyme/src/ReactWrapper.js @@ -951,11 +951,16 @@ class ReactWrapper { /** * Returns true if the current wrapper has nodes. False otherwise. + * If called with a selector it returns `.find(selector).exists()` instead. * + * @param {String|Function} selector (optional) * @returns {boolean} */ - exists() { - return this.length > 0; + exists(selector = null) { + if (arguments.length > 0 && typeof selector !== 'string') { + throw new TypeError('`selector` argument must be a string, if present.'); + } + return typeof selector === 'string' ? this.find(selector).exists() : this.length > 0; } /** diff --git a/packages/enzyme/src/ShallowWrapper.js b/packages/enzyme/src/ShallowWrapper.js index 0c1f07263..ce3c1e9bd 100644 --- a/packages/enzyme/src/ShallowWrapper.js +++ b/packages/enzyme/src/ShallowWrapper.js @@ -1120,11 +1120,16 @@ class ShallowWrapper { /** * Returns true if the current wrapper has nodes. False otherwise. + * If called with a selector it returns `.find(selector).exists()` instead. * + * @param {String|Function} selector (optional) * @returns {boolean} */ - exists() { - return this.length > 0; + exists(selector = null) { + if (arguments.length > 0 && typeof selector !== 'string') { + throw new TypeError('`selector` argument must be a string, if present.'); + } + return typeof selector === 'string' ? this.find(selector).exists() : this.length > 0; } /**