@samitouri / QOS-React / commits / 6947db1529

Use a closure to bind argument to callback in ReactErrorUtils (#8363)

* Use a closure to bind gaurded callback This way the fake event isn't being implicitly passed into the event handler * Add tests for ReactErrorUtils Add fiber test report Linting fixes

Brandon Dail committed Nov 28, 2016 at 10:54 UTC 6947db1529f1d7abd9f2aab10fe1eb1e49e31997
3 files changed +83 -1
scripts/fiber/tests-passing.txt
+7
@@ -1482,6 +1482,13 @@ src/renderers/shared/stack/reconciler/__tests__/refs-test.js
1482 * ref called correctly for stateless component when __DEV__ = true
1483 * coerces numbers to strings
1484
1485 +src/renderers/shared/utils/__tests__/ReactErrorUtils-test.js
1486 +* should call the callback with only the passed argument
1487 +* should catch errors
1488 +* should rethrow caught errors
1489 +* should call the callback with only the passed argument
1490 +* should use invokeGuardedCallbackWithCatch in production
1491 +
1492 src/renderers/shared/utils/__tests__/accumulateInto-test.js
1493 * throws if the second item is null
1494 * returns the second item if first is null
src/renderers/shared/utils/ReactErrorUtils.js
+3 -1
@@ -72,7 +72,9 @@ if (__DEV__) {
72 func: (a: A) => void,
73 a: A,
74 ): void {
75 - var boundFunc = func.bind(null, a);
75 + var boundFunc = function() {
76 + func(a);
77 + };
78 var evtType = `react-${name}`;
79 fakeNode.addEventListener(evtType, boundFunc, false);
80 var evt = document.createEvent('Event');
src/renderers/shared/utils/__tests__/ReactErrorUtils-test.js new
+73
@@ -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 +});