Skip to content

Commit 7fcd96e

Browse files
krawallerljharb
authored andcommitted
[New] shallow/mount: allow .exists() to take an optional selector
1 parent 0d446f4 commit 7fcd96e

File tree

8 files changed

+87
-25
lines changed

8 files changed

+87
-25
lines changed

docs/api/ReactWrapper/exists.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
# `.exists() => Boolean`
1+
# `.exists([selector]) => Boolean`
2+
3+
Returns whether or not the current node exists. Or, if a selector is passed in, whether that selector has any matching results.
4+
5+
6+
7+
#### Arguments
8+
9+
1. `selector` ([`EnzymeSelector`](../selector.md) [optional]): The selector to check existence for.
210

3-
Returns whether or not the current node exists.
411

512

613
#### Returns
714

8-
`Boolean`: whether or not the current node exists.
15+
`Boolean`: whether or not the current node exists, or the selector had any results.
916

1017

1118

@@ -14,5 +21,6 @@ Returns whether or not the current node exists.
1421

1522
```jsx
1623
const wrapper = mount(<div className="some-class" />);
24+
expect(wrapper.exists('.some-class')).to.equal(true);
1725
expect(wrapper.find('.other-class').exists()).to.equal(false);
1826
```

docs/api/ShallowWrapper/exists.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
# `.exists() => Boolean`
1+
# `.exists([selector]) => Boolean`
2+
3+
Returns whether or not the current node exists. Or, if a selector is passed in, whether that selector has any matching results.
4+
5+
6+
7+
#### Arguments
8+
9+
1. `selector` ([`EnzymeSelector`](../selector.md) [optional]): The selector to check existence for.
210

3-
Returns whether or not the current node exists.
411

512

613
#### Returns
714

8-
`Boolean`: whether or not the current node exists.
15+
`Boolean`: whether or not the current node exists, or the selector had any results.
916

1017

1118

1219
#### Example
1320

1421

1522
```jsx
16-
const wrapper = shallow(<div className="some-class" />);
23+
const wrapper = mount(<div className="some-class" />);
24+
expect(wrapper.exists('.some-class')).to.equal(true);
1725
expect(wrapper.find('.other-class').exists()).to.equal(false);
1826
```

docs/api/mount.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ Returns whether or not the current root node has the given class name or not.
9090
#### [`.is(selector) => Boolean`](ReactWrapper/is.md)
9191
Returns whether or not the current node matches a provided selector.
9292

93-
#### [`.exists() => Boolean`](ReactWrapper/exists.md)
94-
Returns whether or not the current node exists.
93+
#### [`.exists([selector]) => Boolean`](ReactWrapper/exists.md)
94+
Returns whether or not the current node exists, or, if given a selector, whether that selector has any matching results.
9595

9696
#### [`.isEmpty() => Boolean`](ReactWrapper/isEmpty.md)
9797
*Deprecated*: Use [.exists()](ReactWrapper/exists.md) instead.

docs/api/shallow.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ Returns whether or not the current node has the given class name or not.
9797
#### [`.is(selector) => Boolean`](ShallowWrapper/is.md)
9898
Returns whether or not the current node matches a provided selector.
9999

100-
#### [`.exists() => Boolean`](ShallowWrapper/exists.md)
101-
Returns whether or not the current node exists.
100+
#### [`.exists([selector]) => Boolean`](ShallowWrapper/exists.md)
101+
Returns whether or not the current node exists, or, if given a selector, whether that selector has any matching results.
102102

103103
#### [`.isEmpty() => Boolean`](ShallowWrapper/isEmpty.md)
104104
*Deprecated*: Use [.exists()](ShallowWrapper/exists.md) instead.

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,10 +2943,28 @@ describeWithDOM('mount', () => {
29432943
});
29442944

29452945
describe('.exists()', () => {
2946-
it('should return true if node exists in wrapper', () => {
2947-
const wrapper = mount(<div className="foo" />);
2948-
expect(wrapper.find('.bar').exists()).to.equal(false);
2949-
expect(wrapper.find('.foo').exists()).to.equal(true);
2946+
it('has no required arguments', () => {
2947+
expect(ReactWrapper.prototype.exists).to.have.lengthOf(0);
2948+
});
2949+
2950+
describe('without arguments', () => {
2951+
it('should return true if node exists in wrapper', () => {
2952+
const wrapper = mount(<div className="foo" />);
2953+
expect(wrapper.find('.bar').exists()).to.equal(false);
2954+
expect(wrapper.find('.foo').exists()).to.equal(true);
2955+
});
2956+
});
2957+
describe('with argument', () => {
2958+
it('should return .find(arg).exists() instead', () => {
2959+
const wrapper = mount(<div />);
2960+
const fakeFindExistsReturnVal = Symbol('fake .find(arg).exists() return value');
2961+
const fakeSelector = '.someClass';
2962+
wrapper.find = sinon.stub().returns({ exists: () => fakeFindExistsReturnVal });
2963+
const existsResult = wrapper.exists(fakeSelector);
2964+
expect(wrapper.find.callCount).to.equal(1);
2965+
expect(wrapper.find.firstCall.args[0]).to.equal(fakeSelector);
2966+
expect(existsResult).to.equal(fakeFindExistsReturnVal);
2967+
});
29502968
});
29512969
});
29522970

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2858,12 +2858,30 @@ describe('shallow', () => {
28582858
});
28592859

28602860
describe('.exists()', () => {
2861-
it('should return true if node exists in wrapper', () => {
2862-
const wrapper = shallow((
2863-
<div className="foo" />
2864-
));
2865-
expect(wrapper.find('.bar').exists()).to.equal(false);
2866-
expect(wrapper.find('.foo').exists()).to.equal(true);
2861+
it('has no required arguments', () => {
2862+
expect(ShallowWrapper.prototype.exists).to.have.lengthOf(0);
2863+
});
2864+
2865+
describe('without argument', () => {
2866+
it('should return true if node exists in wrapper', () => {
2867+
const wrapper = shallow((
2868+
<div className="foo" />
2869+
));
2870+
expect(wrapper.find('.bar').exists()).to.equal(false);
2871+
expect(wrapper.find('.foo').exists()).to.equal(true);
2872+
});
2873+
});
2874+
describe('with argument', () => {
2875+
it('should return .find(arg).exists() instead', () => {
2876+
const wrapper = shallow(<div />);
2877+
const fakeFindExistsReturnVal = Symbol('fake .find(arg).exists() return value');
2878+
const fakeSelector = '.someClass';
2879+
wrapper.find = sinon.stub().returns({ exists: () => fakeFindExistsReturnVal });
2880+
const existsResult = wrapper.exists(fakeSelector);
2881+
expect(wrapper.find.callCount).to.equal(1);
2882+
expect(wrapper.find.firstCall.args[0]).to.equal(fakeSelector);
2883+
expect(existsResult).to.equal(fakeFindExistsReturnVal);
2884+
});
28672885
});
28682886
});
28692887

packages/enzyme/src/ReactWrapper.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,11 +951,16 @@ class ReactWrapper {
951951

952952
/**
953953
* Returns true if the current wrapper has nodes. False otherwise.
954+
* If called with a selector it returns `.find(selector).exists()` instead.
954955
*
956+
* @param {String|Function} selector (optional)
955957
* @returns {boolean}
956958
*/
957-
exists() {
958-
return this.length > 0;
959+
exists(selector = null) {
960+
if (arguments.length > 0 && typeof selector !== 'string') {
961+
throw new TypeError('`selector` argument must be a string, if present.');
962+
}
963+
return typeof selector === 'string' ? this.find(selector).exists() : this.length > 0;
959964
}
960965

961966
/**

packages/enzyme/src/ShallowWrapper.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,11 +1120,16 @@ class ShallowWrapper {
11201120

11211121
/**
11221122
* Returns true if the current wrapper has nodes. False otherwise.
1123+
* If called with a selector it returns `.find(selector).exists()` instead.
11231124
*
1125+
* @param {String|Function} selector (optional)
11241126
* @returns {boolean}
11251127
*/
1126-
exists() {
1127-
return this.length > 0;
1128+
exists(selector = null) {
1129+
if (arguments.length > 0 && typeof selector !== 'string') {
1130+
throw new TypeError('`selector` argument must be a string, if present.');
1131+
}
1132+
return typeof selector === 'string' ? this.find(selector).exists() : this.length > 0;
11281133
}
11291134

11301135
/**

0 commit comments

Comments
 (0)