Skip to content

Commit f293884

Browse files
author
Brandon Dail
committed
Add tests for ReactErrorUtils
Add fiber test report Linting fixes
1 parent 14dcb61 commit f293884

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

scripts/fiber/tests-passing.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,13 @@ src/renderers/shared/stack/reconciler/__tests__/refs-test.js
14181418
* ref called correctly for stateless component when __DEV__ = true
14191419
* coerces numbers to strings
14201420

1421+
src/renderers/shared/utils/__tests__/ReactErrorUtils-test.js
1422+
* should call the callback with only the passed argument
1423+
* should catch errors
1424+
* should rethrow caught errors
1425+
* should call the callback with only the passed argument
1426+
* should use invokeGuardedCallbackWithCatch in production
1427+
14211428
src/renderers/shared/utils/__tests__/accumulateInto-test.js
14221429
* throws if the second item is null
14231430
* returns the second item if first is null
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright 2014-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @emails react-core
10+
*/
11+
12+
'use strict';
13+
14+
var ReactErrorUtils;
15+
16+
describe('ReactErrorUtils', () => {
17+
18+
beforeEach(() => {
19+
ReactErrorUtils = require('ReactErrorUtils');
20+
});
21+
22+
describe('invokeGuardedCallbackWithCatch', () => {
23+
it('should call the callback with only the passed argument', () => {
24+
var callback = jest.fn();
25+
ReactErrorUtils.invokeGuardedCallbackWithCatch('foo', callback, 'arg');
26+
expect(callback).toBeCalledWith('arg');
27+
});
28+
29+
it('should catch errors', () => {
30+
var callback = function() {
31+
throw new Error('foo');
32+
};
33+
expect(
34+
() => ReactErrorUtils.invokeGuardedCallbackWithCatch('foo', callback)
35+
).not.toThrow();
36+
});
37+
});
38+
39+
describe('rethrowCaughtError', () => {
40+
it('should rethrow caught errors', () => {
41+
var err = new Error('foo');
42+
var callback = function() {
43+
throw err;
44+
};
45+
ReactErrorUtils.invokeGuardedCallbackWithCatch('foo', callback);
46+
expect(() => ReactErrorUtils.rethrowCaughtError()).toThrow(err);
47+
});
48+
});
49+
50+
describe('invokeGuardedCallback', () => {
51+
it('should call the callback with only the passed argument', () => {
52+
var callback = jest.fn();
53+
ReactErrorUtils.invokeGuardedCallback('foo', callback, 'arg');
54+
expect(callback).toBeCalledWith('arg');
55+
});
56+
57+
it('should use invokeGuardedCallbackWithCatch in production', () => {
58+
expect(ReactErrorUtils.invokeGuardedCallback).not.toEqual(
59+
ReactErrorUtils.invokeGuardedCallbackWithCatch
60+
);
61+
__DEV__ = false;
62+
var oldProcess = process;
63+
global.process = {env: {NODE_ENV: 'production'}};
64+
jest.resetModuleRegistry();
65+
ReactErrorUtils = require('ReactErrorUtils');
66+
expect(ReactErrorUtils.invokeGuardedCallback).toEqual(
67+
ReactErrorUtils.invokeGuardedCallbackWithCatch
68+
);
69+
__DEV__ = true;
70+
global.process = oldProcess;
71+
});
72+
});
73+
});

0 commit comments

Comments
 (0)