forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactErrorUtils-test.internal.js
212 lines (192 loc) · 5.39 KB
/
ReactErrorUtils-test.internal.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
/**
* Copyright (c) 2014-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/
'use strict';
let ReactErrorUtils;
describe('ReactErrorUtils', () => {
beforeEach(() => {
// TODO: can we express this test with only public API?
ReactErrorUtils = require('shared/ReactErrorUtils').default;
});
it(`it should rethrow caught errors`, () => {
const err = new Error('foo');
const callback = function() {
throw err;
};
ReactErrorUtils.invokeGuardedCallbackAndCatchFirstError(
'foo',
callback,
null,
);
expect(ReactErrorUtils.hasCaughtError()).toBe(false);
expect(() => ReactErrorUtils.rethrowCaughtError()).toThrow(err);
});
it(`should call the callback the passed arguments`, () => {
const callback = jest.fn();
ReactErrorUtils.invokeGuardedCallback(
'foo',
callback,
null,
'arg1',
'arg2',
);
expect(callback).toBeCalledWith('arg1', 'arg2');
});
it(`should call the callback with the provided context`, () => {
const context = {didCall: false};
ReactErrorUtils.invokeGuardedCallback(
'foo',
function() {
this.didCall = true;
},
context,
);
expect(context.didCall).toBe(true);
});
it(`should catch errors`, () => {
const error = new Error();
const returnValue = ReactErrorUtils.invokeGuardedCallback(
'foo',
function() {
throw error;
},
null,
'arg1',
'arg2',
);
expect(returnValue).toBe(undefined);
expect(ReactErrorUtils.hasCaughtError()).toBe(true);
expect(ReactErrorUtils.clearCaughtError()).toBe(error);
});
it(`should return false from clearCaughtError if no error was thrown`, () => {
const callback = jest.fn();
ReactErrorUtils.invokeGuardedCallback('foo', callback, null);
expect(ReactErrorUtils.hasCaughtError()).toBe(false);
expect(ReactErrorUtils.clearCaughtError).toThrow('no error was captured');
});
it(`can nest with same debug name`, () => {
const err1 = new Error();
let err2;
const err3 = new Error();
let err4;
ReactErrorUtils.invokeGuardedCallback(
'foo',
function() {
ReactErrorUtils.invokeGuardedCallback(
'foo',
function() {
throw err1;
},
null,
);
err2 = ReactErrorUtils.clearCaughtError();
throw err3;
},
null,
);
err4 = ReactErrorUtils.clearCaughtError();
expect(err2).toBe(err1);
expect(err4).toBe(err3);
});
it(`handles nested errors`, () => {
const err1 = new Error();
let err2;
ReactErrorUtils.invokeGuardedCallback(
'foo',
function() {
ReactErrorUtils.invokeGuardedCallback(
'foo',
function() {
throw err1;
},
null,
);
err2 = ReactErrorUtils.clearCaughtError();
},
null,
);
// Returns null because inner error was already captured
expect(ReactErrorUtils.hasCaughtError()).toBe(false);
expect(err2).toBe(err1);
});
it('handles nested errors in separate renderers', () => {
const ReactErrorUtils1 = require('shared/ReactErrorUtils').default;
jest.resetModules();
const ReactErrorUtils2 = require('shared/ReactErrorUtils').default;
expect(ReactErrorUtils1).not.toEqual(ReactErrorUtils2);
let ops = [];
ReactErrorUtils1.invokeGuardedCallback(
null,
() => {
ReactErrorUtils2.invokeGuardedCallback(
null,
() => {
throw new Error('nested error');
},
null,
);
// ReactErrorUtils2 should catch the error
ops.push(ReactErrorUtils2.hasCaughtError());
ops.push(ReactErrorUtils2.clearCaughtError().message);
},
null,
);
// ReactErrorUtils1 should not catch the error
ops.push(ReactErrorUtils1.hasCaughtError());
expect(ops).toEqual([true, 'nested error', false]);
});
if (!__DEV__) {
// jsdom doesn't handle this properly, but Chrome and Firefox should. Test
// this with a fixture.
it('catches null values', () => {
ReactErrorUtils.invokeGuardedCallback(
null,
function() {
throw null; // eslint-disable-line no-throw-literal
},
null,
);
expect(ReactErrorUtils.hasCaughtError()).toBe(true);
expect(ReactErrorUtils.clearCaughtError()).toBe(null);
});
}
it(`can be shimmed`, () => {
const ops = [];
jest.resetModules();
jest.mock(
'shared/invokeGuardedCallback',
() =>
function invokeGuardedCallback(name, func, context, a) {
ops.push(a);
try {
func.call(context, a);
} catch (error) {
this._hasCaughtError = true;
this._caughtError = error;
}
},
);
ReactErrorUtils = require('shared/ReactErrorUtils').default;
try {
const err = new Error('foo');
const callback = function() {
throw err;
};
ReactErrorUtils.invokeGuardedCallbackAndCatchFirstError(
'foo',
callback,
null,
'somearg',
);
expect(() => ReactErrorUtils.rethrowCaughtError()).toThrow(err);
expect(ops).toEqual(['somearg']);
} finally {
jest.unmock('shared/invokeGuardedCallback');
}
});
});