mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
* Comment previous occurrences of this issue
* Fix DEV performance regression in V8
* Extract try/catch into a separate function when calling hooks
(cherry picked from commit afa27bc4d5)
86 lines
2.4 KiB
JavaScript
86 lines
2.4 KiB
JavaScript
/**
|
|
* Copyright 2016-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @emails react-core
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
describe('ReactDebugTool', function() {
|
|
var ReactDebugTool;
|
|
|
|
beforeEach(function() {
|
|
jest.resetModuleRegistry();
|
|
ReactDebugTool = require('ReactDebugTool');
|
|
});
|
|
|
|
it('should add and remove hooks', () => {
|
|
var handler1 = jasmine.createSpy('spy');
|
|
var handler2 = jasmine.createSpy('spy');
|
|
var hook1 = {onTestEvent: handler1};
|
|
var hook2 = {onTestEvent: handler2};
|
|
|
|
ReactDebugTool.addHook(hook1);
|
|
ReactDebugTool.onTestEvent();
|
|
expect(handler1.calls.count()).toBe(1);
|
|
expect(handler2.calls.count()).toBe(0);
|
|
|
|
ReactDebugTool.onTestEvent();
|
|
expect(handler1.calls.count()).toBe(2);
|
|
expect(handler2.calls.count()).toBe(0);
|
|
|
|
ReactDebugTool.addHook(hook2);
|
|
ReactDebugTool.onTestEvent();
|
|
expect(handler1.calls.count()).toBe(3);
|
|
expect(handler2.calls.count()).toBe(1);
|
|
|
|
ReactDebugTool.onTestEvent();
|
|
expect(handler1.calls.count()).toBe(4);
|
|
expect(handler2.calls.count()).toBe(2);
|
|
|
|
ReactDebugTool.removeHook(hook1);
|
|
ReactDebugTool.onTestEvent();
|
|
expect(handler1.calls.count()).toBe(4);
|
|
expect(handler2.calls.count()).toBe(3);
|
|
|
|
ReactDebugTool.removeHook(hook2);
|
|
ReactDebugTool.onTestEvent();
|
|
expect(handler1.calls.count()).toBe(4);
|
|
expect(handler2.calls.count()).toBe(3);
|
|
});
|
|
|
|
it('warns once when an error is thrown in hook', () => {
|
|
spyOn(console, 'error');
|
|
ReactDebugTool.addHook({
|
|
onTestEvent() {
|
|
throw new Error('Hi.');
|
|
},
|
|
});
|
|
|
|
ReactDebugTool.onTestEvent();
|
|
expect(console.error.calls.count()).toBe(1);
|
|
expect(console.error.calls.argsFor(0)[0]).toContain(
|
|
'Exception thrown by hook while handling ' +
|
|
'onTestEvent: Error: Hi.'
|
|
);
|
|
|
|
ReactDebugTool.onTestEvent();
|
|
expect(console.error.calls.count()).toBe(1);
|
|
});
|
|
|
|
it('returns isProfiling state', () => {
|
|
expect(ReactDebugTool.isProfiling()).toBe(false);
|
|
|
|
ReactDebugTool.beginProfiling();
|
|
expect(ReactDebugTool.isProfiling()).toBe(true);
|
|
|
|
ReactDebugTool.endProfiling();
|
|
expect(ReactDebugTool.isProfiling()).toBe(false);
|
|
});
|
|
});
|