Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New API change: Delegate isEmpty() to exists() and begin deprecation of using isEmpty() #722

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion book.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"plugins": [
"edit-link",
"prism",
"prism@2.0.0",
"-highlight",
"github",
"-search",
Expand Down
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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)
* [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 @@ -82,6 +83,7 @@
* [debug()](/docs/api/ReactWrapper/debug.md)
* [detach()](/docs/api/ReactWrapper/detach.md)
* [every(selector)](/docs/api/ReactWrapper/every.md)
* [exists()](/docs/api/ReactWrapper/exists.md)
* [everyWhere(predicate)](/docs/api/ReactWrapper/everyWhere.md)
* [filter(selector)](/docs/api/ReactWrapper/filter.md)
* [filterWhere(predicate)](/docs/api/ReactWrapper/filterWhere.md)
Expand Down
18 changes: 18 additions & 0 deletions docs/api/ReactWrapper/exists.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# `.exists() => Boolean`

Returns whether or not the current node exists.


#### Returns

`Boolean`: whether or not the current node exists.



#### Example


```jsx
const wrapper = mount(<div className="some-class" />);
expect(wrapper.find('.other-class').exists()).to.be(false);
```
1 change: 1 addition & 0 deletions docs/api/ReactWrapper/isEmpty.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# `.isEmpty() => Boolean`
**Deprecated**: Use [.exists()](exists.md) instead.

Returns whether or not the current node is empty.

Expand Down
18 changes: 18 additions & 0 deletions docs/api/ShallowWrapper/exists.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# `.exists() => Boolean`

Returns whether or not the current node exists.


#### Returns

`Boolean`: whether or not the current node exists.



#### Example


```jsx
const wrapper = shallow(<div className="some-class" />);
expect(wrapper.find('.other-class').exists()).to.be(false);
```
1 change: 1 addition & 0 deletions docs/api/ShallowWrapper/isEmpty.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# `.isEmpty() => Boolean`
**Deprecated**: Use [.exists()](exists.md) instead.

Returns whether or not the current node is empty.

Expand Down
5 changes: 4 additions & 1 deletion docs/api/mount.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ 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.

#### [`.isEmpty() => Boolean`](ReactWrapper/isEmpty.md)
Returns whether or not the current node is empty.
*Deprecated*: Use [.exists()](ReactWrapper/exists.md) instead.

#### [`.not(selector) => ReactWrapper`](ReactWrapper/not.md)
Remove nodes in the current wrapper that match the provided selector. (inverse of `.filter()`)
Expand Down
5 changes: 4 additions & 1 deletion docs/api/shallow.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ 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.

#### [`.isEmpty() => Boolean`](ShallowWrapper/isEmpty.md)
Returns whether or not the current node is empty.
*Deprecated*: Use [.exists()](ShallowWrapper/exists.md) instead.

#### [`.not(selector) => ShallowWrapper`](ShallowWrapper/not.md)
Remove nodes in the current wrapper that match the provided selector. (inverse of `.filter()`)
Expand Down
15 changes: 13 additions & 2 deletions src/ReactWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -885,12 +885,23 @@ class ReactWrapper {
}

/**
* Returns true if the current wrapper has no nodes. False otherwise.
* Delegates to exists()
*
* @returns {boolean}
*/
isEmpty() {
return this.length === 0;
// eslint-disable-next-line no-console
console.warn('Enzyme::Deprecated method isEmpty() called, use exists() instead.');
return !this.exists();
}

/**
* Returns true if the current wrapper has nodes. False otherwise.
*
* @returns {boolean}
*/
exists() {
return this.length > 0;
}

/**
Expand Down
15 changes: 13 additions & 2 deletions src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,12 +942,23 @@ class ShallowWrapper {
}

/**
* Returns true if the current wrapper has no nodes. False otherwise.
* Delegates to exists()
*
* @returns {boolean}
*/
isEmpty() {
return this.length === 0;
// eslint-disable-next-line no-console
console.warn('Enzyme::Deprecated method isEmpty() called, use exists() instead.');
return !this.exists();
}

/**
* Returns true if the current wrapper has nodes. False otherwise.
*
* @returns {boolean}
*/
exists() {
return this.length > 0;
}

/**
Expand Down
43 changes: 39 additions & 4 deletions test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2464,12 +2464,47 @@ describeWithDOM('mount', () => {
});

describe('.isEmpty()', () => {
it('should return true iff wrapper is empty', () => {
let warningStub;
let fooNode;
let missingNode;

beforeEach(() => {
warningStub = sinon.stub(console, 'warn');
const wrapper = mount(
<div className="foo" />,
);
fooNode = wrapper.find('.foo');
missingNode = wrapper.find('.missing');
});
afterEach(() => {
warningStub.restore();
});

it('should display a deprecation warning', () => {
fooNode.isEmpty();
expect(warningStub.calledWith('Enzyme::Deprecated method isEmpty() called, use exists() instead.')).to.equal(true);
});

it('calls exists() instead', () => {
const existsSpy = sinon.spy();
fooNode.exists = existsSpy;
fooNode.isEmpty();
expect(existsSpy.called).to.equal(true);
});

it('should return true if wrapper is empty', () => {
expect(fooNode.isEmpty()).to.equal(false);
expect(missingNode.isEmpty()).to.equal(true);
});
});

describe('.exists()', () => {
it('should return true if node exists in wrapper', () => {
const wrapper = mount(
<div className="foo" />,
);
expect(wrapper.find('.bar').isEmpty()).to.equal(true);
expect(wrapper.find('.foo').isEmpty()).to.equal(false);
expect(wrapper.find('.bar').exists()).to.equal(false);
expect(wrapper.find('.foo').exists()).to.equal(true);
});
});

Expand Down Expand Up @@ -3215,7 +3250,7 @@ describeWithDOM('mount', () => {
const ref = wrapper.ref('not-a-ref');

expect(ref.length).to.equal(0);
expect(ref.isEmpty()).to.equal(true);
expect(ref.exists()).to.equal(false);
});
});
});
Expand Down
41 changes: 38 additions & 3 deletions test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2474,12 +2474,47 @@ describe('shallow', () => {
});

describe('.isEmpty()', () => {
it('should return true iff wrapper is empty', () => {
let warningStub;
let fooNode;
let missingNode;

beforeEach(() => {
warningStub = sinon.stub(console, 'warn');
const wrapper = shallow(
<div className="foo" />,
);
fooNode = wrapper.find('.foo');
missingNode = wrapper.find('.missing');
});
afterEach(() => {
warningStub.restore();
});

it('should display a deprecation warning', () => {
fooNode.isEmpty();
expect(warningStub.calledWith('Enzyme::Deprecated method isEmpty() called, use exists() instead.')).to.equal(true);
});

it('calls exists() instead', () => {
const existsSpy = sinon.spy();
fooNode.exists = existsSpy;
fooNode.isEmpty();
expect(existsSpy.called).to.equal(true);
});

it('should return true if wrapper is empty', () => {
expect(fooNode.isEmpty()).to.equal(false);
expect(missingNode.isEmpty()).to.equal(true);
});
});

describe('.exists()', () => {
it('should return true if node exists in wrapper', () => {
const wrapper = shallow(
<div className="foo" />,
);
expect(wrapper.find('.bar').isEmpty()).to.equal(true);
expect(wrapper.find('.foo').isEmpty()).to.equal(false);
expect(wrapper.find('.bar').exists()).to.equal(false);
expect(wrapper.find('.foo').exists()).to.equal(true);
});
});

Expand Down