Skip to content

Commit d5c9d72

Browse files
authored
Merge componentWillMount test from master (#3811)
Cherry-picked from: Invoke setState callbacks setup in componentWillMount (#3806)
1 parent ebd2194 commit d5c9d72

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

test/browser/lifecycles/componentWillMount.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createElement, render, Component } from 'preact';
2+
import { setupRerender } from 'preact/test-utils';
23
import { setupScratch, teardown } from '../../_util/helpers';
34

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

11+
/** @type {() => void} */
12+
let rerender;
13+
1014
beforeEach(() => {
1115
scratch = setupScratch();
16+
rerender = setupRerender();
1217
});
1318

1419
afterEach(() => {
@@ -39,5 +44,40 @@ describe('Lifecycle methods', () => {
3944

4045
expect(componentState).to.deep.equal({ value: 1 });
4146
});
47+
48+
it('should invoke setState callbacks when setState is called in componentWillMount', () => {
49+
let componentState;
50+
let callback = sinon.spy();
51+
52+
class Foo extends Component {
53+
constructor(props) {
54+
super(props);
55+
this.state = {
56+
value: 0
57+
};
58+
}
59+
componentWillMount() {
60+
this.setState({ value: 1 }, callback);
61+
this.setState({ value: 2 }, () => {
62+
callback();
63+
this.setState({ value: 3 }, callback);
64+
});
65+
}
66+
render() {
67+
componentState = this.state;
68+
return <div />;
69+
}
70+
}
71+
72+
render(<Foo />, scratch);
73+
74+
expect(componentState).to.deep.equal({ value: 2 });
75+
expect(callback).to.have.been.calledTwice;
76+
77+
rerender();
78+
79+
expect(componentState).to.deep.equal({ value: 3 });
80+
expect(callback).to.have.been.calledThrice;
81+
});
4282
});
4383
});

0 commit comments

Comments
 (0)