socket.io-mock
Advanced tools
Comparing version 1.2.4 to 1.3.0
@@ -190,2 +190,4 @@ function createCommonjsModule(fn, module) { | ||
this._emitFn = componentEmitter.prototype.emit; | ||
this.connected = true; | ||
this.disconnected = false; | ||
} | ||
@@ -217,2 +219,19 @@ | ||
} | ||
/** | ||
* Close the socket | ||
*/ | ||
close () { | ||
this.disconnected = true; | ||
this.connected = false; | ||
this.emit('disconnect', 'io client disconnect'); | ||
return this | ||
} | ||
/** | ||
* Disconnet the socket alias for close | ||
*/ | ||
disconnect () { | ||
return this.close() | ||
} | ||
} | ||
@@ -311,4 +330,14 @@ | ||
} | ||
/** | ||
* Closing the socket server | ||
* @param {Function} cb | ||
*/ | ||
disconnect () { | ||
this.emit('disconnecting', 'io server disconnect'); | ||
this.emit('disconnect', 'io server disconnect'); | ||
return this | ||
} | ||
} | ||
export default SocketMock; |
@@ -1,1 +0,342 @@ | ||
"use strict";var t=function(t,e){return t(e={exports:{}},e.exports),e.exports}((function(t){function e(t){if(t)return function(t){for(var s in e.prototype)t[s]=e.prototype[s];return t}(t)}t.exports=e,e.prototype.on=e.prototype.addEventListener=function(t,e){return this._callbacks=this._callbacks||{},(this._callbacks["$"+t]=this._callbacks["$"+t]||[]).push(e),this},e.prototype.once=function(t,e){function s(){this.off(t,s),e.apply(this,arguments)}return s.fn=e,this.on(t,s),this},e.prototype.off=e.prototype.removeListener=e.prototype.removeAllListeners=e.prototype.removeEventListener=function(t,e){if(this._callbacks=this._callbacks||{},0==arguments.length)return this._callbacks={},this;var s,i=this._callbacks["$"+t];if(!i)return this;if(1==arguments.length)return delete this._callbacks["$"+t],this;for(var n=0;n<i.length;n++)if((s=i[n])===e||s.fn===e){i.splice(n,1);break}return 0===i.length&&delete this._callbacks["$"+t],this},e.prototype.emit=function(t){this._callbacks=this._callbacks||{};for(var e=new Array(arguments.length-1),s=this._callbacks["$"+t],i=1;i<arguments.length;i++)e[i-1]=arguments[i];if(s){i=0;for(var n=(s=s.slice(0)).length;i<n;++i)s[i].apply(this,e)}return this},e.prototype.listeners=function(t){return this._callbacks=this._callbacks||{},this._callbacks["$"+t]||[]},e.prototype.hasListeners=function(t){return!!this.listeners(t).length}}));class e extends t{constructor(e){super(),this._socketMock=e,this._emitFn=t.prototype.emit}emit(t,e,s){"function"==typeof e&&(s=e=null),(s||function(){})(this._socketMock.emitEvent(t,e))}fireEvent(t,e){this._emitFn(t,e)}}const s=function(t){return t?JSON.parse(JSON.stringify(t)):void 0};module.exports=class extends t{constructor(){super(),this.joinedRooms=this.rooms=[],this.socketClient=new e(this),this._emitFn=t.prototype.emit,this.generalCallbacks={},this.broadcast={to:t=>({emit:(e,i)=>{this.generalCallbacks[e]&&this.generalCallbacks[e](s(i),t)}})}}emitEvent(t,e){this._emitFn(t,s(e))}onEmit(t,e){this.generalCallbacks[t]=e}emit(t,e){this.socketClient.fireEvent(t,e)}join(t){this.joinedRooms.push(t)}leave(t){const e=this.joinedRooms.indexOf(t);this.joinedRooms.splice(e,1)}monitor(t){return t}}; | ||
'use strict'; | ||
function createCommonjsModule(fn, module) { | ||
return module = { exports: {} }, fn(module, module.exports), module.exports; | ||
} | ||
var componentEmitter = createCommonjsModule(function (module) { | ||
/** | ||
* Expose `Emitter`. | ||
*/ | ||
{ | ||
module.exports = Emitter; | ||
} | ||
/** | ||
* Initialize a new `Emitter`. | ||
* | ||
* @api public | ||
*/ | ||
function Emitter(obj) { | ||
if (obj) return mixin(obj); | ||
} | ||
/** | ||
* Mixin the emitter properties. | ||
* | ||
* @param {Object} obj | ||
* @return {Object} | ||
* @api private | ||
*/ | ||
function mixin(obj) { | ||
for (var key in Emitter.prototype) { | ||
obj[key] = Emitter.prototype[key]; | ||
} | ||
return obj; | ||
} | ||
/** | ||
* Listen on the given `event` with `fn`. | ||
* | ||
* @param {String} event | ||
* @param {Function} fn | ||
* @return {Emitter} | ||
* @api public | ||
*/ | ||
Emitter.prototype.on = | ||
Emitter.prototype.addEventListener = function(event, fn){ | ||
this._callbacks = this._callbacks || {}; | ||
(this._callbacks['$' + event] = this._callbacks['$' + event] || []) | ||
.push(fn); | ||
return this; | ||
}; | ||
/** | ||
* Adds an `event` listener that will be invoked a single | ||
* time then automatically removed. | ||
* | ||
* @param {String} event | ||
* @param {Function} fn | ||
* @return {Emitter} | ||
* @api public | ||
*/ | ||
Emitter.prototype.once = function(event, fn){ | ||
function on() { | ||
this.off(event, on); | ||
fn.apply(this, arguments); | ||
} | ||
on.fn = fn; | ||
this.on(event, on); | ||
return this; | ||
}; | ||
/** | ||
* Remove the given callback for `event` or all | ||
* registered callbacks. | ||
* | ||
* @param {String} event | ||
* @param {Function} fn | ||
* @return {Emitter} | ||
* @api public | ||
*/ | ||
Emitter.prototype.off = | ||
Emitter.prototype.removeListener = | ||
Emitter.prototype.removeAllListeners = | ||
Emitter.prototype.removeEventListener = function(event, fn){ | ||
this._callbacks = this._callbacks || {}; | ||
// all | ||
if (0 == arguments.length) { | ||
this._callbacks = {}; | ||
return this; | ||
} | ||
// specific event | ||
var callbacks = this._callbacks['$' + event]; | ||
if (!callbacks) return this; | ||
// remove all handlers | ||
if (1 == arguments.length) { | ||
delete this._callbacks['$' + event]; | ||
return this; | ||
} | ||
// remove specific handler | ||
var cb; | ||
for (var i = 0; i < callbacks.length; i++) { | ||
cb = callbacks[i]; | ||
if (cb === fn || cb.fn === fn) { | ||
callbacks.splice(i, 1); | ||
break; | ||
} | ||
} | ||
// Remove event specific arrays for event types that no | ||
// one is subscribed for to avoid memory leak. | ||
if (callbacks.length === 0) { | ||
delete this._callbacks['$' + event]; | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Emit `event` with the given args. | ||
* | ||
* @param {String} event | ||
* @param {Mixed} ... | ||
* @return {Emitter} | ||
*/ | ||
Emitter.prototype.emit = function(event){ | ||
this._callbacks = this._callbacks || {}; | ||
var args = new Array(arguments.length - 1) | ||
, callbacks = this._callbacks['$' + event]; | ||
for (var i = 1; i < arguments.length; i++) { | ||
args[i - 1] = arguments[i]; | ||
} | ||
if (callbacks) { | ||
callbacks = callbacks.slice(0); | ||
for (var i = 0, len = callbacks.length; i < len; ++i) { | ||
callbacks[i].apply(this, args); | ||
} | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Return array of callbacks for `event`. | ||
* | ||
* @param {String} event | ||
* @return {Array} | ||
* @api public | ||
*/ | ||
Emitter.prototype.listeners = function(event){ | ||
this._callbacks = this._callbacks || {}; | ||
return this._callbacks['$' + event] || []; | ||
}; | ||
/** | ||
* Check if this emitter has `event` handlers. | ||
* | ||
* @param {String} event | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
Emitter.prototype.hasListeners = function(event){ | ||
return !! this.listeners(event).length; | ||
}; | ||
}); | ||
class SocketClient extends componentEmitter { | ||
/** | ||
* A mocking class for the Socket IO Client side | ||
* @param {SocketMock} socketMock | ||
*/ | ||
constructor (socketMock) { | ||
super(); | ||
this._socketMock = socketMock; | ||
this._emitFn = componentEmitter.prototype.emit; | ||
this.connected = true; | ||
this.disconnected = false; | ||
} | ||
/** | ||
* Emit an event to the server client | ||
* @param {string} eventKey -- The event key that needs to be attached | ||
* @param {object} payload -- The payload that needs to be attached to the emit | ||
* @param {function} in_callback | ||
*/ | ||
emit (eventKey, payload, cb) { | ||
if (typeof payload === 'function') { | ||
payload = null; | ||
cb = payload; | ||
} | ||
const callback = cb || function () {}; | ||
callback(this._socketMock.emitEvent(eventKey, payload)); | ||
} | ||
/** | ||
* Fire an event to the server | ||
* @param {string} eventKey -- The event key that needs to be attached | ||
* @param {object} payload -- The payload that needs to be attached to the emit | ||
* @param {Function} callback | ||
*/ | ||
fireEvent (eventKey, payload) { | ||
this._emitFn(eventKey, payload); | ||
} | ||
/** | ||
* Close the socket | ||
*/ | ||
close () { | ||
this.disconnected = true; | ||
this.connected = false; | ||
this.emit('disconnect', 'io client disconnect'); | ||
return this | ||
} | ||
/** | ||
* Disconnet the socket alias for close | ||
*/ | ||
disconnect () { | ||
return this.close() | ||
} | ||
} | ||
const createPayload = function (object) { | ||
return object ? JSON.parse(JSON.stringify(object)) : undefined | ||
}; | ||
/** | ||
* A mocking class for the Socket IO Server side | ||
*/ | ||
class SocketMock extends componentEmitter { | ||
/** | ||
* Creates a new SocketMock instance | ||
**/ | ||
constructor () { | ||
super(); | ||
this.joinedRooms = this.rooms = []; | ||
this.socketClient = new SocketClient(this); | ||
this._emitFn = componentEmitter.prototype.emit; | ||
this.generalCallbacks = {}; | ||
this.broadcast = { | ||
/** | ||
* Broadcast to room | ||
* @param {string} roomKey the roomkey which need to be attached to | ||
* @return {object} | ||
**/ | ||
to: roomKey => { | ||
return { | ||
/** | ||
* Emitting | ||
* @param {string} eventKey | ||
* @param {object} payload | ||
**/ | ||
emit: (eventKey, payload) => { | ||
if (this.generalCallbacks[eventKey]) { | ||
this.generalCallbacks[eventKey](createPayload(payload), roomKey); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
/** | ||
* Emit an event to the server (used by client) | ||
* @param {string} eventKey -- The event key | ||
* @param {object} payload -- Additional payload | ||
**/ | ||
emitEvent (eventKey, payload) { | ||
this._emitFn(eventKey, createPayload(payload)); | ||
} | ||
/** | ||
* Register on every event that the server sends | ||
* @param {string} eventKey | ||
* @param {Function} callback | ||
**/ | ||
onEmit (eventKey, callback) { | ||
this.generalCallbacks[eventKey] = callback; | ||
} | ||
/** | ||
* Emit an event to the client | ||
* @param {string} eventKey -- The event key | ||
* @param {object} payload -- Additional payload | ||
**/ | ||
emit (eventKey, payload) { | ||
this.socketClient.fireEvent(eventKey, payload); | ||
} | ||
/** | ||
* Joining a room | ||
* @param {string} roomKey The room we want to join | ||
**/ | ||
join (roomKey) { | ||
this.joinedRooms.push(roomKey); | ||
} | ||
/** | ||
* Leaving a room | ||
* @param {string} roomKey The room you want to leave | ||
**/ | ||
leave (roomKey) { | ||
const index = this.joinedRooms.indexOf(roomKey); | ||
this.joinedRooms.splice(index, 1); | ||
} | ||
/** | ||
* Monitor logging feature | ||
* @param {string} value The value you want to monitor | ||
**/ | ||
monitor (value) { | ||
return value | ||
} | ||
/** | ||
* Closing the socket server | ||
* @param {Function} cb | ||
*/ | ||
disconnect () { | ||
this.emit('disconnecting', 'io server disconnect'); | ||
this.emit('disconnect', 'io server disconnect'); | ||
return this | ||
} | ||
} | ||
module.exports = SocketMock; |
{ | ||
"name": "socket.io-mock", | ||
"version": "1.2.4", | ||
"version": "1.3.0", | ||
"description": "a mocked version of a socket.io socket for testing", | ||
@@ -45,8 +45,6 @@ "main": "dist/index.js", | ||
"rollup-plugin-bundle-size": "^1.0.3", | ||
"rollup-plugin-progress": "^1.1.1", | ||
"rollup-plugin-terser": "^5.2.0", | ||
"rollup-watch": "^4.3.1", | ||
"standard": "^14.3.1", | ||
"uglify-js": "^3.8.0", | ||
"wrtc": "0.4.4" | ||
"uglify-js": "^3.8.0" | ||
}, | ||
@@ -53,0 +51,0 @@ "standard": { |
@@ -17,3 +17,3 @@ # socket.io-mock | ||
> Don't forget to enable [`pkg.module`](https://github.com/rollup/rollup/wiki/pkg.module) in your rollup config. | ||
> NEW! Added support for disconnect() and close() | ||
@@ -20,0 +20,0 @@ # Installation |
import resolve from '@rollup/plugin-node-resolve' | ||
import commonjs from '@rollup/plugin-commonjs' | ||
import bundleSize from 'rollup-plugin-bundle-size' | ||
import progress from 'rollup-plugin-progress' | ||
import { terser } from 'rollup-plugin-terser' | ||
@@ -20,3 +19,2 @@ | ||
}), | ||
progress(), | ||
bundleSize() | ||
@@ -23,0 +21,0 @@ ] |
@@ -95,2 +95,12 @@ import Emitter from 'component-emitter' | ||
} | ||
/** | ||
* Closing the socket server | ||
* @param {Function} cb | ||
*/ | ||
disconnect () { | ||
this.emit('disconnecting', 'io server disconnect') | ||
this.emit('disconnect', 'io server disconnect') | ||
return this | ||
} | ||
} |
@@ -12,2 +12,4 @@ import Emitter from 'component-emitter' | ||
this._emitFn = Emitter.prototype.emit | ||
this.connected = true | ||
this.disconnected = false | ||
} | ||
@@ -39,2 +41,19 @@ | ||
} | ||
/** | ||
* Close the socket | ||
*/ | ||
close () { | ||
this.disconnected = true | ||
this.connected = false | ||
this.emit('disconnect', 'io client disconnect') | ||
return this | ||
} | ||
/** | ||
* Disconnet the socket alias for close | ||
*/ | ||
disconnect () { | ||
return this.close() | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
29557
12
17
882
0