From 86f7898a3fa0d08a8602d239ba2e8d001f471ee1 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Fri, 12 Jan 2024 08:04:53 +0100 Subject: [PATCH] port test from https://github.com/preactjs/preact/pull/3884 functionality seems to work in v11 --- .../lifecycles/shouldComponentUpdate.test.js | 57 +++++++++++++++++-- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/test/browser/lifecycles/shouldComponentUpdate.test.js b/test/browser/lifecycles/shouldComponentUpdate.test.js index 6b5f18709a..1376c19162 100644 --- a/test/browser/lifecycles/shouldComponentUpdate.test.js +++ b/test/browser/lifecycles/shouldComponentUpdate.test.js @@ -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 ?
: null; @@ -106,7 +106,7 @@ describe('Lifecycle methods', () => { {rows .sort((a, b) => (a[sortBy] > b[sortBy] ? -1 : 1)) - .map(row => ( + .map((row) => ( ))}
@@ -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) { @@ -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 }) { @@ -822,7 +822,7 @@ describe('Lifecycle methods', () => { {rows .sort((a, b) => (a[sortBy] > b[sortBy] ? -1 : 1)) - .map(row => ( + .map((row) => ( ))}
@@ -913,4 +913,51 @@ describe('Lifecycle methods', () => { `
Before
Component
After
` ); }); + + 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 ( + + {this.props.children} and {this.state.text} + + ); + } + } + + 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

{this.state.text}

; + } + } + + render( + + + , + scratch + ); + + updateParent(); + updateChild(); + rerender(); + expect(scratch.innerHTML).to.equal('

Child-NEW

and Parent-NEW'); + }); });