UNPKG

pusher-js-mock

Version:

Mock Pusher.js in your JavaScript tests with ease

119 lines (118 loc) 4.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var pusher_events_1 = require("./pusher-events"); var pusher_js_mock_instance_1 = require("./pusher-js-mock-instance"); var pusher_presence_channel_mock_1 = require("./pusher-presence-channel-mock"); /** Class representing fake Pusher Client. */ var PusherMock = /** @class */ (function () { /** Initialize PusherMock */ function PusherMock(clientKey, config) { this.id = undefined; this.info = undefined; this.connection = pusher_js_mock_instance_1.default.connection; this.channels = pusher_js_mock_instance_1.default.channels; this.channel = pusher_js_mock_instance_1.default.channel; this.clientKey = clientKey; this.config = config; this.setAuthInfo = this.setAuthInfo.bind(this); this.subscribe = this.subscribe.bind(this); this.unsubscribe = this.unsubscribe.bind(this); } PusherMock.prototype.setAuthInfo = function (errored, auth) { if (!errored) { this.id = auth.id; this.info = auth.info; } }; /** * Mock subscribing to a channel. * @param {String} name - name of the channel. * @returns {PusherChannelMock} PusherChannelMock object that represents channel */ PusherMock.prototype.subscribe = function (name) { var _a; var channel = pusher_js_mock_instance_1.default.channel(name, this); if (pusher_presence_channel_mock_1.isPresenceChannel(channel)) { ((_a = this.config) === null || _a === void 0 ? void 0 : _a.authorizer) ? this.config .authorizer({}) .authorize(channel, this.setAuthInfo) : this.setAuthInfo(false, { id: Math.random() .toString(36) .substr(2, 9), info: {} }); pusher_events_1.emitConnectionEvents(channel, this); } return channel; }; /** * Unsubscribe from a mocked channel. * @param {String} name - name of the channel. */ PusherMock.prototype.unsubscribe = function (name) { if (name in pusher_js_mock_instance_1.default.channels) { var channel = pusher_js_mock_instance_1.default.channel(name, this); if (pusher_presence_channel_mock_1.isPresenceChannel(channel)) { this.unsubscribePresence(name); } else { // public channel pusher_js_mock_instance_1.default.channels[name].callbacks = {}; delete pusher_js_mock_instance_1.default.channels[name]; } } }; /** * Bind a callback to an event on all channels * @param {String} name - name of the event to bind to. * @param {Function} callback - callback to be called on event. */ PusherMock.prototype.bind = function (name, callback) { var _this = this; Object.keys(this.channels).forEach(function (channelName) { _this.channel(channelName).bind(name, callback); }); }; /** * Unbind a callback from an event on all channels * @param {String} name - name of the event to unbind from. * @param {Function} callback - callback to be called on event. */ PusherMock.prototype.unbind = function (name, callback) { var _this = this; Object.keys(this.channels).forEach(function (channelName) { _this.channel(channelName).unbind(name, callback); }); }; /** * Returns a list of all channels */ PusherMock.prototype.allChannels = function () { return Object.values(this.channels); }; /** * Unsubscribe from a mocked presence channel. * @param {String} name - name of the channel. */ PusherMock.prototype.unsubscribePresence = function (name) { var _this = this; var channel = pusher_js_mock_instance_1.default.channels[name]; pusher_events_1.emitDisconnectionEvents(channel, this); for (var _i = 0, _a = Object.keys(channel.callbacks); _i < _a.length; _i++) { var key = _a[_i]; // filter out any callbacks that are our own channel.callbacks[key] = channel.callbacks[key].filter(function (cb) { return cb.owner !== _this.id; }); // delete the callback list if there are no callbacks left if (channel.callbacks[key].length === 0) { delete channel.callbacks[key]; } } // if there are no callback events left, delete the channel if (Object.keys(Object.assign({}, channel.callbacks)).length === 0) { delete pusher_js_mock_instance_1.default.channels[name]; } }; return PusherMock; }()); exports.default = PusherMock;