Skip to content

Commit 26ccc8f

Browse files
committed
Test that lifecycles are reverted in reverse order
1 parent c9a9872 commit 26ccc8f

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

src/renderers/shared/stack/reconciler/__tests__/ReactErrorBoundaries-test.js

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('ReactErrorBoundaries', () => {
5151
}
5252
render() {
5353
log.push('BrokenConstructor render');
54-
return <div />;
54+
return <div>{this.props.children}</div>;
5555
}
5656
componentWillMount() {
5757
log.push('BrokenConstructor componentWillMount');
@@ -80,7 +80,7 @@ describe('ReactErrorBoundaries', () => {
8080
}
8181
render() {
8282
log.push('BrokenComponentWillMount render');
83-
return <div />;
83+
return <div>{this.props.children}</div>;
8484
}
8585
componentWillMount() {
8686
log.push('BrokenComponentWillMount componentWillMount [!]');
@@ -110,7 +110,7 @@ describe('ReactErrorBoundaries', () => {
110110
}
111111
render() {
112112
log.push('BrokenComponentDidMount render');
113-
return <div />;
113+
return <div>{this.props.children}</div>;
114114
}
115115
componentWillMount() {
116116
log.push('BrokenComponentDidMount componentWillMount');
@@ -140,7 +140,7 @@ describe('ReactErrorBoundaries', () => {
140140
}
141141
render() {
142142
log.push('BrokenComponentWillReceiveProps render');
143-
return <div />;
143+
return <div>{this.props.children}</div>;
144144
}
145145
componentWillMount() {
146146
log.push('BrokenComponentWillReceiveProps componentWillMount');
@@ -170,7 +170,7 @@ describe('ReactErrorBoundaries', () => {
170170
}
171171
render() {
172172
log.push('BrokenComponentWillUpdate render');
173-
return <div />;
173+
return <div>{this.props.children}</div>;
174174
}
175175
componentWillMount() {
176176
log.push('BrokenComponentWillUpdate componentWillMount');
@@ -200,7 +200,7 @@ describe('ReactErrorBoundaries', () => {
200200
}
201201
render() {
202202
log.push('BrokenComponentDidUpdate render');
203-
return <div />;
203+
return <div>{this.props.children}</div>;
204204
}
205205
componentWillMount() {
206206
log.push('BrokenComponentDidUpdate componentWillMount');
@@ -230,7 +230,7 @@ describe('ReactErrorBoundaries', () => {
230230
}
231231
render() {
232232
log.push('BrokenComponentWillUnmount render');
233-
return <div />;
233+
return <div>{this.props.children}</div>;
234234
}
235235
componentWillMount() {
236236
log.push('BrokenComponentWillUnmount componentWillMount');
@@ -476,7 +476,7 @@ describe('ReactErrorBoundaries', () => {
476476
log.push('Stateful render [!]');
477477
throw new Error('Hello');
478478
}
479-
return <div />;
479+
return <div>{this.props.children}</div>;
480480
}
481481
}
482482

@@ -522,7 +522,7 @@ describe('ReactErrorBoundaries', () => {
522522
log.push('Stateful render [!]');
523523
throw new Error('Hello');
524524
}
525-
return <div />;
525+
return <div>{this.props.children}</div>;
526526
}
527527
}
528528

@@ -709,19 +709,41 @@ describe('ReactErrorBoundaries', () => {
709709
var container = document.createElement('div');
710710
ReactDOM.render(
711711
<ErrorBoundary>
712+
<BrokenComponentWillUnmount>
713+
<Normal />
714+
</BrokenComponentWillUnmount>
712715
<BrokenComponentDidMount />
716+
<Normal logName="NeverFullyMounted" />
713717
</ErrorBoundary>,
714718
container
715719
);
716720
expect(log).toEqual([
717721
'ErrorBoundary constructor',
718722
'ErrorBoundary componentWillMount',
719723
'ErrorBoundary render success',
724+
'BrokenComponentWillUnmount constructor',
725+
'BrokenComponentWillUnmount componentWillMount',
726+
'BrokenComponentWillUnmount render',
727+
'Normal constructor',
728+
'Normal componentWillMount',
729+
'Normal render',
720730
'BrokenComponentDidMount constructor',
721731
'BrokenComponentDidMount componentWillMount',
722732
'BrokenComponentDidMount render',
733+
'NeverFullyMounted constructor',
734+
'NeverFullyMounted componentWillMount',
735+
'NeverFullyMounted render',
736+
// Start flushing didMount queue
737+
'Normal componentDidMount',
738+
'BrokenComponentWillUnmount componentDidMount',
723739
'BrokenComponentDidMount componentDidMount [!]',
740+
// Call willUnmount for every didMount so far
724741
'BrokenComponentDidMount componentWillUnmount',
742+
// Parents get willUnmount first
743+
'BrokenComponentWillUnmount componentWillUnmount [!]',
744+
// Continue unmounting safely despite any errors
745+
'Normal componentWillUnmount',
746+
// We didn't need to call willUnmount for NeverFullyMounted.
725747
'ErrorBoundary unstable_handleError',
726748
'ErrorBoundary constructor',
727749
'ErrorBoundary componentWillMount',
@@ -743,7 +765,11 @@ describe('ReactErrorBoundaries', () => {
743765
expect(() => {
744766
ReactDOM.render(
745767
<ErrorBoundary>
768+
<Normal>
769+
<Normal logName="NormalChild" />
770+
</Normal>
746771
<BrokenComponentDidMount />
772+
<Normal logName="NeverFullyMounted" />
747773
</ErrorBoundary>,
748774
container
749775
);
@@ -752,19 +778,23 @@ describe('ReactErrorBoundaries', () => {
752778
'ErrorBoundary constructor',
753779
'ErrorBoundary componentWillMount',
754780
'ErrorBoundary render success',
781+
'Normal constructor',
782+
'Normal componentWillMount',
783+
'Normal render',
784+
'NormalChild constructor',
785+
'NormalChild componentWillMount',
786+
'NormalChild render',
755787
'BrokenComponentDidMount constructor',
756788
'BrokenComponentDidMount componentWillMount',
757789
'BrokenComponentDidMount render',
790+
'NeverFullyMounted constructor',
791+
'NeverFullyMounted componentWillMount',
792+
'NeverFullyMounted render',
793+
'NormalChild componentDidMount',
794+
'Normal componentDidMount',
758795
'BrokenComponentDidMount componentDidMount [!]',
759796
// The error doesn't get caught. :-(
760797
]);
761-
762-
log.length = 0;
763-
ReactDOM.unmountComponentAtNode(container);
764-
expect(log).toEqual([
765-
'ErrorBoundary componentWillUnmount',
766-
'BrokenComponentDidMount componentWillUnmount',
767-
]);
768798
});
769799
}
770800

@@ -1742,7 +1772,7 @@ describe('ReactErrorBoundaries', () => {
17421772
if (fail) {
17431773
throw new Error('Hello');
17441774
}
1745-
return <div />;
1775+
return <div>{this.props.children}</div>;
17461776
}
17471777
}
17481778

0 commit comments

Comments
 (0)