/
lifecycle.js
1490 lines (1307 loc) · 39.7 KB
/
lifecycle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { h, render, rerender, Component } from '../../src/preact';
/** @jsx h */
let spyAll = obj => Object.keys(obj).forEach( key => sinon.spy(obj,key) );
const EMPTY_CHILDREN = [];
describe('Lifecycle methods', () => {
let scratch;
before( () => {
scratch = document.createElement('div');
(document.body || document.documentElement).appendChild(scratch);
});
beforeEach( () => {
scratch.innerHTML = '';
});
after( () => {
scratch.parentNode.removeChild(scratch);
scratch = null;
});
it('should call nested new lifecycle methods in the right order', () => {
let updateOuterState;
let updateInnerState;
let forceUpdateOuter;
let forceUpdateInner;
let log;
const logger = function(msg) {
return function() {
// return true for shouldComponentUpdate
log.push(msg);
return true;
};
};
class Outer extends Component {
static getDerivedStateFromProps() {
log.push('outer getDerivedStateFromProps');
return null;
}
constructor() {
super();
log.push('outer constructor');
this.state = { value: 0 };
forceUpdateOuter = () => this.forceUpdate();
updateOuterState = () => this.setState({
value: (this.state.value + 1) % 2
});
}
render() {
log.push('outer render');
return (
<div>
<Inner x={this.props.x} outerValue={this.state.value} />
</div>
);
}
}
Object.assign(Outer.prototype, {
componentDidMount: logger('outer componentDidMount'),
shouldComponentUpdate: logger('outer shouldComponentUpdate'),
getSnapshotBeforeUpdate: logger('outer getSnapshotBeforeUpdate'),
componentDidUpdate: logger('outer componentDidUpdate'),
componentWillUnmount: logger('outer componentWillUnmount')
});
class Inner extends Component {
static getDerivedStateFromProps() {
log.push('inner getDerivedStateFromProps');
return null;
}
constructor() {
super();
log.push('inner constructor');
this.state = { value: 0 };
forceUpdateInner = () => this.forceUpdate();
updateInnerState = () => this.setState({
value: (this.state.value + 1) % 2
});
}
render() {
log.push('inner render');
return <span>{this.props.x} {this.props.outerValue} {this.state.value}</span>;
}
}
Object.assign(Inner.prototype, {
componentDidMount: logger('inner componentDidMount'),
shouldComponentUpdate: logger('inner shouldComponentUpdate'),
getSnapshotBeforeUpdate: logger('inner getSnapshotBeforeUpdate'),
componentDidUpdate: logger('inner componentDidUpdate'),
componentWillUnmount: logger('inner componentWillUnmount')
});
// Constructor & mounting
log = [];
render(<Outer x={1} />, scratch);
expect(log).to.deep.equal([
'outer constructor',
'outer getDerivedStateFromProps',
'outer render',
'inner constructor',
'inner getDerivedStateFromProps',
'inner render',
'inner componentDidMount',
'outer componentDidMount'
]);
// Outer & Inner props update
log = [];
render(<Outer x={2} />, scratch, scratch.firstChild);
// Note: we differ from react here in that we apply changes to the dom
// as we find them while diffing. React on the other hand separates this
// into specific phases, meaning changes to the dom are only flushed
// once the whole diff-phase is complete. This is why
// "outer getSnapshotBeforeUpdate" is called just before the "inner" hooks.
// For react this call would be right before "outer componentDidUpdate"
expect(log).to.deep.equal([
'outer getDerivedStateFromProps',
'outer shouldComponentUpdate',
'outer render',
'outer getSnapshotBeforeUpdate',
'inner getDerivedStateFromProps',
'inner shouldComponentUpdate',
'inner render',
'inner getSnapshotBeforeUpdate',
'inner componentDidUpdate',
'outer componentDidUpdate'
]);
// Outer state update & Inner props update
log = [];
updateOuterState();
rerender();
expect(log).to.deep.equal([
'outer getDerivedStateFromProps',
'outer shouldComponentUpdate',
'outer render',
'outer getSnapshotBeforeUpdate',
'inner getDerivedStateFromProps',
'inner shouldComponentUpdate',
'inner render',
'inner getSnapshotBeforeUpdate',
'inner componentDidUpdate',
'outer componentDidUpdate'
]);
// Inner state update
log = [];
updateInnerState();
rerender();
expect(log).to.deep.equal([
'inner getDerivedStateFromProps',
'inner shouldComponentUpdate',
'inner render',
'inner getSnapshotBeforeUpdate',
'inner componentDidUpdate'
]);
// Force update Outer
log = [];
forceUpdateOuter();
rerender();
expect(log).to.deep.equal([
'outer getDerivedStateFromProps',
'outer render',
'outer getSnapshotBeforeUpdate',
'inner getDerivedStateFromProps',
'inner shouldComponentUpdate',
'inner render',
'inner getSnapshotBeforeUpdate',
'inner componentDidUpdate',
'outer componentDidUpdate'
]);
// Force update Inner
log = [];
forceUpdateInner();
rerender();
expect(log).to.deep.equal([
'inner getDerivedStateFromProps',
'inner render',
'inner getSnapshotBeforeUpdate',
'inner componentDidUpdate'
]);
// Unmounting Outer & Inner
log = [];
render(<table />, scratch, scratch.firstChild);
expect(log).to.deep.equal([
'outer componentWillUnmount',
'inner componentWillUnmount'
]);
});
describe('static getDerivedStateFromProps', () => {
it('should set initial state with value returned from getDerivedStateFromProps', () => {
class Foo extends Component {
static getDerivedStateFromProps(nextProps) {
return {
foo: nextProps.foo,
bar: 'bar'
};
}
render() {
return <div className={`${this.state.foo} ${this.state.bar}`} />;
}
}
let element = render(<Foo foo="foo" />, scratch);
expect(element.className).to.be.equal('foo bar');
});
it('should update initial state with value returned from getDerivedStateFromProps', () => {
class Foo extends Component {
constructor(props, context) {
super(props, context);
this.state = {
foo: 'foo',
bar: 'bar'
};
}
static getDerivedStateFromProps(nextProps, prevState) {
return {
foo: `not-${prevState.foo}`
};
}
render() {
return <div className={`${this.state.foo} ${this.state.bar}`} />;
}
}
let element = render(<Foo />, scratch);
expect(element.className).to.equal('not-foo bar');
});
it('should update the instance\'s state with the value returned from getDerivedStateFromProps when props change', () => {
class Foo extends Component {
constructor(props, context) {
super(props, context);
this.state = {
value: 'initial'
};
}
static getDerivedStateFromProps(nextProps) {
if (nextProps.update) {
return {
value: 'updated'
};
}
return null;
}
componentDidMount() {}
componentDidUpdate() {}
render() {
return <div className={this.state.value} />;
}
}
let element;
sinon.spy(Foo, 'getDerivedStateFromProps');
sinon.spy(Foo.prototype, 'componentDidMount');
sinon.spy(Foo.prototype, 'componentDidUpdate');
element = render(<Foo update={false} />, scratch, element);
expect(element.className).to.equal('initial');
expect(Foo.getDerivedStateFromProps).to.have.callCount(1);
expect(Foo.prototype.componentDidMount).to.have.callCount(1); // verify mount occurred
expect(Foo.prototype.componentDidUpdate).to.have.callCount(0);
element = render(<Foo update={true} />, scratch, element);
expect(element.className).to.equal('updated');
expect(Foo.getDerivedStateFromProps).to.have.callCount(2);
expect(Foo.prototype.componentDidMount).to.have.callCount(1);
expect(Foo.prototype.componentDidUpdate).to.have.callCount(1); // verify update occurred
});
it('should update the instance\'s state with the value returned from getDerivedStateFromProps when state changes', () => {
class Foo extends Component {
constructor(props, context) {
super(props, context);
this.state = {
value: 'initial'
};
}
static getDerivedStateFromProps(nextProps, prevState) {
// Don't change state for call that happens after the constructor
if (prevState.value === 'initial') {
return null;
}
return {
value: prevState.value + ' derived'
};
}
componentDidMount() {
this.setState({ value: 'updated' });
}
render() {
return <div className={this.state.value} />;
}
}
let element;
sinon.spy(Foo, 'getDerivedStateFromProps');
element = render(<Foo />, scratch, element);
expect(element.className).to.equal('initial');
expect(Foo.getDerivedStateFromProps).to.have.been.calledOnce;
rerender(); // call rerender to handle cDM setState call
expect(element.className).to.equal('updated derived');
expect(Foo.getDerivedStateFromProps).to.have.been.calledTwice;
});
it('should NOT modify state if null is returned', () => {
class Foo extends Component {
constructor(props, context) {
super(props, context);
this.state = {
foo: 'foo',
bar: 'bar'
};
}
static getDerivedStateFromProps() {
return null;
}
render() {
return <div className={`${this.state.foo} ${this.state.bar}`} />;
}
}
sinon.spy(Foo, 'getDerivedStateFromProps');
let element = render(<Foo />, scratch);
expect(element.className).to.equal('foo bar');
expect(Foo.getDerivedStateFromProps).to.have.been.called;
});
// NOTE: Difference from React
// React v16.3.2 warns if undefined if returned from getDerivedStateFromProps
it('should NOT modify state if undefined is returned', () => {
class Foo extends Component {
constructor(props, context) {
super(props, context);
this.state = {
foo: 'foo',
bar: 'bar'
};
}
static getDerivedStateFromProps() {}
render() {
return <div className={`${this.state.foo} ${this.state.bar}`} />;
}
}
sinon.spy(Foo, 'getDerivedStateFromProps');
let element = render(<Foo />, scratch);
expect(element.className).to.equal('foo bar');
expect(Foo.getDerivedStateFromProps).to.have.been.called;
});
it('should NOT invoke deprecated lifecycles (cWM/cWRP) if new static gDSFP is present', () => {
class Foo extends Component {
static getDerivedStateFromProps() {}
componentWillMount() {}
componentWillReceiveProps() {}
render() {
return <div />;
}
}
sinon.spy(Foo, 'getDerivedStateFromProps');
sinon.spy(Foo.prototype, 'componentWillMount');
sinon.spy(Foo.prototype, 'componentWillReceiveProps');
render(<Foo />, scratch);
expect(Foo.getDerivedStateFromProps).to.have.been.called;
expect(Foo.prototype.componentWillMount).to.not.have.been.called;
expect(Foo.prototype.componentWillReceiveProps).to.not.have.been.called;
});
it('is not called if neither state nor props have changed', () => {
let logs = [];
let childRef;
class Parent extends Component {
constructor(props) {
super(props);
this.state = { parentRenders: 0 };
}
static getDerivedStateFromProps(props, prevState) {
logs.push('parent getDerivedStateFromProps');
return prevState.parentRenders + 1;
}
render() {
logs.push('parent render');
return <Child parentRenders={this.state.parentRenders} ref={child => childRef = child} />;
}
}
class Child extends Component {
render() {
logs.push('child render');
return this.props.parentRenders;
}
}
render(<Parent />, scratch);
expect(logs).to.deep.equal([
'parent getDerivedStateFromProps',
'parent render',
'child render'
]);
logs = [];
childRef.setState({});
rerender();
expect(logs).to.deep.equal([
'child render'
]);
});
// TODO: Investigate this test:
// [should not override state with stale values if prevState is spread within getDerivedStateFromProps](https://github.com/facebook/react/blob/25dda90c1ecb0c662ab06e2c80c1ee31e0ae9d36/packages/react-dom/src/__tests__/ReactComponentLifeCycle-test.js#L1035)
it('should be passed next props and state', () => {
/** @type {() => void} */
let updateState;
let propsArg;
let stateArg;
class Foo extends Component {
constructor(props) {
super(props);
this.state = {
value: 0
};
updateState = () => this.setState({
value: this.state.value + 1
});
}
static getDerivedStateFromProps(props, state) {
// These object references might be updated later so copy
// object so we can assert their values at this snapshot in time
propsArg = {...props};
stateArg = {...state};
// NOTE: Don't do this in real production code!
// https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html
return {
value: state.value + 1
};
}
render() {
return <div>{this.state.value}</div>;
}
}
// Initial render
// state.value: initialized to 0 in constructor, 0 -> 1 in gDSFP
let element = render(<Foo foo="foo" />, scratch);
expect(element.textContent).to.be.equal('1');
expect(propsArg).to.deep.equal({
foo: "foo",
children: []
});
expect(stateArg).to.deep.equal({
value: 0
});
// New Props
// state.value: 1 -> 2 in gDSFP
render(<Foo foo="bar" />, scratch, scratch.firstChild);
expect(element.textContent).to.be.equal('2');
expect(propsArg).to.deep.equal({
foo: "bar",
children: []
});
expect(stateArg).to.deep.equal({
value: 1
});
// New state
// state.value: 2 -> 3 in updateState, 3 -> 4 in gDSFP
updateState();
rerender();
expect(element.textContent).to.be.equal('4');
expect(propsArg).to.deep.equal({
foo: "bar",
children: []
});
expect(stateArg).to.deep.equal({
value: 3
});
});
});
describe("#getSnapshotBeforeUpdate", () => {
it('should pass the return value from getSnapshotBeforeUpdate to componentDidUpdate', () => {
let log = [];
class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
value: 0
};
}
static getDerivedStateFromProps(nextProps, prevState) {
return {
value: prevState.value + 1
};
}
getSnapshotBeforeUpdate(prevProps, prevState) {
log.push(
`getSnapshotBeforeUpdate() prevProps:${prevProps.value} prevState:${
prevState.value
}`,
);
return 'abc';
}
componentDidUpdate(prevProps, prevState, snapshot) {
log.push(
`componentDidUpdate() prevProps:${prevProps.value} prevState:${
prevState.value
} snapshot:${snapshot}`,
);
}
render() {
log.push('render');
return null;
}
}
render(<MyComponent value="foo" />, scratch);
expect(log).to.deep.equal(['render']);
log = [];
render(<MyComponent value="bar" />, scratch, scratch.firstChild);
expect(log).to.deep.equal([
'render',
'getSnapshotBeforeUpdate() prevProps:foo prevState:1',
'componentDidUpdate() prevProps:foo prevState:1 snapshot:abc'
]);
log = [];
render(<MyComponent value="baz" />, scratch, scratch.firstChild);
expect(log).to.deep.equal([
'render',
'getSnapshotBeforeUpdate() prevProps:bar prevState:2',
'componentDidUpdate() prevProps:bar prevState:2 snapshot:abc'
]);
log = [];
render(<div />, scratch, scratch.firstChild);
expect(log).to.deep.equal([]);
});
it('should call getSnapshotBeforeUpdate before mutations are committed', () => {
let log = [];
class MyComponent extends Component {
getSnapshotBeforeUpdate(prevProps) {
log.push('getSnapshotBeforeUpdate');
expect(this.divRef.textContent).to.equal(
`value:${prevProps.value}`,
);
return 'foobar';
}
componentDidUpdate(prevProps, prevState, snapshot) {
log.push('componentDidUpdate');
expect(this.divRef.textContent).to.equal(
`value:${this.props.value}`,
);
expect(snapshot).to.equal('foobar');
}
render() {
log.push('render');
return <div ref={ref => this.divRef = ref}>{`value:${this.props.value}`}</div>;
}
}
render(<MyComponent value="foo" />, scratch);
expect(log).to.deep.equal(['render']);
log = [];
render(<MyComponent value="bar" />, scratch, scratch.firstChild);
expect(log).to.deep.equal([
'render',
'getSnapshotBeforeUpdate',
'componentDidUpdate'
]);
});
it('should be passed the previous props and state', () => {
/** @type {() => void} */
let updateState;
let prevPropsArg;
let prevStateArg;
let curProps;
let curState;
class Foo extends Component {
constructor(props) {
super(props);
this.state = {
value: 0
};
updateState = () => this.setState({
value: this.state.value + 1
});
}
static getDerivedStateFromProps(props, state) {
// NOTE: Don't do this in real production code!
// https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html
return {
value: state.value + 1
};
}
getSnapshotBeforeUpdate(prevProps, prevState) {
// These object references might be updated later so copy
// object so we can assert their values at this snapshot in time
prevPropsArg = {...prevProps};
prevStateArg = {...prevState};
curProps = {...this.props};
curState = {...this.state};
}
render() {
return <div>{this.state.value}</div>;
}
}
// Expectation:
// `prevState` in getSnapshotBeforeUpdate should be
// the state before setState or getDerivedStateFromProps was called.
// `this.state` in getSnapshotBeforeUpdate should be
// the updated state after getDerivedStateFromProps was called.
// Initial render
// state.value: initialized to 0 in constructor, 0 -> 1 in gDSFP
let element = render(<Foo foo="foo" />, scratch);
expect(element.textContent).to.be.equal('1');
expect(prevPropsArg).to.be.undefined;
expect(prevStateArg).to.be.undefined;
expect(curProps).to.be.undefined;
expect(curState).to.be.undefined;
// New props
// state.value: 1 -> 2 in gDSFP
element = render(<Foo foo="bar" />, scratch, scratch.firstChild);
expect(element.textContent).to.be.equal('2');
expect(prevPropsArg).to.deep.equal({
foo: "foo",
children: []
});
expect(prevStateArg).to.deep.equal({
value: 1
});
expect(curProps).to.deep.equal({
foo: "bar",
children: []
});
expect(curState).to.deep.equal({
value: 2
});
// New state
// state.value: 2 -> 3 in updateState, 3 -> 4 in gDSFP
updateState();
rerender();
expect(element.textContent).to.be.equal('4');
expect(prevPropsArg).to.deep.equal({
foo: "bar",
children: []
});
expect(prevStateArg).to.deep.equal({
value: 2
});
expect(curProps).to.deep.equal({
foo: "bar",
children: []
});
expect(curState).to.deep.equal({
value: 4
});
});
});
describe('#componentWillUpdate', () => {
it('should NOT be called on initial render', () => {
class ReceivePropsComponent extends Component {
componentWillUpdate() {}
render() {
return <div />;
}
}
sinon.spy(ReceivePropsComponent.prototype, 'componentWillUpdate');
render(<ReceivePropsComponent />, scratch);
expect(ReceivePropsComponent.prototype.componentWillUpdate).not.to.have.been.called;
});
it('should be called when rerender with new props from parent', () => {
let doRender;
class Outer extends Component {
constructor(p, c) {
super(p, c);
this.state = { i: 0 };
}
componentDidMount() {
doRender = () => this.setState({ i: this.state.i + 1 });
}
render(props, { i }) {
return <Inner i={i} {...props} />;
}
}
class Inner extends Component {
componentWillUpdate(nextProps, nextState) {
expect(nextProps).to.be.deep.equal({ children:EMPTY_CHILDREN, i: 1 });
expect(nextState).to.be.deep.equal({});
}
render() {
return <div />;
}
}
sinon.spy(Inner.prototype, 'componentWillUpdate');
sinon.spy(Outer.prototype, 'componentDidMount');
// Initial render
render(<Outer />, scratch);
expect(Inner.prototype.componentWillUpdate).not.to.have.been.called;
// Rerender inner with new props
doRender();
rerender();
expect(Inner.prototype.componentWillUpdate).to.have.been.called;
});
it('should be called on new state', () => {
let doRender;
class ReceivePropsComponent extends Component {
componentWillUpdate() {}
componentDidMount() {
doRender = () => this.setState({ i: this.state.i + 1 });
}
render() {
return <div />;
}
}
sinon.spy(ReceivePropsComponent.prototype, 'componentWillUpdate');
render(<ReceivePropsComponent />, scratch);
expect(ReceivePropsComponent.prototype.componentWillUpdate).not.to.have.been.called;
doRender();
rerender();
expect(ReceivePropsComponent.prototype.componentWillUpdate).to.have.been.called;
});
it('should be called after children are mounted', () => {
let log = [];
class Inner extends Component {
componentDidMount() {
log.push('Inner mounted');
// Verify that the component is actually mounted when this
// callback is invoked.
expect(scratch.querySelector('#inner')).to.equal(this.base);
}
render() {
return <div id="inner"/>;
}
}
class Outer extends Component {
componentDidUpdate() {
log.push('Outer updated');
}
render(props) {
return props.renderInner ? <Inner /> : <div />;
}
}
const elem = render(<Outer renderInner={false} />, scratch);
render(<Outer renderInner={true} />, scratch, elem);
// expect(log).to.deep.equal(['Inner mounted', 'Outer updated']);
});
});
describe('#componentWillReceiveProps', () => {
it('should NOT be called on initial render', () => {
class ReceivePropsComponent extends Component {
componentWillReceiveProps() {}
render() {
return <div />;
}
}
sinon.spy(ReceivePropsComponent.prototype, 'componentWillReceiveProps');
render(<ReceivePropsComponent />, scratch);
expect(ReceivePropsComponent.prototype.componentWillReceiveProps).not.to.have.been.called;
});
it('should be called when rerender with new props from parent', () => {
let doRender;
class Outer extends Component {
constructor(p, c) {
super(p, c);
this.state = { i: 0 };
}
componentDidMount() {
doRender = () => this.setState({ i: this.state.i + 1 });
}
render(props, { i }) {
return <Inner i={i} {...props} />;
}
}
class Inner extends Component {
componentWillMount() {
expect(this.props.i).to.be.equal(0);
}
componentWillReceiveProps(nextProps) {
expect(nextProps.i).to.be.equal(1);
}
render() {
return <div />;
}
}
sinon.spy(Inner.prototype, 'componentWillReceiveProps');
sinon.spy(Outer.prototype, 'componentDidMount');
// Initial render
render(<Outer />, scratch);
expect(Inner.prototype.componentWillReceiveProps).not.to.have.been.called;
// Rerender inner with new props
doRender();
rerender();
expect(Inner.prototype.componentWillReceiveProps).to.have.been.called;
});
it('should be called in right execution order', () => {
let doRender;
class Outer extends Component {
constructor(p, c) {
super(p, c);
this.state = { i: 0 };
}
componentDidMount() {
doRender = () => this.setState({ i: this.state.i + 1 });
}
render(props, { i }) {
return <Inner i={i} {...props} />;
}
}
class Inner extends Component {
componentDidUpdate() {
expect(Inner.prototype.componentWillReceiveProps).to.have.been.called;
expect(Inner.prototype.componentWillUpdate).to.have.been.called;
}
componentWillReceiveProps() {
expect(Inner.prototype.componentWillUpdate).not.to.have.been.called;
expect(Inner.prototype.componentDidUpdate).not.to.have.been.called;
}
componentWillUpdate() {
expect(Inner.prototype.componentWillReceiveProps).to.have.been.called;
expect(Inner.prototype.componentDidUpdate).not.to.have.been.called;
}
render() {
return <div />;
}
}
sinon.spy(Inner.prototype, 'componentWillReceiveProps');
sinon.spy(Inner.prototype, 'componentDidUpdate');
sinon.spy(Inner.prototype, 'componentWillUpdate');
sinon.spy(Outer.prototype, 'componentDidMount');
render(<Outer />, scratch);
doRender();
rerender();
expect(Inner.prototype.componentWillReceiveProps).to.have.been.calledBefore(Inner.prototype.componentWillUpdate);
expect(Inner.prototype.componentWillUpdate).to.have.been.calledBefore(Inner.prototype.componentDidUpdate);
});
});
describe('#componentDidUpdate', () => {
it('should be passed previous props and state', () => {
/** @type {() => void} */
let updateState;
let prevPropsArg;
let prevStateArg;
let curProps;
let curState;
class Foo extends Component {
constructor(props) {
super(props);
this.state = {
value: 0
};
updateState = () => this.setState({
value: this.state.value + 1
});
}
static getDerivedStateFromProps(props, state) {
// NOTE: Don't do this in real production code!
// https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html
return {
value: state.value + 1
};
}
componentDidUpdate(prevProps, prevState) {
// These object references might be updated later so copy
// object so we can assert their values at this snapshot in time
prevPropsArg = {...prevProps};
prevStateArg = {...prevState};
curProps = {...this.props};
curState = {...this.state};
}
render() {
return <div>{this.state.value}</div>;
}
}
// Expectation:
// `prevState` in componentDidUpdate should be
// the state before setState and getDerivedStateFromProps was called.
// `this.state` in componentDidUpdate should be
// the updated state after getDerivedStateFromProps was called.
// Initial render
// state.value: initialized to 0 in constructor, 0 -> 1 in gDSFP
let element = render(<Foo foo="foo" />, scratch);
expect(element.textContent).to.be.equal('1');
expect(prevPropsArg).to.be.undefined;
expect(prevStateArg).to.be.undefined;
expect(curProps).to.be.undefined;
expect(curState).to.be.undefined;
// New props
// state.value: 1 -> 2 in gDSFP
element = render(<Foo foo="bar" />, scratch, scratch.firstChild);
expect(element.textContent).to.be.equal('2');
expect(prevPropsArg).to.deep.equal({
foo: "foo",
children: []
});
expect(prevStateArg).to.deep.equal({
value: 1
});
expect(curProps).to.deep.equal({
foo: "bar",
children: []
});
expect(curState).to.deep.equal({
value: 2
});
// New state
// state.value: 2 -> 3 in updateState, 3 -> 4 in gDSFP
updateState();
rerender();
expect(element.textContent).to.be.equal('4');
expect(prevPropsArg).to.deep.equal({
foo: "bar",
children: []
});
expect(prevStateArg).to.deep.equal({
value: 2
});
expect(curProps).to.deep.equal({
foo: "bar",
children: []
});
expect(curState).to.deep.equal({
value: 4
});
});
});
describe('top-level componentWillUnmount', () => {