Skip to content

Commit

Permalink
[Tests] improve throwing assertions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 6, 2016
1 parent 8722bd2 commit cd63168
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 28 deletions.
57 changes: 46 additions & 11 deletions test/ReactWrapper-spec.js
Expand Up @@ -64,7 +64,7 @@ describeWithDOM('mount', () => {
});

const context = { name: 'foo' };
expect(() => mount(<SimpleComponent />, { context })).to.not.throw(Error);
expect(() => mount(<SimpleComponent />, { context })).to.not.throw();
});

it('is instrospectable through context API', () => {
Expand Down Expand Up @@ -121,7 +121,7 @@ describeWithDOM('mount', () => {
);

const context = { name: 'foo' };
expect(() => mount(<SimpleComponent />, { context })).to.not.throw(Error);
expect(() => mount(<SimpleComponent />, { context })).to.not.throw();
});

it('is instrospectable through context API', () => {
Expand Down Expand Up @@ -559,9 +559,18 @@ describeWithDOM('mount', () => {
<input className="foo" type="text" />
</div>
);
expect(() => wrapper.find({})).to.throw(Error);
expect(() => wrapper.find([])).to.throw(Error);
expect(() => wrapper.find(null)).to.throw(Error);
expect(() => wrapper.find({})).to.throw(
TypeError,
'Enzyme::Selector does not support an array, null, or empty object as a selector'
);
expect(() => wrapper.find([])).to.throw(
TypeError,
'Enzyme::Selector does not support an array, null, or empty object as a selector'
);
expect(() => wrapper.find(null)).to.throw(
TypeError,
'Enzyme::Selector does not support an array, null, or empty object as a selector'
);
});

it('Should query attributes with spaces in their values', () => {
Expand Down Expand Up @@ -916,6 +925,14 @@ describeWithDOM('mount', () => {
}
}

const similarException = ((() => {
try {
return undefined.givenName;
} catch (e) {
return e;
}
})());

const validUser = {
name: {
givenName: 'Brian',
Expand All @@ -930,7 +947,7 @@ describeWithDOM('mount', () => {
});
};

expect(setInvalidProps).to.throw();
expect(setInvalidProps).to.throw(TypeError, similarException.message);
});

describeIf(!REACT013, 'stateless function components', () => {
Expand Down Expand Up @@ -975,6 +992,14 @@ describeWithDOM('mount', () => {
},
};

const similarException = ((() => {
try {
return undefined.givenName;
} catch (e) {
return e;
}
})());

const wrapper = mount(<Trainwreck user={validUser} />);

const setInvalidProps = () => {
Expand All @@ -983,7 +1008,10 @@ describeWithDOM('mount', () => {
});
};

expect(setInvalidProps).to.throw();
expect(setInvalidProps).to.throw(
TypeError,
similarException.message
);
});
});
});
Expand All @@ -1008,7 +1036,7 @@ describeWithDOM('mount', () => {
expect(wrapper.text()).to.equal('baz');
});

it('should throw if it is called when shallow didnt include context', () => {
it('should throw if it is called when shallow didn’t include context', () => {
const SimpleComponent = React.createClass({
contextTypes: {
name: React.PropTypes.string,
Expand All @@ -1019,7 +1047,10 @@ describeWithDOM('mount', () => {
});

const wrapper = mount(<SimpleComponent />);
expect(() => wrapper.setContext({ name: 'bar' })).to.throw(Error);
expect(() => wrapper.setContext({ name: 'bar' })).to.throw(
Error,
'ShallowWrapper::setContext() can only be called on a wrapper that was originally passed a context option' // eslint-disable-line max-len
);
});

describeIf(!REACT013, 'stateless function components', () => {
Expand All @@ -1038,14 +1069,17 @@ describeWithDOM('mount', () => {
expect(wrapper.text()).to.equal('baz');
});

it('should throw if it is called when shallow didnt include context', () => {
it('should throw if it is called when shallow didn’t include context', () => {
const SimpleComponent = (props, context) => (
<div>{context.name}</div>
);
SimpleComponent.contextTypes = { name: React.PropTypes.string };

const wrapper = mount(<SimpleComponent />);
expect(() => wrapper.setContext({ name: 'bar' })).to.throw(Error);
expect(() => wrapper.setContext({ name: 'bar' })).to.throw(
Error,
'ShallowWrapper::setContext() can only be called on a wrapper that was originally passed a context option' // eslint-disable-line max-len
);
});
});
});
Expand Down Expand Up @@ -2122,6 +2156,7 @@ describeWithDOM('mount', () => {
</div>
);
expect(() => wrapper.some('.foo')).to.throw(
Error,
'ReactWrapper::some() can not be called on the root'
);
});
Expand Down
68 changes: 51 additions & 17 deletions test/ShallowWrapper-spec.js
Expand Up @@ -31,7 +31,7 @@ describe('shallow', () => {
});

const context = { name: 'foo' };
expect(() => shallow(<SimpleComponent />, { context })).to.not.throw(Error);
expect(() => shallow(<SimpleComponent />, { context })).to.not.throw();
});

it('is instrospectable through context API', () => {
Expand Down Expand Up @@ -121,7 +121,10 @@ describe('shallow', () => {
const wrapper = shallow(<Foo />);
const div = wrapper.find('div');

expect(() => div.instance()).to.throw();
expect(() => div.instance()).to.throw(
Error,
'ShallowWrapper::instance() can only be called on the root'
);
});

});
Expand Down Expand Up @@ -221,8 +224,14 @@ describe('shallow', () => {
it('should throw on invalid argument', () => {
const wrapper = shallow(<div></div>);

expect(() => wrapper.contains({})).to.throw();
expect(() => wrapper.contains(() => ({}))).to.throw();
expect(() => wrapper.contains({})).to.throw(
Error,
'ShallowWrapper::contains() can only be called with ReactElement (or array of them), string or number as argument.' // eslint-disable-line max-len
);
expect(() => wrapper.contains(() => ({}))).to.throw(
Error,
'ShallowWrapper::contains() can only be called with ReactElement (or array of them), string or number as argument.' // eslint-disable-line max-len
);
});

describeIf(!REACT013, 'stateless function components', () => {
Expand Down Expand Up @@ -443,7 +452,10 @@ describe('shallow', () => {
</div>
);

expect(() => wrapper.find('[type=text]')).to.throw();
expect(() => wrapper.find('[type=text]')).to.throw(
TypeError,
'Enzyme::Unable to parse selector \'[type=text]\'. Perhaps you forgot to escape a string? Try \'[type="text"]\' instead.' // eslint-disable-line max-len
);
});

it('should compound tag and prop selector', () => {
Expand Down Expand Up @@ -570,9 +582,18 @@ describe('shallow', () => {
<input className="foo" type="text" />
</div>
);
expect(() => wrapper.find({})).to.throw(Error);
expect(() => wrapper.find([])).to.throw(Error);
expect(() => wrapper.find(null)).to.throw(Error);
expect(() => wrapper.find({})).to.throw(
TypeError,
'Enzyme::Selector does not support an array, null, or empty object as a selector'
);
expect(() => wrapper.find([])).to.throw(
TypeError,
'Enzyme::Selector does not support an array, null, or empty object as a selector'
);
expect(() => wrapper.find(null)).to.throw(
TypeError,
'Enzyme::Selector does not support an array, null, or empty object as a selector'
);
});

describeIf(!REACT013, 'stateless function components', () => {
Expand Down Expand Up @@ -858,12 +879,15 @@ describe('shallow', () => {
expect(wrapper.text()).to.equal('baz');
});

it('should throw if it is called when shallow didnt include context', () => {
it('should throw if it is called when shallow didn’t include context', () => {
const wrapper = shallow(<SimpleComponent />);
expect(() => wrapper.setContext({ name: 'bar' })).to.throw(Error);
expect(() => wrapper.setContext({ name: 'bar' })).to.throw(
Error,
'ShallowWrapper::setContext() can only be called on a wrapper that was originally passed a context option' // eslint-disable-line max-len
);
});

describeIf(!REACT013, 'stateless function components', () => {
describeIf(!REACT013, 'stateless functional components', () => {
const SFC = (props, context) => (
<div>{context.name}</div>
);
Expand All @@ -879,9 +903,12 @@ describe('shallow', () => {
expect(wrapper.text()).to.equal('baz');
});

it('should throw if it is called when shallow didnt include context', () => {
it('should throw if it is called when shallow didn’t include context', () => {
const wrapper = shallow(<SFC />);
expect(() => wrapper.setContext({ name: 'bar' })).to.throw(Error);
expect(() => wrapper.setContext({ name: 'bar' })).to.throw(
Error,
'ShallowWrapper::setContext() can only be called on a wrapper that was originally passed a context option' // eslint-disable-line max-len
);
});
});
});
Expand Down Expand Up @@ -1079,7 +1106,10 @@ describe('shallow', () => {

const wrapper = shallow(<Foo />);

expect(() => wrapper.state()).to.throw();
expect(() => wrapper.state()).to.throw(
Error,
'ShallowWrapper::state() can only be called on class components'
);
});

it('should throw when trying to set state', () => {
Expand All @@ -1089,7 +1119,10 @@ describe('shallow', () => {

const wrapper = shallow(<Foo />);

expect(() => wrapper.setState({ a: 1 })).to.throw();
expect(() => wrapper.setState({ a: 1 })).to.throw(
Error,
'ShallowWrapper::setState() can only be called on class components'
);
});
});
});
Expand Down Expand Up @@ -2005,6 +2038,7 @@ describe('shallow', () => {
</div>
);
expect(() => wrapper.some('.foo')).to.throw(
Error,
'ShallowWrapper::some() can not be called on the root'
);
});
Expand Down Expand Up @@ -2156,7 +2190,7 @@ describe('shallow', () => {

const context = { name: 'foo' };
const wrapper = shallow(<Foo />);
expect(() => wrapper.find(Bar).shallow({ context })).to.not.throw(Error);
expect(() => wrapper.find(Bar).shallow({ context })).to.not.throw();
});

it('is instrospectable through context API', () => {
Expand Down Expand Up @@ -2234,7 +2268,7 @@ describe('shallow', () => {

const context = { name: 'foo' };
const wrapper = shallow(<Foo />);
expect(() => wrapper.find(Bar).shallow({ context })).to.not.throw(Error);
expect(() => wrapper.find(Bar).shallow({ context })).to.not.throw();
});

it('is instrospectable through context API', () => {
Expand Down

0 comments on commit cd63168

Please sign in to comment.