@samitouri / QOS-React / commits / f89f94d145

Add tests for ReactDebugTool and ReactDOMDebugTool

Dan Abramov committed Apr 25, 2016 at 00:41 UTC f89f94d1451a8a65dbaca97a7566c186ca26e6e0
4 files changed +156
src/isomorphic/ReactDebugTool.js
+3
@@ -70,6 +70,9 @@ var ReactDebugTool = {
70 onUnmountComponent(internalInstance) {
71 emitEvent('onUnmountComponent', internalInstance);
72 },
73 + onTestEvent() {
74 + emitEvent('onTestEvent');
75 + },
76 };
77
78 ReactDebugTool.addDevtool(ReactInvalidSetStateWarningDevTool);
src/isomorphic/__tests__/ReactDebugTool-test.js new
+75
@@ -0,0 +1,75 @@
1 +/**
2 + * Copyright 2016-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 +describe('ReactDebugTool', function() {
15 + var ReactDebugTool;
16 +
17 + beforeEach(function() {
18 + jest.resetModuleRegistry();
19 + ReactDebugTool = require('ReactDebugTool');
20 + });
21 +
22 + it('should add and remove devtools', () => {
23 + var handler1 = jasmine.createSpy('spy');
24 + var handler2 = jasmine.createSpy('spy');
25 + var devtool1 = {onTestEvent: handler1};
26 + var devtool2 = {onTestEvent: handler2};
27 +
28 + ReactDebugTool.addDevtool(devtool1);
29 + ReactDebugTool.onTestEvent();
30 + expect(handler1.calls.length).toBe(1);
31 + expect(handler2.calls.length).toBe(0);
32 +
33 + ReactDebugTool.onTestEvent();
34 + expect(handler1.calls.length).toBe(2);
35 + expect(handler2.calls.length).toBe(0);
36 +
37 + ReactDebugTool.addDevtool(devtool2);
38 + ReactDebugTool.onTestEvent();
39 + expect(handler1.calls.length).toBe(3);
40 + expect(handler2.calls.length).toBe(1);
41 +
42 + ReactDebugTool.onTestEvent();
43 + expect(handler1.calls.length).toBe(4);
44 + expect(handler2.calls.length).toBe(2);
45 +
46 + ReactDebugTool.removeDevtool(devtool1);
47 + ReactDebugTool.onTestEvent();
48 + expect(handler1.calls.length).toBe(4);
49 + expect(handler2.calls.length).toBe(3);
50 +
51 + ReactDebugTool.removeDevtool(devtool2);
52 + ReactDebugTool.onTestEvent();
53 + expect(handler1.calls.length).toBe(4);
54 + expect(handler2.calls.length).toBe(3);
55 + });
56 +
57 + it('warns once when an error is thrown in devtool', () => {
58 + spyOn(console, 'error');
59 + ReactDebugTool.addDevtool({
60 + onTestEvent() {
61 + throw new Error('Hi.');
62 + },
63 + });
64 +
65 + ReactDebugTool.onTestEvent();
66 + expect(console.error.calls.length).toBe(1);
67 + expect(console.error.argsForCall[0][0]).toContain(
68 + 'exception thrown by devtool while handling ' +
69 + 'onTestEvent: Hi.'
70 + );
71 +
72 + ReactDebugTool.onTestEvent();
73 + expect(console.error.calls.length).toBe(1);
74 + });
75 +});
src/renderers/dom/shared/ReactDOMDebugTool.js
+3
@@ -59,6 +59,9 @@ var ReactDOMDebugTool = {
59 onDeleteValueForProperty(node, name) {
60 emitEvent('onDeleteValueForProperty', node, name);
61 },
62 + onTestEvent() {
63 + emitEvent('onTestEvent');
64 + },
65 };
66
67 ReactDOMDebugTool.addDevtool(ReactDOMUnknownPropertyDevtool);
src/renderers/dom/shared/__tests__/ReactDOMDebugTool-test.js new
+75
@@ -0,0 +1,75 @@
1 +/**
2 + * Copyright 2016-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 +describe('ReactDOMDebugTool', function() {
15 + var ReactDOMDebugTool;
16 +
17 + beforeEach(function() {
18 + jest.resetModuleRegistry();
19 + ReactDOMDebugTool = require('ReactDOMDebugTool');
20 + });
21 +
22 + it('should add and remove devtools', () => {
23 + var handler1 = jasmine.createSpy('spy');
24 + var handler2 = jasmine.createSpy('spy');
25 + var devtool1 = {onTestEvent: handler1};
26 + var devtool2 = {onTestEvent: handler2};
27 +
28 + ReactDOMDebugTool.addDevtool(devtool1);
29 + ReactDOMDebugTool.onTestEvent();
30 + expect(handler1.calls.length).toBe(1);
31 + expect(handler2.calls.length).toBe(0);
32 +
33 + ReactDOMDebugTool.onTestEvent();
34 + expect(handler1.calls.length).toBe(2);
35 + expect(handler2.calls.length).toBe(0);
36 +
37 + ReactDOMDebugTool.addDevtool(devtool2);
38 + ReactDOMDebugTool.onTestEvent();
39 + expect(handler1.calls.length).toBe(3);
40 + expect(handler2.calls.length).toBe(1);
41 +
42 + ReactDOMDebugTool.onTestEvent();
43 + expect(handler1.calls.length).toBe(4);
44 + expect(handler2.calls.length).toBe(2);
45 +
46 + ReactDOMDebugTool.removeDevtool(devtool1);
47 + ReactDOMDebugTool.onTestEvent();
48 + expect(handler1.calls.length).toBe(4);
49 + expect(handler2.calls.length).toBe(3);
50 +
51 + ReactDOMDebugTool.removeDevtool(devtool2);
52 + ReactDOMDebugTool.onTestEvent();
53 + expect(handler1.calls.length).toBe(4);
54 + expect(handler2.calls.length).toBe(3);
55 + });
56 +
57 + it('warns once when an error is thrown in devtool', () => {
58 + spyOn(console, 'error');
59 + ReactDOMDebugTool.addDevtool({
60 + onTestEvent() {
61 + throw new Error('Hi.');
62 + },
63 + });
64 +
65 + ReactDOMDebugTool.onTestEvent();
66 + expect(console.error.calls.length).toBe(1);
67 + expect(console.error.argsForCall[0][0]).toContain(
68 + 'exception thrown by devtool while handling ' +
69 + 'onTestEvent: Hi.'
70 + );
71 +
72 + ReactDOMDebugTool.onTestEvent();
73 + expect(console.error.calls.length).toBe(1);
74 + });
75 +});