Run ReactBrowserEventEmitter test on bundles (#19225)
* Run ReactBrowserEventEmitter test on bundles * Rm bad tests
Dan Abramov committed
Jul 1, 2020 at 16:01 UTC
5707eeb2d2f13983b12cf56a0cdf242a21972dfe
4 files changed
+4
-211
packages/legacy-events/__tests__/accumulate-test.internal.js
deleted
-49
@@ -1,49 +0,0 @@
1
-/**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- * @emails react-core
8
- */
9
-
10
-'use strict';
11
-
12
-let accumulate;
13
-
14
-describe('accumulate', () => {
15
- beforeEach(() => {
16
- accumulate = require('legacy-events/accumulate').default;
17
- });
18
-
19
- it('throws if the second item is null', () => {
20
- expect(function() {
21
- accumulate([], null);
22
- }).toThrowError(
23
- 'accumulate(...): Accumulated items must not be null or undefined.',
24
- );
25
- });
26
-
27
- it('return second item if first item is null', () => {
28
- const a = [];
29
- expect(accumulate(null, a)).toBe(a);
30
- });
31
-
32
- it('return concatenation of items if first item is an array', () => {
33
- const a = ['hello'];
34
- const b = 'world';
35
- expect(accumulate(a, b)).toEqual(['hello', 'world']);
36
- });
37
-
38
- it('return concatenation of items if second item is an array', () => {
39
- const a = 'hello';
40
- const b = ['world'];
41
- expect(accumulate(a, b)).toEqual(['hello', 'world']);
42
- });
43
-
44
- it('return an array containing both items if neither item is an array', () => {
45
- const a = 'hello';
46
- const b = 'world';
47
- expect(accumulate(a, b)).toEqual(['hello', 'world']);
48
- });
49
-});
packages/legacy-events/__tests__/accumulateInto-test.internal.js
deleted
-49
@@ -1,49 +0,0 @@
1
-/**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- * @emails react-core
8
- */
9
-
10
-'use strict';
11
-
12
-let accumulateInto;
13
-
14
-describe('accumulateInto', () => {
15
- beforeEach(() => {
16
- accumulateInto = require('legacy-events/accumulateInto').default;
17
- });
18
-
19
- it('throws if the second item is null', () => {
20
- expect(function() {
21
- accumulateInto([], null);
22
- }).toThrowError(
23
- 'accumulateInto(...): Accumulated items must not be null or undefined.',
24
- );
25
- });
26
-
27
- it('returns the second item if first is null', () => {
28
- const a = [];
29
- expect(accumulateInto(null, a)).toBe(a);
30
- });
31
-
32
- it('merges the second into the first if first item is an array', () => {
33
- const a = [1, 2];
34
- const b = [3, 4];
35
- accumulateInto(a, b);
36
- expect(a).toEqual([1, 2, 3, 4]);
37
- expect(b).toEqual([3, 4]);
38
- const c = [1];
39
- accumulateInto(c, 2);
40
- expect(c).toEqual([1, 2]);
41
- });
42
-
43
- it('returns a new array if first or both items are scalar', () => {
44
- const a = [2];
45
- expect(accumulateInto(1, a)).toEqual([1, 2]);
46
- expect(a).toEqual([2]);
47
- expect(accumulateInto(1, 2)).toEqual([1, 2]);
48
- });
49
-});
packages/react-dom/src/__tests__/ReactBrowserEventEmitter-test.js
renamed
+2
-113
@@ -9,15 +9,9 @@
9
10
'use strict';
11
12
-let EventPluginGetListener;
13
-let EventPluginRegistry;
12
let React;
13
let ReactDOM;
16
-let ReactDOMComponentTree;
17
-let listenToEvent;
18
-let ReactDOMEventListener;
14
let ReactTestUtils;
20
-let ReactFeatureFlags;
15
16
let idCallOrder;
17
const recordID = function(id) {
@@ -33,7 +27,6 @@ const recordIDAndReturnFalse = function(id, event) {
27
};
28
const LISTENER = jest.fn();
29
const ON_CLICK_KEY = 'onClick';
36
-const ON_CHANGE_KEY = 'onChange';
30
const ON_MOUSE_ENTER_KEY = 'onMouseEnter';
31
32
let GRANDPARENT;
@@ -41,42 +34,20 @@ let PARENT;
34
let CHILD;
35
let BUTTON;
36
44
-let getListener;
37
let putListener;
38
let deleteAllListeners;
39
40
let container;
41
50
-function registerSimpleTestHandler() {
51
- putListener(CHILD, ON_CLICK_KEY, LISTENER);
52
- const listener = getListener(CHILD, ON_CLICK_KEY);
53
- expect(listener).toEqual(LISTENER);
54
- return getListener(CHILD, ON_CLICK_KEY);
55
-}
56
-
57
-// We should probably remove this file at some point, it's just full of
58
-// internal API usage.
42
+// This test is written in a bizarre way because it was previously using internals.
43
+// It should probably be rewritten but we're keeping it for some extra coverage.
44
describe('ReactBrowserEventEmitter', () => {
45
beforeEach(() => {
46
jest.resetModules();
47
LISTENER.mockClear();
48
64
- ReactFeatureFlags = require('shared/ReactFeatureFlags');
65
- EventPluginGetListener = require('react-dom/src/events/getListener')
66
- .default;
67
- EventPluginRegistry = require('legacy-events/EventPluginRegistry');
49
React = require('react');
50
ReactDOM = require('react-dom');
70
- ReactDOMComponentTree = require('../client/ReactDOMComponentTree');
71
- if (ReactFeatureFlags.enableModernEventSystem) {
72
- listenToEvent = require('../events/DOMModernPluginEventSystem')
73
- .listenToEvent;
74
- } else {
75
- listenToEvent = require('../events/DOMLegacyEventPluginSystem')
76
- .legacyListenToEvent;
77
- }
78
-
79
- ReactDOMEventListener = require('../events/ReactDOMEventListener');
51
ReactTestUtils = require('react-dom/test-utils');
52
53
container = document.createElement('div');
@@ -111,10 +82,6 @@ describe('ReactBrowserEventEmitter', () => {
82
83
renderTree();
84
114
- getListener = function(node, eventName) {
115
- const inst = ReactDOMComponentTree.getInstanceFromNode(node);
116
- return EventPluginGetListener(inst, eventName);
117
- };
85
putListener = function(node, eventName, listener) {
86
switch (node) {
87
case CHILD:
@@ -159,47 +126,6 @@ describe('ReactBrowserEventEmitter', () => {
126
container = null;
127
});
128
162
- it('should store a listener correctly', () => {
163
- registerSimpleTestHandler();
164
- const listener = getListener(CHILD, ON_CLICK_KEY);
165
- expect(listener).toBe(LISTENER);
166
- });
167
-
168
- it('should retrieve a listener correctly', () => {
169
- registerSimpleTestHandler();
170
- const listener = getListener(CHILD, ON_CLICK_KEY);
171
- expect(listener).toEqual(LISTENER);
172
- });
173
-
174
- it('should not retrieve listeners on a disabled interactive element', () => {
175
- putListener(BUTTON, ON_MOUSE_ENTER_KEY, recordID.bind(null, BUTTON));
176
- const listener = getListener(BUTTON, ON_MOUSE_ENTER_KEY);
177
- expect(listener).toBe(null);
178
- });
179
-
180
- it('should clear all handlers when asked to', () => {
181
- registerSimpleTestHandler();
182
- deleteAllListeners(CHILD);
183
- const listener = getListener(CHILD, ON_CLICK_KEY);
184
- expect(listener).toBe(undefined);
185
- });
186
-
187
- it('should invoke a simple handler registered on a node', () => {
188
- registerSimpleTestHandler();
189
- CHILD.click();
190
- expect(LISTENER).toHaveBeenCalledTimes(1);
191
- });
192
-
193
- it('should not invoke handlers if ReactDOMEventListener is disabled', () => {
194
- registerSimpleTestHandler();
195
- ReactDOMEventListener.setEnabled(false);
196
- CHILD.click();
197
- expect(LISTENER).toHaveBeenCalledTimes(0);
198
- ReactDOMEventListener.setEnabled(true);
199
- CHILD.click();
200
- expect(LISTENER).toHaveBeenCalledTimes(1);
201
- });
202
-
129
it('should bubble simply', () => {
130
putListener(CHILD, ON_CLICK_KEY, recordID.bind(null, CHILD));
131
putListener(PARENT, ON_CLICK_KEY, recordID.bind(null, PARENT));
@@ -356,41 +282,4 @@ describe('ReactBrowserEventEmitter', () => {
282
expect(idCallOrder.length).toBe(1);
283
expect(idCallOrder[0]).toBe(CHILD);
284
});
359
-
360
- it('should listen to events only once', () => {
361
- spyOnDevAndProd(EventTarget.prototype, 'addEventListener');
362
- listenToEvent(ON_CLICK_KEY, document);
363
- listenToEvent(ON_CLICK_KEY, document);
364
- expect(EventTarget.prototype.addEventListener).toHaveBeenCalledTimes(1);
365
- });
366
-
367
- it('should work with event plugins without dependencies', () => {
368
- spyOnDevAndProd(EventTarget.prototype, 'addEventListener');
369
-
370
- listenToEvent(ON_CLICK_KEY, document);
371
-
372
- expect(EventTarget.prototype.addEventListener.calls.argsFor(0)[0]).toBe(
373
- 'click',
374
- );
375
- });
376
-
377
- it('should work with event plugins with dependencies', () => {
378
- spyOnDevAndProd(EventTarget.prototype, 'addEventListener');
379
-
380
- listenToEvent(ON_CHANGE_KEY, document);
381
-
382
- const setEventListeners = [];
383
- const listenCalls = EventTarget.prototype.addEventListener.calls.allArgs();
384
- for (let i = 0; i < listenCalls.length; i++) {
385
- setEventListeners.push(listenCalls[i][1]);
386
- }
387
-
388
- const module = EventPluginRegistry.registrationNameModules[ON_CHANGE_KEY];
389
- const dependencies = module.eventTypes.change.dependencies;
390
- expect(setEventListeners.length).toEqual(dependencies.length);
391
-
392
- for (let i = 0; i < setEventListeners.length; i++) {
393
- expect(dependencies.indexOf(setEventListeners[i])).toBeTruthy();
394
- }
395
- });
285
});
packages/react-dom/src/events/ReactDOMEventListener.js
+2
@@ -67,6 +67,8 @@ const {
67
// TODO: can we stop exporting these?
68
export let _enabled = true;
69
70
+// This is exported in FB builds for use by legacy FB layer infra.
71
+// We'd like to remove this but it's not clear if this is safe.
72
export function setEnabled(enabled: ?boolean) {
73
_enabled = !!enabled;
74
}