|
| 1 | +import React, { Component } from 'react' |
| 2 | +import renderPrepass from '..' |
| 3 | + |
| 4 | +it('returns to the next componentDidCatch boundary on erroring', () => { |
| 5 | + const Throw = jest.fn(() => { |
| 6 | + throw new Error() |
| 7 | + }) |
| 8 | + const Inner = jest.fn(() => null) |
| 9 | + |
| 10 | + class Outer extends Component { |
| 11 | + constructor() { |
| 12 | + super() |
| 13 | + this.state = { error: false } |
| 14 | + } |
| 15 | + |
| 16 | + componentDidCatch(error) { |
| 17 | + this.setState({ error: true }) |
| 18 | + } |
| 19 | + |
| 20 | + render() { |
| 21 | + return this.state.error ? <Inner /> : <Throw /> |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + const render$ = renderPrepass(<Outer />) |
| 26 | + expect(Throw).toHaveBeenCalledTimes(1) |
| 27 | + expect(Inner).not.toHaveBeenCalled() |
| 28 | + |
| 29 | + return render$.then(() => { |
| 30 | + expect(Inner).toHaveBeenCalledTimes(1) |
| 31 | + }) |
| 32 | +}) |
| 33 | + |
| 34 | +it('returns to the next getDerivedStateFromError boundary on erroring', () => { |
| 35 | + const Throw = jest.fn(() => { |
| 36 | + throw new Error() |
| 37 | + }) |
| 38 | + const Inner = jest.fn(() => null) |
| 39 | + |
| 40 | + class Outer extends Component { |
| 41 | + static getDerivedStateFromProps() { |
| 42 | + return { error: false } |
| 43 | + } |
| 44 | + |
| 45 | + static getDerivedStateFromError() { |
| 46 | + return { error: true } |
| 47 | + } |
| 48 | + |
| 49 | + render() { |
| 50 | + return this.state.error ? <Inner /> : <Throw /> |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + const render$ = renderPrepass(<Outer />) |
| 55 | + expect(Throw).toHaveBeenCalledTimes(1) |
| 56 | + expect(Inner).not.toHaveBeenCalled() |
| 57 | + |
| 58 | + return render$.then(() => { |
| 59 | + expect(Inner).toHaveBeenCalledTimes(1) |
| 60 | + }) |
| 61 | +}) |
| 62 | + |
| 63 | +it('guards against infinite render loops', () => { |
| 64 | + const Throw = jest.fn(() => { |
| 65 | + throw new Error() |
| 66 | + }) |
| 67 | + |
| 68 | + class Outer extends Component { |
| 69 | + componentDidCatch() {} // NOTE: This doesn't actually recover from errors |
| 70 | + render() { |
| 71 | + return <Throw /> |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return renderPrepass(<Outer />).then(() => { |
| 76 | + expect(Throw).toHaveBeenCalledTimes(25) |
| 77 | + }) |
| 78 | +}) |
| 79 | + |
| 80 | +it('returns to the next error boundary on a suspense error', () => { |
| 81 | + const Inner = jest.fn(() => null) |
| 82 | + |
| 83 | + const Throw = jest.fn(() => { |
| 84 | + throw Promise.reject(new Error('Suspense!')) |
| 85 | + }) |
| 86 | + |
| 87 | + class Outer extends Component { |
| 88 | + static getDerivedStateFromProps() { |
| 89 | + return { error: false } |
| 90 | + } |
| 91 | + |
| 92 | + static getDerivedStateFromError(error) { |
| 93 | + expect(error).not.toBeInstanceOf(Promise) |
| 94 | + return { error: true } |
| 95 | + } |
| 96 | + |
| 97 | + render() { |
| 98 | + return this.state.error ? <Inner /> : <Throw /> |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + const render$ = renderPrepass(<Outer />) |
| 103 | + expect(Throw).toHaveBeenCalledTimes(1) |
| 104 | + expect(Inner).not.toHaveBeenCalled() |
| 105 | + |
| 106 | + return render$.then(() => { |
| 107 | + expect(Inner).toHaveBeenCalledTimes(1) |
| 108 | + }) |
| 109 | +}) |
| 110 | + |
| 111 | +it('returns to the next error boundary on a nested error', () => { |
| 112 | + const Throw = jest.fn(({ depth }) => { |
| 113 | + if (depth >= 4) { |
| 114 | + throw new Error('' + depth) |
| 115 | + } |
| 116 | + |
| 117 | + return <Throw depth={depth + 1} /> |
| 118 | + }) |
| 119 | + |
| 120 | + class Outer extends Component { |
| 121 | + static getDerivedStateFromProps() { |
| 122 | + return { error: false } |
| 123 | + } |
| 124 | + |
| 125 | + static getDerivedStateFromError(error) { |
| 126 | + expect(error.message).toBe('4') |
| 127 | + return { error: true } |
| 128 | + } |
| 129 | + |
| 130 | + render() { |
| 131 | + return !this.state.error ? <Throw depth={1} /> : null |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + renderPrepass(<Outer />).then(() => { |
| 136 | + expect(Throw).toHaveBeenCalledTimes(4) |
| 137 | + }) |
| 138 | +}) |
| 139 | + |
| 140 | +it('always returns to the correct error boundary', () => { |
| 141 | + const values = [] |
| 142 | + |
| 143 | + const Inner = jest.fn(({ value, depth }) => { |
| 144 | + values.push({ value, depth }) |
| 145 | + return value |
| 146 | + }) |
| 147 | + |
| 148 | + const Throw = jest.fn(({ value }) => { |
| 149 | + throw new Error('' + value) |
| 150 | + }) |
| 151 | + |
| 152 | + class Outer extends Component { |
| 153 | + static getDerivedStateFromProps(props) { |
| 154 | + return { value: null } |
| 155 | + } |
| 156 | + |
| 157 | + static getDerivedStateFromError(error) { |
| 158 | + return { value: error.message } |
| 159 | + } |
| 160 | + |
| 161 | + render() { |
| 162 | + return [ |
| 163 | + this.state.value ? ( |
| 164 | + <Inner value={this.state.value} depth={this.props.depth} /> |
| 165 | + ) : ( |
| 166 | + <Throw value={this.props.depth} /> |
| 167 | + ), |
| 168 | + this.props.depth < 4 ? <Outer depth={this.props.depth + 1} /> : null |
| 169 | + ] |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return renderPrepass(<Outer depth={1} />).then(() => { |
| 174 | + expect(Throw).toHaveBeenCalledTimes(4) |
| 175 | + expect(Inner).toHaveBeenCalledTimes(4) |
| 176 | + expect(values).toMatchInlineSnapshot(` |
| 177 | + Array [ |
| 178 | + Object { |
| 179 | + "depth": 1, |
| 180 | + "value": "1", |
| 181 | + }, |
| 182 | + Object { |
| 183 | + "depth": 2, |
| 184 | + "value": "2", |
| 185 | + }, |
| 186 | + Object { |
| 187 | + "depth": 3, |
| 188 | + "value": "3", |
| 189 | + }, |
| 190 | + Object { |
| 191 | + "depth": 4, |
| 192 | + "value": "4", |
| 193 | + }, |
| 194 | + ] |
| 195 | + `) |
| 196 | + }) |
| 197 | +}) |
0 commit comments