Skip to content

Commit

Permalink
[Tests] .setProps: add missing shallow/mount tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 17, 2018
1 parent e6287cc commit 20bfdf6
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 2 deletions.
74 changes: 72 additions & 2 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@ describeWithDOM('mount', () => {
]);
});

itIf(!REACT16, 'should throw if an exception occurs during render', () => {
it('should throw if an exception occurs during render', () => {
class Trainwreck extends React.Component {
render() {
const { user } = this.props;
Expand Down Expand Up @@ -1413,6 +1413,62 @@ describeWithDOM('mount', () => {
});
});

it('should call componentWillReceiveProps, shouldComponentUpdate, componentWillUpdate, and componentDidUpdate with merged newProps', () => {
const spy = sinon.spy();

class Foo extends React.Component {
componentWillReceiveProps(nextProps) {
spy('componentWillReceiveProps', this.props, nextProps);
}

shouldComponentUpdate(nextProps) {
spy('shouldComponentUpdate', this.props, nextProps);
return true;
}

componentWillUpdate(nextProps) {
spy('componentWillUpdate', this.props, nextProps);
}

componentDidUpdate(prevProps) {
spy('componentDidUpdate', prevProps, this.props);
}

render() {
return (
<div />
);
}
}

const wrapper = mount(<Foo a="a" b="b" />);

wrapper.setProps({ b: 'c', d: 'e' });

expect(spy.args).to.deep.equal([
[
'componentWillReceiveProps',
{ a: 'a', b: 'b' },
{ a: 'a', b: 'c', d: 'e' },
],
[
'shouldComponentUpdate',
{ a: 'a', b: 'b' },
{ a: 'a', b: 'c', d: 'e' },
],
[
'componentWillUpdate',
{ a: 'a', b: 'b' },
{ a: 'a', b: 'c', d: 'e' },
],
[
'componentDidUpdate',
{ a: 'a', b: 'b' },
{ a: 'a', b: 'c', d: 'e' },
],
]);
});

describeIf(is('> 0.13'), 'stateless function components', () => {
it('should set props for a component multiple times', () => {
const Foo = props => (
Expand Down Expand Up @@ -1445,7 +1501,21 @@ describeWithDOM('mount', () => {
expect(wrapper.props().d).to.equal('e');
});

itIf(!REACT16, 'should throw if an exception occurs during render', () => {
it('should pass in old context', () => {
const Foo = (props, context) => (
<div>{context.x}</div>
);
Foo.contextTypes = { x: PropTypes.string };

const context = { x: 'yolo' };
const wrapper = mount(<Foo x={5} />, { context });
expect(wrapper.first('div').text()).to.equal('yolo');

wrapper.setProps({ x: 5 }); // Just force a re-render
expect(wrapper.first('div').text()).to.equal('yolo');
});

it('should throw if an exception occurs during render', () => {
const Trainwreck = ({ user }) => (
<div>
{user.name.givenName}
Expand Down
96 changes: 96 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,66 @@ describe('shallow', () => {
]);
});

it('should throw if an exception occurs during render', () => {
class Trainwreck extends React.Component {
render() {
const { user } = this.props;
return (
<div>
{user.name.givenName}
</div>
);
}
}

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

const validUser = {
name: {
givenName: 'Brian',
},
};

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

const setInvalidProps = () => {
wrapper.setProps({
user: {},
});
};

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

it('should call the callback when setProps has completed', () => {
class Foo extends React.Component {
render() {
const { id } = this.props;
return (
<div className={id}>
{id}
</div>
);
}
}
const wrapper = shallow(<Foo id="foo" />);
expect(wrapper.find('.foo')).to.have.lengthOf(1);

wrapper[sym('__renderer__')].batchedUpdates(() => {
wrapper.setProps({ id: 'bar', foo: 'bla' }, () => {
expect(wrapper.find('.bar')).to.have.lengthOf(1);
});
expect(wrapper.find('.bar')).to.have.lengthOf(0);
});
});

it('should call componentWillReceiveProps, shouldComponentUpdate, componentWillUpdate, and componentDidUpdate with merged newProps', () => {
const spy = sinon.spy();

Expand Down Expand Up @@ -1341,6 +1401,42 @@ describe('shallow', () => {
wrapper.setProps({ x: 5 }); // Just force a re-render
expect(wrapper.first('div').text()).to.equal('yolo');
});

it('should throw if an exception occurs during render', () => {
const Trainwreck = ({ user }) => (
<div>
{user.name.givenName}
</div>
);

const validUser = {
name: {
givenName: 'Brian',
},
};

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

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

const setInvalidProps = () => {
wrapper.setProps({
user: {},
});
};

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

Expand Down

0 comments on commit 20bfdf6

Please sign in to comment.