Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate the referential equality of the args to componentDidUpdate #1198

Merged
merged 1 commit into from
Aug 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
140 changes: 140 additions & 0 deletions test/browser/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,146 @@ describe('Lifecycle methods', () => {
value: 4
});
});

it('prevState argument should be the same object if state doesn\'t change', () => {
let changeProps, cduPrevState, cduCurrentState;

class PropsProvider extends Component {
constructor() {
super();
this.state = { value: 0 };
changeProps = this.changeReceiverProps.bind(this);
}
changeReceiverProps() {
let value = (this.state.value + 1) % 2;
this.setState({
value
});
}
render() {
return <PropsReceiver value={this.state.value} />;
}
}

class PropsReceiver extends Component {
componentDidUpdate(prevProps, prevState) {
cduPrevState = prevState;
cduCurrentState = this.state;
}
render({ value }) {
return <div>{value}</div>;
}
}

render(<PropsProvider />, scratch);

changeProps();
rerender();

expect(cduPrevState).to.equal(cduCurrentState);
});

it('prevState argument should be a different object if state does change', () => {
let updateState, cduPrevState, cduCurrentState;

class Foo extends Component {
constructor() {
super();
this.state = { value: 0 };
updateState = this.updateState.bind(this);
}
updateState() {
let value = (this.state.value + 1) % 2;
this.setState({
value
});
}
componentDidUpdate(prevProps, prevState) {
cduPrevState = prevState;
cduCurrentState = this.state;
}
render() {
return <div>{this.state.value}</div>;
}
}

render(<Foo />, scratch);

updateState();
rerender();

expect(cduPrevState).to.not.equal(cduCurrentState);
});

it('prevProps argument should be the same object if props don\'t change', () => {
let updateState, cduPrevProps, cduCurrentProps;

class Foo extends Component {
constructor() {
super();
this.state = { value: 0 };
updateState = this.updateState.bind(this);
}
updateState() {
let value = (this.state.value + 1) % 2;
this.setState({
value
});
}
componentDidUpdate(prevProps) {
cduPrevProps = prevProps;
cduCurrentProps = this.props;
}
render() {
return <div>{this.state.value}</div>;
}
}

render(<Foo />, scratch);

updateState();
rerender();

expect(cduPrevProps).to.equal(cduCurrentProps);
});

it('prevProps argument should be a different object if props do change', () => {
let changeProps, cduPrevProps, cduCurrentProps;

class PropsProvider extends Component {
constructor() {
super();
this.state = { value: 0 };
changeProps = this.changeReceiverProps.bind(this);
}
changeReceiverProps() {
let value = (this.state.value + 1) % 2;
this.setState({
value
});
}
render() {
return <PropsReceiver value={this.state.value} />;
}
}

class PropsReceiver extends Component {
componentDidUpdate(prevProps) {
cduPrevProps = prevProps;
cduCurrentProps = this.props;
}
render({ value }) {
return <div>{value}</div>;
}
}

render(<PropsProvider />, scratch);

changeProps();
rerender();

expect(cduPrevProps).to.not.equal(cduCurrentProps);
});
});


Expand Down