Skip to content

Commit a3568c0

Browse files
author
btimo
committed
Fix .exists not accepting any EnzymeSelector
`.exists`, since [#1695](#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`.
1 parent a3b745d commit a3568c0

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

SUMMARY.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* [equals(node)](/docs/api/ShallowWrapper/equals.md)
3434
* [every(selector)](/docs/api/ShallowWrapper/every.md)
3535
* [everyWhere(predicate)](/docs/api/ShallowWrapper/everyWhere.md)
36-
* [exists()](/docs/api/ShallowWrapper/exists.md)
36+
* [exists([selector])](/docs/api/ShallowWrapper/exists.md)
3737
* [filter(selector)](/docs/api/ShallowWrapper/filter.md)
3838
* [filterWhere(predicate)](/docs/api/ShallowWrapper/filterWhere.md)
3939
* [find(selector)](/docs/api/ShallowWrapper/find.md)
@@ -91,7 +91,7 @@
9191
* [detach()](/docs/api/ReactWrapper/detach.md)
9292
* [every(selector)](/docs/api/ReactWrapper/every.md)
9393
* [everyWhere(predicate)](/docs/api/ReactWrapper/everyWhere.md)
94-
* [exists()](/docs/api/ReactWrapper/exists.md)
94+
* [exists([selector])](/docs/api/ReactWrapper/exists.md)
9595
* [filter(selector)](/docs/api/ReactWrapper/filter.md)
9696
* [filterWhere(predicate)](/docs/api/ReactWrapper/filterWhere.md)
9797
* [find(selector)](/docs/api/ReactWrapper/find.md)

packages/enzyme-test-suite/test/ReactWrapper-spec.jsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import isEqual from 'lodash.isequal';
88
import {
99
mount,
1010
render,
11-
ReactWrapper,
11+
ReactWrapper, shallow,
1212
} from 'enzyme';
1313
import {
1414
ITERATOR_SYMBOL,
@@ -4753,6 +4753,14 @@ describeWithDOM('mount', () => {
47534753
});
47544754
});
47554755
describe('with argument', () => {
4756+
it('throws on invalid EnzymeSelector', () => {
4757+
const wrapper = shallow(<div />);
4758+
4759+
expect(() => wrapper.exists(null)).to.throw(TypeError);
4760+
expect(() => wrapper.exists(undefined)).to.throw(TypeError);
4761+
expect(() => wrapper.exists(45)).to.throw(TypeError);
4762+
expect(() => wrapper.exists({})).to.throw(TypeError);
4763+
});
47564764
it('returns .find(arg).exists() instead', () => {
47574765
const wrapper = mount(<div />);
47584766
const fakeFindExistsReturnVal = Symbol('fake .find(arg).exists() return value');

packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4553,6 +4553,15 @@ describe('shallow', () => {
45534553
});
45544554
});
45554555
describe('with argument', () => {
4556+
it('throws on invalid EnzymeSelector', () => {
4557+
const wrapper = shallow(<div />);
4558+
4559+
expect(() => wrapper.exists(null)).to.throw(TypeError);
4560+
expect(() => wrapper.exists(undefined)).to.throw(TypeError);
4561+
expect(() => wrapper.exists(45)).to.throw(TypeError);
4562+
expect(() => wrapper.exists({})).to.throw(TypeError);
4563+
});
4564+
45564565
it('returns .find(arg).exists() instead', () => {
45574566
const wrapper = shallow(<div />);
45584567
const fakeFindExistsReturnVal = Symbol('fake .find(arg).exists() return value');

packages/enzyme/src/ReactWrapper.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,10 +1077,7 @@ class ReactWrapper {
10771077
* @returns {boolean}
10781078
*/
10791079
exists(selector = null) {
1080-
if (arguments.length > 0 && typeof selector !== 'string') {
1081-
throw new TypeError('`selector` argument must be a string, if present.');
1082-
}
1083-
return typeof selector === 'string' ? this.find(selector).exists() : this.length > 0;
1080+
return arguments.length > 0 ? this.find(selector).exists() : this.length > 0;
10841081
}
10851082

10861083
/**

packages/enzyme/src/ShallowWrapper.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ class ShallowWrapper {
740740
/**
741741
* Finds every node in the render tree of the current wrapper that matches the provided selector.
742742
*
743-
* @param {String|Function} selector
743+
* @param {String|Function|Object} selector
744744
* @returns {ShallowWrapper}
745745
*/
746746
find(selector) {
@@ -1329,14 +1329,11 @@ class ShallowWrapper {
13291329
* Returns true if the current wrapper has nodes. False otherwise.
13301330
* If called with a selector it returns `.find(selector).exists()` instead.
13311331
*
1332-
* @param {String|Function} selector (optional)
1332+
* @param {String|Function|Object} selector (optional)
13331333
* @returns {boolean}
13341334
*/
13351335
exists(selector = null) {
1336-
if (arguments.length > 0 && typeof selector !== 'string') {
1337-
throw new TypeError('`selector` argument must be a string, if present.');
1338-
}
1339-
return typeof selector === 'string' ? this.find(selector).exists() : this.length > 0;
1336+
return arguments.length > 0 ? this.find(selector).exists() : this.length > 0;
13401337
}
13411338

13421339
/**

0 commit comments

Comments
 (0)