Skip to content

Commit

Permalink
[New] shallow/mount: allow .exists() to take an optional selector
Browse files Browse the repository at this point in the history
  • Loading branch information
krawaller authored and ljharb committed Jun 30, 2018
1 parent 0d446f4 commit 7fcd96e
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 25 deletions.
14 changes: 11 additions & 3 deletions docs/api/ReactWrapper/exists.md
Original file line number Diff line number Diff line change
@@ -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.



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

```jsx
const wrapper = mount(<div className="some-class" />);
expect(wrapper.exists('.some-class')).to.equal(true);
expect(wrapper.find('.other-class').exists()).to.equal(false);
```
16 changes: 12 additions & 4 deletions docs/api/ShallowWrapper/exists.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
# `.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.



#### Example


```jsx
const wrapper = shallow(<div className="some-class" />);
const wrapper = mount(<div className="some-class" />);
expect(wrapper.exists('.some-class')).to.equal(true);
expect(wrapper.find('.other-class').exists()).to.equal(false);
```
4 changes: 2 additions & 2 deletions docs/api/mount.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions docs/api/shallow.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 22 additions & 4 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2943,10 +2943,28 @@ describeWithDOM('mount', () => {
});

describe('.exists()', () => {
it('should return true if node exists in wrapper', () => {
const wrapper = mount(<div className="foo" />);
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(<div className="foo" />);
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(<div />);
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);
});
});
});

Expand Down
30 changes: 24 additions & 6 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2858,12 +2858,30 @@ describe('shallow', () => {
});

describe('.exists()', () => {
it('should return true if node exists in wrapper', () => {
const wrapper = shallow((
<div className="foo" />
));
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((
<div className="foo" />
));
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(<div />);
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);
});
});
});

Expand Down
9 changes: 7 additions & 2 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
9 changes: 7 additions & 2 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 7fcd96e

Please sign in to comment.