Skip to content

Commit

Permalink
Fix .exists not accepting any EnzymeSelector
Browse files Browse the repository at this point in the history
`.exists`, since [enzymejs#1695](enzymejs#1695), accepts an optional selector. Unfortunately, this optional parameter was limited to the `string` type whereas the documentation inform us it can be any valid [EnzymeSelector](https://airbnb.io/enzyme/docs/api/selector.html).
This commit fixes this limitation by accepting any type for the parameter and using `.find()` internal functions to throw any necessary `TypeError`.
  • Loading branch information
btimo committed Dec 10, 2018
1 parent a3b745d commit a3568c0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* [equals(node)](/docs/api/ShallowWrapper/equals.md)
* [every(selector)](/docs/api/ShallowWrapper/every.md)
* [everyWhere(predicate)](/docs/api/ShallowWrapper/everyWhere.md)
* [exists()](/docs/api/ShallowWrapper/exists.md)
* [exists([selector])](/docs/api/ShallowWrapper/exists.md)
* [filter(selector)](/docs/api/ShallowWrapper/filter.md)
* [filterWhere(predicate)](/docs/api/ShallowWrapper/filterWhere.md)
* [find(selector)](/docs/api/ShallowWrapper/find.md)
Expand Down Expand Up @@ -91,7 +91,7 @@
* [detach()](/docs/api/ReactWrapper/detach.md)
* [every(selector)](/docs/api/ReactWrapper/every.md)
* [everyWhere(predicate)](/docs/api/ReactWrapper/everyWhere.md)
* [exists()](/docs/api/ReactWrapper/exists.md)
* [exists([selector])](/docs/api/ReactWrapper/exists.md)
* [filter(selector)](/docs/api/ReactWrapper/filter.md)
* [filterWhere(predicate)](/docs/api/ReactWrapper/filterWhere.md)
* [find(selector)](/docs/api/ReactWrapper/find.md)
Expand Down
10 changes: 9 additions & 1 deletion packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import isEqual from 'lodash.isequal';
import {
mount,
render,
ReactWrapper,
ReactWrapper, shallow,
} from 'enzyme';
import {
ITERATOR_SYMBOL,
Expand Down Expand Up @@ -4753,6 +4753,14 @@ describeWithDOM('mount', () => {
});
});
describe('with argument', () => {
it('throws on invalid EnzymeSelector', () => {
const wrapper = shallow(<div />);

expect(() => wrapper.exists(null)).to.throw(TypeError);
expect(() => wrapper.exists(undefined)).to.throw(TypeError);
expect(() => wrapper.exists(45)).to.throw(TypeError);
expect(() => wrapper.exists({})).to.throw(TypeError);
});
it('returns .find(arg).exists() instead', () => {
const wrapper = mount(<div />);
const fakeFindExistsReturnVal = Symbol('fake .find(arg).exists() return value');
Expand Down
9 changes: 9 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4553,6 +4553,15 @@ describe('shallow', () => {
});
});
describe('with argument', () => {
it('throws on invalid EnzymeSelector', () => {
const wrapper = shallow(<div />);

expect(() => wrapper.exists(null)).to.throw(TypeError);
expect(() => wrapper.exists(undefined)).to.throw(TypeError);
expect(() => wrapper.exists(45)).to.throw(TypeError);
expect(() => wrapper.exists({})).to.throw(TypeError);
});

it('returns .find(arg).exists() instead', () => {
const wrapper = shallow(<div />);
const fakeFindExistsReturnVal = Symbol('fake .find(arg).exists() return value');
Expand Down
5 changes: 1 addition & 4 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1077,10 +1077,7 @@ class ReactWrapper {
* @returns {boolean}
*/
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;
return arguments.length > 0 ? this.find(selector).exists() : this.length > 0;
}

/**
Expand Down
9 changes: 3 additions & 6 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ class ShallowWrapper {
/**
* Finds every node in the render tree of the current wrapper that matches the provided selector.
*
* @param {String|Function} selector
* @param {String|Function|Object} selector
* @returns {ShallowWrapper}
*/
find(selector) {
Expand Down Expand Up @@ -1329,14 +1329,11 @@ 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)
* @param {String|Function|Object} selector (optional)
* @returns {boolean}
*/
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;
return arguments.length > 0 ? this.find(selector).exists() : this.length > 0;
}

/**
Expand Down

0 comments on commit a3568c0

Please sign in to comment.