pusher-js-mock
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -7,8 +7,7 @@ import { PusherMock, PusherPresenceChannelMock } from "."; | ||
/** | ||
* Proxies the instance of channel returned so we can still reference the | ||
* shared members object whilst passing our own ID & me properties | ||
* | ||
* @param {PusherPresenceChannelMock} channel The channel we're mocking | ||
* @param {PusherMock} client the client we want to use to proxy the channel | ||
* Create the proxied channel | ||
* @param {PusherPresenceChannelMock} channel the channel to be proxied | ||
* @param {PusherMock} client the client we'll use to proxy the channel | ||
* @returns {Proxy<PusherPresenceChannelMock>} the proxied channel | ||
*/ | ||
export declare const proxyPresenceChannel: (channel: PusherPresenceChannelMock, client: PusherMock) => PusherPresenceChannelMock; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Proxies the instance of channel returned so we can still reference the | ||
* shared members object whilst passing our own ID & me properties | ||
* | ||
* @param {PusherPresenceChannelMock} channel The channel we're mocking | ||
* @param {PusherMock} client the client we want to use to proxy the channel | ||
*/ | ||
exports.proxyPresenceChannel = function (channel, client) { | ||
var proxiedChannel = proxyChannel(channel, client); | ||
emitConnectionEvents(proxiedChannel, client); | ||
return proxiedChannel; | ||
}; | ||
/** | ||
* Proxy custom members info to | ||
@@ -108,3 +60,3 @@ * | ||
*/ | ||
var proxyChannel = function (channel, client) { | ||
exports.proxyPresenceChannel = function (channel, client) { | ||
var handler = { | ||
@@ -138,31 +90,1 @@ /** | ||
}; | ||
/** | ||
* Emit connection events triggered by pusher | ||
* @param {PusherPresenceChannelMock} channel the channel we want to trigger this on | ||
* @param client the client we're using to emit the connection events | ||
* @returns void | ||
*/ | ||
var emitConnectionEvents = function (channel, client) { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
/** setTimeout simulates the async nature of adding members */ | ||
return [4 /*yield*/, Promise.resolve()]; | ||
case 1: | ||
/** setTimeout simulates the async nature of adding members */ | ||
_a.sent(); | ||
channel.members.addMember({ | ||
user_id: client.id, | ||
user_info: client.info | ||
}); | ||
/** Add the member to the members object when proxied. */ | ||
channel.emit("pusher:member_added", { | ||
id: client.id, | ||
info: client.info | ||
}); | ||
/** Emit internal event */ | ||
channel.emit("pusher:subscription_succeeded", channel.members); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; |
@@ -15,4 +15,8 @@ import PusherMock from "./pusher-js-mock"; | ||
channel(name: string, client?: PusherMock): any; | ||
/** | ||
* Resets the instance to a fresh state, i.e. no channels. | ||
*/ | ||
reset(): void; | ||
} | ||
declare const _default: PusherMockInstance; | ||
export default _default; |
@@ -29,4 +29,10 @@ "use strict"; | ||
}; | ||
/** | ||
* Resets the instance to a fresh state, i.e. no channels. | ||
*/ | ||
PusherMockInstance.prototype.reset = function () { | ||
this.channels = {}; | ||
}; | ||
return PusherMockInstance; | ||
}()); | ||
exports.default = new PusherMockInstance(); |
"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"); | ||
@@ -31,6 +32,7 @@ /** Class representing fake Pusher Client. */ | ||
var _a; | ||
var channel = pusher_js_mock_instance_1.default.channel(name, this); | ||
if (name.includes("presence-")) { | ||
((_a = this.config) === null || _a === void 0 ? void 0 : _a.authorizer) ? this.config | ||
.authorizer({}, {}) | ||
.authorize({ name: name }, this.setAuthInfo) | ||
.authorize(channel, this.setAuthInfo) | ||
: this.setAuthInfo(false, { | ||
@@ -42,4 +44,5 @@ id: Math.random() | ||
}); | ||
pusher_events_1.emitConnectionEvents(channel, this); | ||
} | ||
return pusher_js_mock_instance_1.default.channel(name, this); | ||
return channel; | ||
}; | ||
@@ -51,5 +54,26 @@ /** | ||
PusherMock.prototype.unsubscribe = function (name) { | ||
var _this = this; | ||
if (name in pusher_js_mock_instance_1.default.channels) { | ||
pusher_js_mock_instance_1.default.channels[name].callbacks = {}; | ||
delete pusher_js_mock_instance_1.default.channels[name]; | ||
if (name.includes("presence-")) { | ||
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]; | ||
} | ||
} | ||
else { | ||
// public channel | ||
pusher_js_mock_instance_1.default.channels[name].callbacks = {}; | ||
delete pusher_js_mock_instance_1.default.channels[name]; | ||
} | ||
} | ||
@@ -56,0 +80,0 @@ }; |
{ | ||
"name": "pusher-js-mock", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Mock Pusher.js in your JavaScript tests", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -91,3 +91,3 @@ [![npm version](https://badge.fury.io/js/pusher-js-mock.svg)](https://badge.fury.io/js/pusher-js-mock) | ||
return new Pusher(pusherKey); | ||
}, | ||
} | ||
}; | ||
@@ -114,3 +114,3 @@ ``` | ||
This package also supports using presence channels for multiple clients. The mock | ||
This package also supports using presence channels for multiple clients. The mock will automatically detect when `presence-` is in the channel name and return a presence channel with `channel.members` filled out as expected. You can pass in IDs and info via a custom authorizer, just as you would with the real package. | ||
@@ -130,4 +130,4 @@ ```js | ||
callback(false, auth); | ||
}, | ||
}), | ||
} | ||
}) | ||
}); | ||
@@ -145,3 +145,3 @@ | ||
jest.mock("../getAuthSomehow", () => ({ | ||
getAuthSomehow: (id, info) => ({ id, info }), | ||
getAuthSomehow: (id, info) => ({ id, info }) | ||
})); | ||
@@ -160,6 +160,6 @@ | ||
id: "my-id", | ||
info: { role: "moderator" }, | ||
info: { role: "moderator" } | ||
}); | ||
expect(presenceChannel.members.members).toEqual({ | ||
"my-id": { role: "moderator" }, | ||
"my-id": { role: "moderator" } | ||
}); | ||
@@ -171,2 +171,41 @@ }); | ||
#### Pusher events emitted by presence channels | ||
The mocked Pusher instance will also emit pusher internal events `pusher:subscription_succeeded`, `pusher:member_added` and `pusher:member_removed` to the relevant clients: | ||
```js | ||
it("should emit presence-channel events", async () => { | ||
const client = createClient({ id: "my-id" }); | ||
const channel = client.subscribe("presence-channel"); | ||
const listener = jest.fn(); | ||
/** | ||
* On bind, pusher:subscription_succeded will trigger | ||
* for the client subscribing. Other clients will be | ||
* notified via pusher:member_added as below. | ||
*/ | ||
await channel.bind("pusher:subscription_succeeded", listener); | ||
expect(listener).toHaveBeenCalledTimes(1); | ||
/** | ||
* Create and subscribe a new client that will trigger the | ||
* pusher:member_added event. This only gets triggered for | ||
* clients are not the client subscribing | ||
*/ | ||
channel.bind("pusher:member_added", listener); | ||
const otherClient = createClient({ id: "your-id" }); | ||
await otherClient.subscribe("presence-channel"); | ||
expect(listener).toHaveBeenCalledTimes(2); | ||
/** | ||
* Unsubscribe the otherClient to trigger pusher:member_removed. | ||
* This only gets triggered for clients that are not the client | ||
* unsubscribing. | ||
*/ | ||
channel.bind("pusher:member_removed", listener); | ||
await otherClient.unsubscribe("presence-channel"); | ||
expect(listener).toHaveBeenCalledTimes(3); | ||
}); | ||
``` | ||
### [Code of Conduct](CODE_OF_CODUCT.md) | ||
@@ -173,0 +212,0 @@ |
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
37233
21
696
212
0