unit tests, and some cleaning up

This commit is contained in:
Josh Burman
2019-03-14 10:14:42 -04:00
parent 501a14d713
commit 21f77fa4c3
134 changed files with 16787 additions and 359 deletions

34
node_modules/mock-socket/src/event/close.js generated vendored Normal file
View File

@ -0,0 +1,34 @@
import EventPrototype from './prototype';
import { ERROR_PREFIX } from '../constants';
export default class CloseEvent extends EventPrototype {
constructor(type, eventInitConfig = {}) {
super();
if (!type) {
throw new TypeError(`${ERROR_PREFIX.EVENT.CLOSE} 1 argument required, but only 0 present.`);
}
if (typeof eventInitConfig !== 'object') {
throw new TypeError(`${ERROR_PREFIX.EVENT.CLOSE} parameter 2 ('eventInitDict') is not an object`);
}
const { bubbles, cancelable, code, reason, wasClean } = eventInitConfig;
this.type = `${type}`;
this.timeStamp = Date.now();
this.target = null;
this.srcElement = null;
this.returnValue = true;
this.isTrusted = false;
this.eventPhase = 0;
this.defaultPrevented = false;
this.currentTarget = null;
this.cancelable = cancelable ? Boolean(cancelable) : false;
this.cancelBubble = false;
this.bubbles = bubbles ? Boolean(bubbles) : false;
this.code = typeof code === 'number' ? parseInt(code, 10) : 0;
this.reason = `${reason || ''}`;
this.wasClean = wasClean ? Boolean(wasClean) : false;
}
}

31
node_modules/mock-socket/src/event/event.js generated vendored Normal file
View File

@ -0,0 +1,31 @@
import EventPrototype from './prototype';
import { ERROR_PREFIX } from '../constants';
export default class Event extends EventPrototype {
constructor(type, eventInitConfig = {}) {
super();
if (!type) {
throw new TypeError(`${ERROR_PREFIX.EVENT_ERROR} 1 argument required, but only 0 present.`);
}
if (typeof eventInitConfig !== 'object') {
throw new TypeError(`${ERROR_PREFIX.EVENT_ERROR} parameter 2 ('eventInitDict') is not an object.`);
}
const { bubbles, cancelable } = eventInitConfig;
this.type = `${type}`;
this.timeStamp = Date.now();
this.target = null;
this.srcElement = null;
this.returnValue = true;
this.isTrusted = false;
this.eventPhase = 0;
this.defaultPrevented = false;
this.currentTarget = null;
this.cancelable = cancelable ? Boolean(cancelable) : false;
this.canncelBubble = false;
this.bubbles = bubbles ? Boolean(bubbles) : false;
}
}

75
node_modules/mock-socket/src/event/factory.js generated vendored Normal file
View File

@ -0,0 +1,75 @@
import Event from './event';
import MessageEvent from './message';
import CloseEvent from './close';
/*
* Creates an Event object and extends it to allow full modification of
* its properties.
*
* @param {object} config - within config you will need to pass type and optionally target
*/
function createEvent(config) {
const { type, target } = config;
const eventObject = new Event(type);
if (target) {
eventObject.target = target;
eventObject.srcElement = target;
eventObject.currentTarget = target;
}
return eventObject;
}
/*
* Creates a MessageEvent object and extends it to allow full modification of
* its properties.
*
* @param {object} config - within config: type, origin, data and optionally target
*/
function createMessageEvent(config) {
const { type, origin, data, target } = config;
const messageEvent = new MessageEvent(type, {
data,
origin
});
if (target) {
messageEvent.target = target;
messageEvent.srcElement = target;
messageEvent.currentTarget = target;
}
return messageEvent;
}
/*
* Creates a CloseEvent object and extends it to allow full modification of
* its properties.
*
* @param {object} config - within config: type and optionally target, code, and reason
*/
function createCloseEvent(config) {
const { code, reason, type, target } = config;
let { wasClean } = config;
if (!wasClean) {
wasClean = code === 1000;
}
const closeEvent = new CloseEvent(type, {
code,
reason,
wasClean
});
if (target) {
closeEvent.target = target;
closeEvent.srcElement = target;
closeEvent.currentTarget = target;
}
return closeEvent;
}
export { createEvent, createMessageEvent, createCloseEvent };

35
node_modules/mock-socket/src/event/message.js generated vendored Normal file
View File

@ -0,0 +1,35 @@
import EventPrototype from './prototype';
import { ERROR_PREFIX } from '../constants';
export default class MessageEvent extends EventPrototype {
constructor(type, eventInitConfig = {}) {
super();
if (!type) {
throw new TypeError(`${ERROR_PREFIX.EVENT.MESSAGE} 1 argument required, but only 0 present.`);
}
if (typeof eventInitConfig !== 'object') {
throw new TypeError(`${ERROR_PREFIX.EVENT.MESSAGE} parameter 2 ('eventInitDict') is not an object`);
}
const { bubbles, cancelable, data, origin, lastEventId, ports } = eventInitConfig;
this.type = `${type}`;
this.timeStamp = Date.now();
this.target = null;
this.srcElement = null;
this.returnValue = true;
this.isTrusted = false;
this.eventPhase = 0;
this.defaultPrevented = false;
this.currentTarget = null;
this.cancelable = cancelable ? Boolean(cancelable) : false;
this.canncelBubble = false;
this.bubbles = bubbles ? Boolean(bubbles) : false;
this.origin = `${origin}`;
this.ports = typeof ports === 'undefined' ? null : ports;
this.data = typeof data === 'undefined' ? null : data;
this.lastEventId = `${lastEventId || ''}`;
}
}

13
node_modules/mock-socket/src/event/prototype.js generated vendored Normal file
View File

@ -0,0 +1,13 @@
export default class EventPrototype {
// Noops
stopPropagation() {}
stopImmediatePropagation() {}
// if no arguments are passed then the type is set to "undefined" on
// chrome and safari.
initEvent(type = 'undefined', bubbles = false, cancelable = false) {
this.type = `${type}`;
this.bubbles = Boolean(bubbles);
this.cancelable = Boolean(cancelable);
}
}

73
node_modules/mock-socket/src/event/target.js generated vendored Normal file
View File

@ -0,0 +1,73 @@
import { reject, filter } from '../helpers/array-helpers';
/*
* EventTarget is an interface implemented by objects that can
* receive events and may have listeners for them.
*
* https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
*/
class EventTarget {
constructor() {
this.listeners = {};
}
/*
* Ties a listener function to an event type which can later be invoked via the
* dispatchEvent method.
*
* @param {string} type - the type of event (ie: 'open', 'message', etc.)
* @param {function} listener - the callback function to invoke whenever an event is dispatched matching the given type
* @param {boolean} useCapture - N/A TODO: implement useCapture functionality
*/
addEventListener(type, listener /* , useCapture */) {
if (typeof listener === 'function') {
if (!Array.isArray(this.listeners[type])) {
this.listeners[type] = [];
}
// Only add the same function once
if (filter(this.listeners[type], item => item === listener).length === 0) {
this.listeners[type].push(listener);
}
}
}
/*
* Removes the listener so it will no longer be invoked via the dispatchEvent method.
*
* @param {string} type - the type of event (ie: 'open', 'message', etc.)
* @param {function} listener - the callback function to invoke whenever an event is dispatched matching the given type
* @param {boolean} useCapture - N/A TODO: implement useCapture functionality
*/
removeEventListener(type, removingListener /* , useCapture */) {
const arrayOfListeners = this.listeners[type];
this.listeners[type] = reject(arrayOfListeners, listener => listener === removingListener);
}
/*
* Invokes all listener functions that are listening to the given event.type property. Each
* listener will be passed the event as the first argument.
*
* @param {object} event - event object which will be passed to all listeners of the event.type property
*/
dispatchEvent(event, ...customArguments) {
const eventName = event.type;
const listeners = this.listeners[eventName];
if (!Array.isArray(listeners)) {
return false;
}
listeners.forEach(listener => {
if (customArguments.length > 0) {
listener.apply(this, customArguments);
} else {
listener.call(this, event);
}
});
return true;
}
}
export default EventTarget;