Add tests verifying we don't swallow exceptions
Dan Abramov committed
Oct 28, 2016 at 19:23 UTC
c08469a7d6647fd1e4f0480713bdf2d9e49a719f
1 file changed
+86
-2
src/renderers/shared/stack/reconciler/__tests__/ReactErrorBoundaries-test.js
+86
-2
@@ -196,7 +196,7 @@ describe('ReactErrorBoundaries', () => {
196
197
BrokenComponentDidUpdate = class extends React.Component {
198
static defaultProps = {
199
- errorText: 'Hello'
199
+ errorText: 'Hello',
200
};
201
constructor(props) {
202
super(props);
@@ -229,7 +229,7 @@ describe('ReactErrorBoundaries', () => {
229
230
BrokenComponentWillUnmount = class extends React.Component {
231
static defaultProps = {
232
- errorText: 'Hello'
232
+ errorText: 'Hello',
233
};
234
constructor(props) {
235
super(props);
@@ -502,6 +502,90 @@ describe('ReactErrorBoundaries', () => {
502
};
503
});
504
505
+ it('does not swallow exceptions on mounting without boundaries', () => {
506
+ var container = document.createElement('div');
507
+ expect(() => {
508
+ ReactDOM.render(<BrokenRender />, container);
509
+ }).toThrow('Hello');
510
+
511
+ container = document.createElement('div');
512
+ expect(() => {
513
+ ReactDOM.render(<BrokenComponentWillMount />, container);
514
+ }).toThrow('Hello');
515
+
516
+ container = document.createElement('div');
517
+ expect(() => {
518
+ ReactDOM.render(<BrokenComponentDidMount />, container);
519
+ }).toThrow('Hello');
520
+ });
521
+
522
+ it('does not swallow exceptions on updating without boundaries', () => {
523
+ var container = document.createElement('div');
524
+ ReactDOM.render(<BrokenComponentWillUpdate />, container);
525
+ expect(() => {
526
+ ReactDOM.render(<BrokenComponentWillUpdate />, container);
527
+ }).toThrow('Hello');
528
+
529
+ container = document.createElement('div');
530
+ ReactDOM.render(<BrokenComponentWillReceiveProps />, container);
531
+ expect(() => {
532
+ ReactDOM.render(<BrokenComponentWillReceiveProps />, container);
533
+ }).toThrow('Hello');
534
+
535
+ container = document.createElement('div');
536
+ ReactDOM.render(<BrokenComponentDidUpdate />, container);
537
+ expect(() => {
538
+ ReactDOM.render(<BrokenComponentDidUpdate />, container);
539
+ }).toThrow('Hello');
540
+ });
541
+
542
+ it('does not swallow exceptions on unmounting without boundaries', () => {
543
+ var container = document.createElement('div');
544
+ ReactDOM.render(<BrokenComponentWillUnmount />, container);
545
+ expect(() => {
546
+ ReactDOM.unmountComponentAtNode(container);
547
+ }).toThrow('Hello');
548
+ });
549
+
550
+ it('prevents errors from leaking into other roots', () => {
551
+ var container1 = document.createElement('div');
552
+ var container2 = document.createElement('div');
553
+ var container3 = document.createElement('div');
554
+
555
+ ReactDOM.render(<span>Before 1</span>, container1);
556
+ expect(() => {
557
+ ReactDOM.render(<BrokenRender />, container2);
558
+ }).toThrow('Hello');
559
+ ReactDOM.render(
560
+ <ErrorBoundary>
561
+ <BrokenRender />
562
+ </ErrorBoundary>,
563
+ container3
564
+ );
565
+ expect(container1.firstChild.textContent).toBe('Before 1');
566
+ expect(container2.firstChild).toBe(null);
567
+ expect(container3.firstChild.textContent).toBe('Caught an error: Hello.');
568
+
569
+ ReactDOM.render(<span>After 1</span>, container1);
570
+ ReactDOM.render(<span>After 2</span>, container2);
571
+ ReactDOM.render(
572
+ <ErrorBoundary forceRetry={true}>
573
+ After 3
574
+ </ErrorBoundary>,
575
+ container3
576
+ );
577
+ expect(container1.firstChild.textContent).toBe('After 1');
578
+ expect(container2.firstChild.textContent).toBe('After 2');
579
+ expect(container3.firstChild.textContent).toBe('After 3');
580
+
581
+ ReactDOM.unmountComponentAtNode(container1);
582
+ ReactDOM.unmountComponentAtNode(container2);
583
+ ReactDOM.unmountComponentAtNode(container3);
584
+ expect(container1.firstChild).toBe(null);
585
+ expect(container2.firstChild).toBe(null);
586
+ expect(container3.firstChild).toBe(null);
587
+ });
588
+
589
it('renders an error state if child throws in render', () => {
590
var container = document.createElement('div');
591
ReactDOM.render(