Skip to content

Commit

Permalink
allow exists to take selector
Browse files Browse the repository at this point in the history
  • Loading branch information
krawaller committed Jun 29, 2018
1 parent 19001b4 commit 0235d60
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
22 changes: 18 additions & 4 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2879,10 +2879,24 @@ 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);
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
26 changes: 20 additions & 6 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2839,12 +2839,26 @@ 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);
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
6 changes: 4 additions & 2 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -948,11 +948,13 @@ 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) {
return selector !== undefined ? this.find(selector).exists() : this.length > 0;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1102,11 +1102,13 @@ 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) {
return selector !== undefined ? this.find(selector).exists() : this.length > 0;
}

/**
Expand Down

0 comments on commit 0235d60

Please sign in to comment.