Skip to content

Commit

Permalink
port test from #3884 functionality seems to work in v11
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jan 12, 2024
1 parent c73e072 commit 86f7898
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions test/browser/lifecycles/shouldComponentUpdate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Lifecycle methods', () => {
constructor() {
super();
this.state = { show: true };
setState = s => this.setState(s);
setState = (s) => this.setState(s);
}
render(props, { show }) {
return show ? <div /> : null;
Expand Down Expand Up @@ -106,7 +106,7 @@ describe('Lifecycle methods', () => {
<table>
{rows
.sort((a, b) => (a[sortBy] > b[sortBy] ? -1 : 1))
.map(row => (
.map((row) => (
<Row id={row.id} key={row.id} />
))}
</table>
Expand Down Expand Up @@ -612,7 +612,7 @@ describe('Lifecycle methods', () => {
constructor(props) {
super(props);
this.state = { hideMe: false };
hideThree = () => this.setState(s => ({ hideMe: !s.hideMe }));
hideThree = () => this.setState((s) => ({ hideMe: !s.hideMe }));
}

shouldComponentUpdate(nextProps, nextState) {
Expand All @@ -629,7 +629,7 @@ describe('Lifecycle methods', () => {
super(props);
this.state = { counter: 1 };
incrementThree = () =>
this.setState(s => ({ counter: s.counter + 1 }));
this.setState((s) => ({ counter: s.counter + 1 }));
}

render(p, { counter }) {
Expand Down Expand Up @@ -822,7 +822,7 @@ describe('Lifecycle methods', () => {
<table>
{rows
.sort((a, b) => (a[sortBy] > b[sortBy] ? -1 : 1))
.map(row => (
.map((row) => (
<Row key={row.id} id={row.id} a={row.a} b={row.b} />
))}
</table>
Expand Down Expand Up @@ -913,4 +913,51 @@ describe('Lifecycle methods', () => {
`<div>Before</div><div>Component</div><div>After</div>`
);
});

it('should correctly handle double state updates', () => {
let updateParent, updateChild;
class Parent extends Component {
state = { text: 'parent-old' };

componentDidMount() {
updateParent = () => this.setState({ text: 'Parent-NEW' });
}

render() {
return (
<Fragment>
{this.props.children} and {this.state.text}
</Fragment>
);
}
}

class Child extends Component {
state = { text: 'child-old' };

shouldComponentUpdate(nextProps, nextState) {
return this.state.text !== nextState.text;
}

componentDidMount() {
updateChild = () => this.setState({ text: 'Child-NEW' });
}

render() {
return <h1>{this.state.text}</h1>;
}
}

render(
<Parent>
<Child />
</Parent>,
scratch
);

updateParent();
updateChild();
rerender();
expect(scratch.innerHTML).to.equal('<h1>Child-NEW</h1> and Parent-NEW');
});
});

0 comments on commit 86f7898

Please sign in to comment.