Merge pull request #9088 from acdlite/testerrorutilsshim

Test that ReactErrorUtils module can be shimmed
This commit is contained in:
Andrew Clark
2017-03-03 14:09:10 -08:00
committed by GitHub
3 changed files with 29 additions and 2 deletions
@@ -26,7 +26,7 @@ let caughtError = null;
const ReactErrorUtils = {
invokeGuardedCallback: function<A, B, C, D, E, F, Context>(
name: string | null,
func: (A, B, C, D, E, F) => void,
func: (a: A, b: B, c: C, d: D, e: E, f: F) => void,
context: Context,
a: A,
b: B,
@@ -55,7 +55,7 @@ const ReactErrorUtils = {
*/
invokeGuardedCallbackAndCatchFirstError: function<A, B, C, D, E, F, Context>(
name: string | null,
func: (A, B, C, D, E, F) => void,
func: (a: A, b: B, c: C, d: D, e: E, f: F) => void,
context: Context,
a: A,
b: B,
@@ -106,5 +106,30 @@ describe('ReactErrorUtils', () => {
expect(err3).toBe(null); // Returns null because inner error was already captured
expect(err2).toBe(err1);
});
it(`can be shimmed (${environment})`, () => {
const ops = [];
// Override the original invokeGuardedCallback
ReactErrorUtils.invokeGuardedCallback = function(name, func, context, a) {
ops.push(a);
try {
func.call(context, a);
} catch (error) {
return error;
}
return null;
};
var err = new Error('foo');
var callback = function() {
throw err;
};
ReactErrorUtils.invokeGuardedCallbackAndCatchFirstError('foo', callback, null, 'somearg');
expect(() => ReactErrorUtils.rethrowCaughtError()).toThrow(err);
// invokeGuardedCallbackAndCatchFirstError and rethrowCaughtError close
// over ReactErrorUtils.invokeGuardedCallback so should use the
// shimmed version.
expect(ops).toEqual(['somearg']);
});
}
});