UNPKG

pusher-js-mock

Version:

Mock Pusher.js in your JavaScript tests with ease

61 lines (60 loc) 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** Class representing a fake Pusher channel. */ var PusherChannelMock = /** @class */ (function () { /** Initialize PusherChannelMock with callbacks object. */ function PusherChannelMock(name) { if (name === void 0) { name = "public-channel"; } this.subscribed = true; this.name = name; this.callbacks = {}; } /** * Bind callback to an event name. * @param {String} name - name of the event. * @param {Function} callback - callback to be called on event. */ PusherChannelMock.prototype.bind = function (name, callback) { this.callbacks[name] = this.callbacks[name] || []; this.callbacks[name].push(callback); return this; }; /** * Unbind callback from an event name. * @param {String} name - name of the event. * @param {Function} callback - callback to be called on event. */ PusherChannelMock.prototype.unbind = function (name, callback) { this.callbacks[name] = (this.callbacks[name] || []).filter(function (cb) { return cb !== callback; }); return this; }; /** * Unbind callbacks from all the events. */ PusherChannelMock.prototype.unbind_all = function () { this.callbacks = {}; return this; }; /** * Emit event with data. * @param {String} name - name of the event. * @param {*} data - data you want to pass in to callback function that gets called. */ PusherChannelMock.prototype.emit = function (name, data) { var callbacks = this.callbacks[name]; if (callbacks) { callbacks.forEach(function (cb) { return cb(data); }); } return this; }; /** * Trigger event with data. * @param {String} name - name of the event. * @param {*} data - data you want to pass in to callback function that gets called. */ PusherChannelMock.prototype.trigger = function (name, data) { this.emit(name, data); }; return PusherChannelMock; }()); exports.default = PusherChannelMock;