From d5c9d72d7908cf26aeeb667a99501c721632ecf9 Mon Sep 17 00:00:00 2001 From: Andre Wiggins <459878+andrewiggins@users.noreply.github.com> Date: Tue, 22 Nov 2022 18:48:59 -0800 Subject: [PATCH] Merge componentWillMount test from master (#3811) Cherry-picked from: Invoke setState callbacks setup in componentWillMount (#3806) --- .../lifecycles/componentWillMount.test.js | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/browser/lifecycles/componentWillMount.test.js b/test/browser/lifecycles/componentWillMount.test.js index 880803e84b..27b5c91212 100644 --- a/test/browser/lifecycles/componentWillMount.test.js +++ b/test/browser/lifecycles/componentWillMount.test.js @@ -1,4 +1,5 @@ import { createElement, render, Component } from 'preact'; +import { setupRerender } from 'preact/test-utils'; import { setupScratch, teardown } from '../../_util/helpers'; /** @jsx createElement */ @@ -7,8 +8,12 @@ describe('Lifecycle methods', () => { /** @type {HTMLDivElement} */ let scratch; + /** @type {() => void} */ + let rerender; + beforeEach(() => { scratch = setupScratch(); + rerender = setupRerender(); }); afterEach(() => { @@ -39,5 +44,40 @@ describe('Lifecycle methods', () => { expect(componentState).to.deep.equal({ value: 1 }); }); + + it('should invoke setState callbacks when setState is called in componentWillMount', () => { + let componentState; + let callback = sinon.spy(); + + class Foo extends Component { + constructor(props) { + super(props); + this.state = { + value: 0 + }; + } + componentWillMount() { + this.setState({ value: 1 }, callback); + this.setState({ value: 2 }, () => { + callback(); + this.setState({ value: 3 }, callback); + }); + } + render() { + componentState = this.state; + return
; + } + } + + render(