Skip to content

Commit

Permalink
Added tests to createReactClassIntegration for new lifecycles
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn committed Jan 17, 2018
1 parent 09c39d0 commit 286df77
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion packages/react/src/__tests__/createReactClassIntegration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ describe('create-react-class-integration', () => {
);
});

// TODO (getDerivedStateFromProps) Reenable after create-react-class updated.
xit('should warn when misspelling UNSAFE_componentWillReceiveProps', () => {
expect(() =>
createReactClass({
UNSAFE_componentWillRecieveProps: function() {
return false;
},
render: function() {
return <div />;
},
}),
).toWarnDev(
'Warning: A component has a method called UNSAFE_componentWillRecieveProps(). ' +
'Did you mean UNSAFE_componentWillReceiveProps()?',
);
});

it('should throw if a reserved property is in statics', () => {
expect(function() {
createReactClass({
Expand All @@ -175,7 +192,6 @@ describe('create-react-class-integration', () => {
});

// TODO: Consider actually moving these to statics or drop this unit test.

xit('should warn when using deprecated non-static spec keys', () => {
expect(() =>
createReactClass({
Expand Down Expand Up @@ -251,6 +267,20 @@ describe('create-react-class-integration', () => {
expect(instance.state.occupation).toEqual('clown');
});

it('should work with getDerivedStateFromProps() return values', () => {
const Component = createReactClass({
render: function() {
return <span />;
},
});
Component.getDerivedStateFromProps = () => ({
occupation: 'clown',
});
let instance = <Component />;
instance = ReactTestUtils.renderIntoDocument(instance);
expect(instance.state.occupation).toEqual('clown');
});

it('renders based on context getInitialState', () => {
const Foo = createReactClass({
contextTypes: {
Expand Down Expand Up @@ -346,6 +376,34 @@ describe('create-react-class-integration', () => {
expect(ops).toEqual(['Render: 0', 'Render: 1', 'Callback: 1']);
});

it('getDerivedStateFromProps updates state when props change', () => {
const Component = createReactClass({
render() {
return <div>count:{this.state.count}</div>;
},
});
Component.getDerivedStateFromProps = (nextProps, prevState) =>
prevState === null
? {count: 1}
: {count: prevState.count + nextProps.incrementBy};

const container = document.createElement('div');
const instance = ReactDOM.render(
<div>
<Component />
</div>,
container,
);
expect(instance.textContent).toEqual('count:1');
ReactDOM.render(
<div>
<Component incrementBy={2} />
</div>,
container,
);
expect(instance.textContent).toEqual('count:3');
});

it('isMounted works', () => {
const ops = [];
let instance;
Expand Down

0 comments on commit 286df77

Please sign in to comment.