Skip to content

Commit

Permalink
[Fix] shallow/mount: improve error message for "single node" asse…
Browse files Browse the repository at this point in the history
…rtion
  • Loading branch information
taylorkearns authored and ljharb committed Nov 14, 2018
1 parent 11d7015 commit 6661db6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
27 changes: 24 additions & 3 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7339,6 +7339,11 @@ describeWithDOM('mount', () => {
);
}
}
class TestZero extends React.Component {
render() {
return <div />;
}
}

it('returns the outermost DOMComponent of the root wrapper', () => {
const wrapper = mount(<Test />);
Expand All @@ -7354,7 +7359,23 @@ describeWithDOM('mount', () => {
const wrapper = mount(<Test />).find('span');
expect(() => wrapper.getDOMNode()).to.throw(
Error,
'Method “getDOMNode” is only meant to be run on a single node. 2 found instead.',
'Method “getDOMNode” is meant to be run on 1 node. 2 found instead.',
);
});

it('throws when wrapping zero elements', () => {
const wrapper = mount(<TestZero />).find('span');
expect(() => wrapper.getDOMNode()).to.throw(
Error,
'Method “getDOMNode” is meant to be run on 1 node. 0 found instead.',
);
});

it('throws when wrapping zero elements', () => {
const wrapper = mount(<TestZero />).find('span');
expect(() => wrapper.getDOMNode()).to.throw(
Error,
'Method “getDOMNode” is meant to be run on 1 node. 0 found instead.',
);
});

Expand Down Expand Up @@ -7382,7 +7403,7 @@ describeWithDOM('mount', () => {
const wrapper = mount(<SFC />).find('span');
expect(() => wrapper.getDOMNode()).to.throw(
Error,
'Method “getDOMNode” is only meant to be run on a single node. 2 found instead.',
'Method “getDOMNode” is meant to be run on 1 node. 2 found instead.',
);
});
});
Expand All @@ -7400,7 +7421,7 @@ describeWithDOM('mount', () => {
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.',
'Method “name!” is meant to be run on 1 node. 2 found instead.',
);
});

Expand Down
43 changes: 41 additions & 2 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6968,6 +6968,11 @@ describe('shallow', () => {
);
}
}
class RendersZero extends React.Component {
render() {
return <div />;
}
}
class WrapsRendersDOM extends React.Component {
render() {
return <RendersDOM />;
Expand Down Expand Up @@ -7010,7 +7015,23 @@ describe('shallow', () => {
const wrapper = shallow(<RendersMultiple />).find('div').children();
expect(() => { wrapper.dive(); }).to.throw(
Error,
'Method “dive” is only meant to be run on a single node. 2 found instead.',
'Method “dive” is meant to be run on 1 node. 2 found instead.',
);
});

it('throws on zero children found', () => {
const wrapper = shallow(<RendersZero />).find('div').children();
expect(() => { wrapper.dive(); }).to.throw(
Error,
'Method “dive” is meant to be run on 1 node. 0 found instead.',
);
});

it('throws on zero children found', () => {
const wrapper = shallow(<RendersZero />).find('div').children();
expect(() => { wrapper.dive(); }).to.throw(
Error,
'Method “dive” is meant to be run on 1 node. 0 found instead.',
);
});

Expand Down Expand Up @@ -7306,7 +7327,25 @@ describe('shallow', () => {
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.',
'Method “name!” is meant to be run on 1 node. 2 found instead.',
);
});

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

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

Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ class ReactWrapper {
const fnName = typeof name === 'string' ? name : 'unknown';
const callback = typeof fn === 'function' ? fn : name;
if (this.length !== 1) {
throw new Error(`Method “${fnName}” is only meant to be run on a single node. ${this.length} found instead.`);
throw new Error(`Method “${fnName}” is meant to be run on 1 node. ${this.length} found instead.`);
}
return callback.call(this, this.getNodeInternal());
}
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ class ShallowWrapper {
const fnName = typeof name === 'string' ? name : 'unknown';
const callback = typeof fn === 'function' ? fn : name;
if (this.length !== 1) {
throw new Error(`Method “${fnName}” is only meant to be run on a single node. ${this.length} found instead.`);
throw new Error(`Method “${fnName}” is meant to be run on 1 node. ${this.length} found instead.`);
}
return callback.call(this, this.getNodeInternal());
}
Expand Down

0 comments on commit 6661db6

Please sign in to comment.