Compare commits

...

2 Commits
pr35173 ... leg

Author SHA1 Message Date
Dan Abramov
8e99618afa Rm bad tests 2020-07-01 15:59:26 +01:00
Dan Abramov
d0e6cbc3b0 Run ReactBrowserEventEmitter test on bundles 2020-07-01 15:52:06 +01:00
4 changed files with 4 additions and 211 deletions

View File

@@ -1,49 +0,0 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/
'use strict';
let accumulate;
describe('accumulate', () => {
beforeEach(() => {
accumulate = require('legacy-events/accumulate').default;
});
it('throws if the second item is null', () => {
expect(function() {
accumulate([], null);
}).toThrowError(
'accumulate(...): Accumulated items must not be null or undefined.',
);
});
it('return second item if first item is null', () => {
const a = [];
expect(accumulate(null, a)).toBe(a);
});
it('return concatenation of items if first item is an array', () => {
const a = ['hello'];
const b = 'world';
expect(accumulate(a, b)).toEqual(['hello', 'world']);
});
it('return concatenation of items if second item is an array', () => {
const a = 'hello';
const b = ['world'];
expect(accumulate(a, b)).toEqual(['hello', 'world']);
});
it('return an array containing both items if neither item is an array', () => {
const a = 'hello';
const b = 'world';
expect(accumulate(a, b)).toEqual(['hello', 'world']);
});
});

View File

@@ -1,49 +0,0 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/
'use strict';
let accumulateInto;
describe('accumulateInto', () => {
beforeEach(() => {
accumulateInto = require('legacy-events/accumulateInto').default;
});
it('throws if the second item is null', () => {
expect(function() {
accumulateInto([], null);
}).toThrowError(
'accumulateInto(...): Accumulated items must not be null or undefined.',
);
});
it('returns the second item if first is null', () => {
const a = [];
expect(accumulateInto(null, a)).toBe(a);
});
it('merges the second into the first if first item is an array', () => {
const a = [1, 2];
const b = [3, 4];
accumulateInto(a, b);
expect(a).toEqual([1, 2, 3, 4]);
expect(b).toEqual([3, 4]);
const c = [1];
accumulateInto(c, 2);
expect(c).toEqual([1, 2]);
});
it('returns a new array if first or both items are scalar', () => {
const a = [2];
expect(accumulateInto(1, a)).toEqual([1, 2]);
expect(a).toEqual([2]);
expect(accumulateInto(1, 2)).toEqual([1, 2]);
});
});

View File

@@ -9,15 +9,9 @@
'use strict';
let EventPluginGetListener;
let EventPluginRegistry;
let React;
let ReactDOM;
let ReactDOMComponentTree;
let listenToEvent;
let ReactDOMEventListener;
let ReactTestUtils;
let ReactFeatureFlags;
let idCallOrder;
const recordID = function(id) {
@@ -33,7 +27,6 @@ const recordIDAndReturnFalse = function(id, event) {
};
const LISTENER = jest.fn();
const ON_CLICK_KEY = 'onClick';
const ON_CHANGE_KEY = 'onChange';
const ON_MOUSE_ENTER_KEY = 'onMouseEnter';
let GRANDPARENT;
@@ -41,42 +34,20 @@ let PARENT;
let CHILD;
let BUTTON;
let getListener;
let putListener;
let deleteAllListeners;
let container;
function registerSimpleTestHandler() {
putListener(CHILD, ON_CLICK_KEY, LISTENER);
const listener = getListener(CHILD, ON_CLICK_KEY);
expect(listener).toEqual(LISTENER);
return getListener(CHILD, ON_CLICK_KEY);
}
// We should probably remove this file at some point, it's just full of
// internal API usage.
// This test is written in a bizarre way because it was previously using internals.
// It should probably be rewritten but we're keeping it for some extra coverage.
describe('ReactBrowserEventEmitter', () => {
beforeEach(() => {
jest.resetModules();
LISTENER.mockClear();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
EventPluginGetListener = require('react-dom/src/events/getListener')
.default;
EventPluginRegistry = require('legacy-events/EventPluginRegistry');
React = require('react');
ReactDOM = require('react-dom');
ReactDOMComponentTree = require('../client/ReactDOMComponentTree');
if (ReactFeatureFlags.enableModernEventSystem) {
listenToEvent = require('../events/DOMModernPluginEventSystem')
.listenToEvent;
} else {
listenToEvent = require('../events/DOMLegacyEventPluginSystem')
.legacyListenToEvent;
}
ReactDOMEventListener = require('../events/ReactDOMEventListener');
ReactTestUtils = require('react-dom/test-utils');
container = document.createElement('div');
@@ -111,10 +82,6 @@ describe('ReactBrowserEventEmitter', () => {
renderTree();
getListener = function(node, eventName) {
const inst = ReactDOMComponentTree.getInstanceFromNode(node);
return EventPluginGetListener(inst, eventName);
};
putListener = function(node, eventName, listener) {
switch (node) {
case CHILD:
@@ -159,47 +126,6 @@ describe('ReactBrowserEventEmitter', () => {
container = null;
});
it('should store a listener correctly', () => {
registerSimpleTestHandler();
const listener = getListener(CHILD, ON_CLICK_KEY);
expect(listener).toBe(LISTENER);
});
it('should retrieve a listener correctly', () => {
registerSimpleTestHandler();
const listener = getListener(CHILD, ON_CLICK_KEY);
expect(listener).toEqual(LISTENER);
});
it('should not retrieve listeners on a disabled interactive element', () => {
putListener(BUTTON, ON_MOUSE_ENTER_KEY, recordID.bind(null, BUTTON));
const listener = getListener(BUTTON, ON_MOUSE_ENTER_KEY);
expect(listener).toBe(null);
});
it('should clear all handlers when asked to', () => {
registerSimpleTestHandler();
deleteAllListeners(CHILD);
const listener = getListener(CHILD, ON_CLICK_KEY);
expect(listener).toBe(undefined);
});
it('should invoke a simple handler registered on a node', () => {
registerSimpleTestHandler();
CHILD.click();
expect(LISTENER).toHaveBeenCalledTimes(1);
});
it('should not invoke handlers if ReactDOMEventListener is disabled', () => {
registerSimpleTestHandler();
ReactDOMEventListener.setEnabled(false);
CHILD.click();
expect(LISTENER).toHaveBeenCalledTimes(0);
ReactDOMEventListener.setEnabled(true);
CHILD.click();
expect(LISTENER).toHaveBeenCalledTimes(1);
});
it('should bubble simply', () => {
putListener(CHILD, ON_CLICK_KEY, recordID.bind(null, CHILD));
putListener(PARENT, ON_CLICK_KEY, recordID.bind(null, PARENT));
@@ -356,41 +282,4 @@ describe('ReactBrowserEventEmitter', () => {
expect(idCallOrder.length).toBe(1);
expect(idCallOrder[0]).toBe(CHILD);
});
it('should listen to events only once', () => {
spyOnDevAndProd(EventTarget.prototype, 'addEventListener');
listenToEvent(ON_CLICK_KEY, document);
listenToEvent(ON_CLICK_KEY, document);
expect(EventTarget.prototype.addEventListener).toHaveBeenCalledTimes(1);
});
it('should work with event plugins without dependencies', () => {
spyOnDevAndProd(EventTarget.prototype, 'addEventListener');
listenToEvent(ON_CLICK_KEY, document);
expect(EventTarget.prototype.addEventListener.calls.argsFor(0)[0]).toBe(
'click',
);
});
it('should work with event plugins with dependencies', () => {
spyOnDevAndProd(EventTarget.prototype, 'addEventListener');
listenToEvent(ON_CHANGE_KEY, document);
const setEventListeners = [];
const listenCalls = EventTarget.prototype.addEventListener.calls.allArgs();
for (let i = 0; i < listenCalls.length; i++) {
setEventListeners.push(listenCalls[i][1]);
}
const module = EventPluginRegistry.registrationNameModules[ON_CHANGE_KEY];
const dependencies = module.eventTypes.change.dependencies;
expect(setEventListeners.length).toEqual(dependencies.length);
for (let i = 0; i < setEventListeners.length; i++) {
expect(dependencies.indexOf(setEventListeners[i])).toBeTruthy();
}
});
});

View File

@@ -67,6 +67,8 @@ const {
// TODO: can we stop exporting these?
export let _enabled = true;
// This is exported in FB builds for use by legacy FB layer infra.
// We'd like to remove this but it's not clear if this is safe.
export function setEnabled(enabled: ?boolean) {
_enabled = !!enabled;
}