Skip to content

Commit

Permalink
[Fix] continue to support one-argument single
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb authored and lelandrichardson committed Oct 17, 2016
1 parent 202f054 commit 54c11cf
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/ReactWrapper.jsx
Expand Up @@ -847,12 +847,14 @@ class ReactWrapper {
* @returns {*}
*/
single(name, fn) {
const fnName = typeof name === 'string' ? name : 'unknown';
const callback = typeof fn === 'function' ? fn : name;
if (this.length !== 1) {
throw new Error(
`Method “${name}” is only meant to be run on a single node. ${this.length} found instead.`
`Method “${fnName}” is only meant to be run on a single node. ${this.length} found instead.`
);
}
return fn.call(this, this.node);
return callback.call(this, this.node);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/ShallowWrapper.js
Expand Up @@ -922,12 +922,14 @@ class ShallowWrapper {
* @returns {*}
*/
single(name, fn) {
const fnName = typeof name === 'string' ? name : 'unknown';
const callback = typeof fn === 'function' ? fn : name;
if (this.length !== 1) {
throw new Error(
`Method “${name}” is only meant to be run on a single node. ${this.length} found instead.`
`Method “${fnName}” is only meant to be run on a single node. ${this.length} found instead.`
);
}
return fn.call(this, this.node);
return callback.call(this, this.node);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions test/ReactWrapper-spec.jsx
Expand Up @@ -3131,4 +3131,29 @@ describeWithDOM('mount', () => {
expect(d1).to.equal(d);
});
});

describe('#single()', () => {
it('throws if run on multiple nodes', () => {
const wrapper = mount(<div><i /><i /></div>).children();
expect(wrapper).to.have.lengthOf(2);
expect(() => wrapper.single('name!')).to.throw(
Error,
'Method “name!” is only meant to be run on a single node. 2 found instead.',
);
});

it('works with a name', () => {
const wrapper = mount(<div />);
wrapper.single('foo', (node) => {
expect(node).to.equal(wrapper.get(0));
});
});

it('works without a name', () => {
const wrapper = mount(<div />);
wrapper.single((node) => {
expect(node).to.equal(wrapper.get(0));
});
});
});
});
25 changes: 25 additions & 0 deletions test/ShallowWrapper-spec.jsx
Expand Up @@ -3717,4 +3717,29 @@ describe('shallow', () => {
expect(d1).to.equal(d);
});
});

describe('#single()', () => {
it('throws if run on multiple nodes', () => {
const wrapper = shallow(<div><i /><i /></div>).children();
expect(wrapper).to.have.lengthOf(2);
expect(() => wrapper.single('name!')).to.throw(
Error,
'Method “name!” is only meant to be run on a single node. 2 found instead.',
);
});

it('works with a name', () => {
const wrapper = shallow(<div />);
wrapper.single('foo', (node) => {
expect(node).to.equal(wrapper.get(0));
});
});

it('works without a name', () => {
const wrapper = shallow(<div />);
wrapper.single((node) => {
expect(node).to.equal(wrapper.get(0));
});
});
});
});

0 comments on commit 54c11cf

Please sign in to comment.