|
| 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