Skip to content

Commit

Permalink
Merge componentWillMount test from master (#3811)
Browse files Browse the repository at this point in the history
Cherry-picked from:

Invoke setState callbacks setup in componentWillMount (#3806)
  • Loading branch information
andrewiggins authored Nov 23, 2022
1 parent ebd2194 commit d5c9d72
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/browser/lifecycles/componentWillMount.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createElement, render, Component } from 'preact';
import { setupRerender } from 'preact/test-utils';
import { setupScratch, teardown } from '../../_util/helpers';

/** @jsx createElement */
Expand All @@ -7,8 +8,12 @@ describe('Lifecycle methods', () => {
/** @type {HTMLDivElement} */
let scratch;

/** @type {() => void} */
let rerender;

beforeEach(() => {
scratch = setupScratch();
rerender = setupRerender();
});

afterEach(() => {
Expand Down Expand Up @@ -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 <div />;
}
}

render(<Foo />, scratch);

expect(componentState).to.deep.equal({ value: 2 });
expect(callback).to.have.been.calledTwice;

rerender();

expect(componentState).to.deep.equal({ value: 3 });
expect(callback).to.have.been.calledThrice;
});
});
});

0 comments on commit d5c9d72

Please sign in to comment.