pusher-js-mock
Advanced tools
Comparing version 0.2.1 to 0.3.0
import PusherChannelMock from "./pusher-channel-mock"; | ||
import PusherFactoryMock from "./pusher-factory-mock"; | ||
import PusherMock from "./pusher-js-mock"; | ||
export { PusherMock, PusherFactoryMock, PusherChannelMock }; | ||
import PusherPresenceChannelMock from "./pusher-presence-channel-mock"; | ||
export { PusherMock, PusherFactoryMock, PusherChannelMock, PusherPresenceChannelMock }; |
@@ -9,1 +9,3 @@ "use strict"; | ||
exports.PusherMock = pusher_js_mock_1.default; | ||
var pusher_presence_channel_mock_1 = require("./pusher-presence-channel-mock"); | ||
exports.PusherPresenceChannelMock = pusher_presence_channel_mock_1.default; |
@@ -7,5 +7,7 @@ /** Interface for all the callbacks each Pusher event could potentially have */ | ||
declare class PusherChannelMock { | ||
name: string; | ||
callbacks: ICallbacks; | ||
subscribed: boolean; | ||
/** Initialize PusherChannelMock with callbacks object. */ | ||
constructor(); | ||
constructor(name?: string); | ||
/** | ||
@@ -12,0 +14,0 @@ * Bind callback to an event name. |
@@ -6,3 +6,6 @@ "use strict"; | ||
/** Initialize PusherChannelMock with callbacks object. */ | ||
function PusherChannelMock() { | ||
function PusherChannelMock(name) { | ||
if (name === void 0) { name = "public-channel"; } | ||
this.subscribed = true; | ||
this.name = name; | ||
this.callbacks = {}; | ||
@@ -9,0 +12,0 @@ } |
@@ -27,3 +27,3 @@ import PusherMock from "./pusher-js-mock"; | ||
*/ | ||
constructor(pusherKey: string); | ||
constructor(pusherKey: string, ...args: any[]); | ||
/** | ||
@@ -30,0 +30,0 @@ * Getter for pusherClientInstance |
"use strict"; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -28,4 +35,8 @@ var pusher_js_mock_1 = require("./pusher-js-mock"); | ||
function PusherFactoryMock(pusherKey) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
this.pusherKey = pusherKey; | ||
this.pusherClientInstance = new pusher_js_mock_1.default(); | ||
this.pusherClientInstance = new (pusher_js_mock_1.default.bind.apply(pusher_js_mock_1.default, __spreadArrays([void 0], args)))(); | ||
} | ||
@@ -32,0 +43,0 @@ /** |
@@ -1,17 +0,14 @@ | ||
/** Interface for storing channels */ | ||
interface IChannels { | ||
[name: string]: any; | ||
} | ||
/** Class representing fake Pusher. */ | ||
import { Config } from "pusher-js"; | ||
/** Class representing fake Pusher Client. */ | ||
declare class PusherMock { | ||
channels: IChannels; | ||
/** Initialize PusherMock with empty channels object. */ | ||
constructor(); | ||
id: string | undefined; | ||
info: Record<string, any> | undefined; | ||
clientKey: string | undefined; | ||
config: Config | undefined; | ||
channels: import("./pusher-js-mock-instance").IChannels; | ||
channel: (name: string, client?: PusherMock) => any; | ||
/** Initialize PusherMock */ | ||
constructor(clientKey?: string, config?: Config); | ||
setAuthInfo(errored: boolean, auth: any): void; | ||
/** | ||
* Get channel by its name. | ||
* @param {String} name - name of the channel. | ||
* @returns {PusherChannelMock} PusherChannelMock object that represents channel | ||
*/ | ||
channel(name: string): any; | ||
/** | ||
* Mock subscribing to a channel. | ||
@@ -18,0 +15,0 @@ * @param {String} name - name of the channel. |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var pusher_channel_mock_1 = require("./pusher-channel-mock"); | ||
/** Class representing fake Pusher. */ | ||
var pusher_js_mock_instance_1 = require("./pusher-js-mock-instance"); | ||
/** Class representing fake Pusher Client. */ | ||
var PusherMock = /** @class */ (function () { | ||
/** Initialize PusherMock with empty channels object. */ | ||
function PusherMock() { | ||
this.channels = {}; | ||
/** Initialize PusherMock */ | ||
function PusherMock(clientKey, config) { | ||
this.id = undefined; | ||
this.info = undefined; | ||
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); | ||
} | ||
/** | ||
* Get channel by its name. | ||
* @param {String} name - name of the channel. | ||
* @returns {PusherChannelMock} PusherChannelMock object that represents channel | ||
*/ | ||
PusherMock.prototype.channel = function (name) { | ||
if (!this.channels[name]) { | ||
this.channels[name] = new pusher_channel_mock_1.default(); | ||
PusherMock.prototype.setAuthInfo = function (errored, auth) { | ||
if (!errored) { | ||
this.id = auth.id; | ||
this.info = auth.info; | ||
} | ||
return this.channels[name]; | ||
}; | ||
@@ -27,3 +30,15 @@ /** | ||
PusherMock.prototype.subscribe = function (name) { | ||
return this.channel(name); | ||
var _a; | ||
if (name.includes("presence-")) { | ||
((_a = this.config) === null || _a === void 0 ? void 0 : _a.authorizer) ? this.config | ||
.authorizer({}, {}) | ||
.authorize({ name: name }, this.setAuthInfo) | ||
: this.setAuthInfo(false, { | ||
id: Math.random() | ||
.toString(36) | ||
.substr(2, 9), | ||
info: {} | ||
}); | ||
} | ||
return pusher_js_mock_instance_1.default.channel(name, this); | ||
}; | ||
@@ -35,5 +50,5 @@ /** | ||
PusherMock.prototype.unsubscribe = function (name) { | ||
if (name in this.channels) { | ||
this.channels[name].callbacks = {}; | ||
delete this.channels[name]; | ||
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]; | ||
} | ||
@@ -40,0 +55,0 @@ }; |
{ | ||
"name": "pusher-js-mock", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Mock Pusher.js in your JavaScript tests", | ||
@@ -40,2 +40,3 @@ "main": "lib/index.js", | ||
"@types/jest": "^24.0.24", | ||
"@types/pusher-js": "^4.2.2", | ||
"babel-jest": "^24.9.0", | ||
@@ -48,3 +49,3 @@ "husky": "^0.14.3", | ||
"pusher-js": "^4.2.2", | ||
"pusher-js-mock": "^0.2.0", | ||
"pusher-js-mock": "^0.2.1", | ||
"rimraf": "^2.6.2", | ||
@@ -51,0 +52,0 @@ "ts-jest": "^24.2.0", |
@@ -16,2 +16,3 @@ [![npm version](https://badge.fury.io/js/pusher-js-mock.svg)](https://badge.fury.io/js/pusher-js-mock) | ||
Using yarn: | ||
``` | ||
@@ -22,2 +23,3 @@ yarn add --dev pusher-js-mock | ||
Or using npm: | ||
``` | ||
@@ -32,2 +34,3 @@ npm install -D pusher-js-mock | ||
- [Stubbing Pusher when used as a global variable](#stubbing-pusher-when-used-as-a-global-variable) | ||
- [Mocking presence channels](#using-presence-channels) | ||
@@ -49,9 +52,9 @@ For more detailed examples, check out [`examples` directory](https://github.com/nikolalsvk/pusher-js-mock/tree/master/examples) | ||
// initializing PusherMock | ||
const pusher = new PusherMock() | ||
const pusher = new PusherMock(); | ||
// subscribing to a Pusher channel | ||
const channel = pusher.subscribe("my-channel") | ||
const channel = pusher.subscribe("my-channel"); | ||
// emitting an event | ||
channel.emit("event-name") | ||
channel.emit("event-name"); | ||
``` | ||
@@ -64,3 +67,3 @@ | ||
```javascript | ||
import Pusher from 'pusher-js'; | ||
import Pusher from "pusher-js"; | ||
``` | ||
@@ -74,7 +77,6 @@ | ||
```javascript | ||
jest.mock('pusher-js', () => { | ||
const Pusher = require('pusher-js-mock').PusherMock | ||
return Pusher | ||
}) | ||
jest.mock("pusher-js", () => { | ||
const Pusher = require("pusher-js-mock").PusherMock; | ||
return Pusher; | ||
}); | ||
``` | ||
@@ -95,3 +97,3 @@ | ||
return new Pusher(pusherKey); | ||
} | ||
}, | ||
}; | ||
@@ -111,3 +113,3 @@ ``` | ||
// get the Pusher client reference | ||
pusher = pusherFactoryMock.pusherClient() | ||
pusher = pusherFactoryMock.pusherClient(); | ||
``` | ||
@@ -117,2 +119,57 @@ | ||
#### Using presence channels | ||
This package also supports using presence channels for multiple clients. The mock | ||
```js | ||
// create-client.js | ||
import Pusher from "pusher-js"; | ||
import { getAuthSomehow } from "./getAuthSomehow"; | ||
export const createClient = ({ id, info }) => | ||
new Pusher("APP_KEY", { | ||
cluster: "APP_CLUSTER", | ||
// see https://github.com/pusher/pusher-js#authorizer-function | ||
authorizer: ({ name }) => ({ | ||
authorize: (socketId, callback) => { | ||
const auth = getAuthSomehow(id, info); | ||
callback(false, auth); | ||
}, | ||
}), | ||
}); | ||
export default createClient; | ||
``` | ||
```js | ||
// create-client.spec.js | ||
import createClient from "../create-client"; | ||
// mock the authorize function and pusher | ||
jest.mock("pusher-js", () => require("pusher-js-mock")); | ||
jest.mock("../getAuthSomehow", () => ({ | ||
getAuthSomehow: (id, info) => ({ id, info }), | ||
})); | ||
it("should create a presence channel", async () => { | ||
// arrange: create pusher client | ||
const pusher = createClient({ id: "my-id", info: { role: "moderator" } }); | ||
// act: required to ensure pusher events are called, i.e. pusher:member_added | ||
const presenceChannel = await pusher.subscribe("presence-channel"); | ||
// assert: presenceChannel has the properties we expect it to. | ||
expect(presenceChannel.members.myID).toBe("my-id"); | ||
expect(presenceChannel.members.me).toEqual({ | ||
id: "my-id", | ||
info: { role: "moderator" }, | ||
}); | ||
expect(presenceChannel.members.members).toEqual({ | ||
"my-id": { role: "moderator" }, | ||
}); | ||
}); | ||
``` | ||
[Check out a code example of using presence channels](https://github.com/nikolalsvk/pusher-js-mock/tree/master/examples/presence-channels) | ||
### [Code of Conduct](CODE_OF_CODUCT.md) | ||
@@ -119,0 +176,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
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
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
33118
19
639
173
18