Mock Pusher.js in your JavaScript tests
MIT License
Bot releases are visible (Hide)
Published by nikolalsvk almost 3 years ago
Make bind
, unbind
, unbind_all
, and emit
return the channel mock instance so that the above is supported by this library as well.
Thanks to @ruipserra and the PR https://github.com/nikolalsvk/pusher-js-mock/pull/57
Published by nikolalsvk almost 3 years ago
You can now get all channels by doing pusherMock.allChannels()
. Thanks, @ruipserra for the idea and this PR https://github.com/nikolalsvk/pusher-js-mock/pull/55
Published by nikolalsvk about 3 years ago
@erickponce fixed https://github.com/nikolalsvk/pusher-js-mock/issues/47, big thanks to him
Published by nikolalsvk over 3 years ago
You can now do channel.trigger('event-name')
.
NOTE:
We are going to deprecate channel.emit
in the next version since it is not used anymore.
Published by nikolalsvk over 3 years ago
You can now unbind all callbacks form a channel mock like so:
channelMock.unbind_all();
Thanks to @kubk, here is his PR https://github.com/nikolalsvk/pusher-js-mock/pull/35
Published by nikolalsvk over 4 years ago
In the 0.3.3 version you can now emit an even from a connection:
import { PusherMock } from "pusher-js-mock";
// initializing PusherMock
const pusher = new PusherMock();
// emitting connection event
pusher.connection.emit("event-name");
And, you can listen for it
import { PusherMock } from "pusher-js-mock";
descibe("listening for an event", () => {
// initializing PusherMock
const pusher = new PusherMock();
// define and attach a listener
const listener = jest.fn();
pusher.connection.bind("event-name", listener);
// emitting an event
pusher.connection.emit("event-name");
// Expect listener to have been called
expect(listener).toHaveBeenCalled();
});
Thanks to @DeadEnglish for pushing this through! 🙇
Published by nikolalsvk over 4 years ago
The new version of pusher-js-mock brings a couple of changes
You can now test pusher.bind()
that will bind a callback to all your channels.
This is inspired by the issue raised by @snaptopixel in https://github.com/nikolalsvk/pusher-js-mock/issues/23 and by the Pusher JS docs describing how to use this.
Here's an example on how to use it:
describe("#bind", () => {
it("binds event to all channels", () => {
const listener = jest.fn();
const myChannel = pusherMock.channel("my-channel");
const myOtherChannel = pusherMock.channel("my-other-channel");
const myThirdChannel = pusherMock.channel("my-third-channel");
pusherMock.bind("test-event", listener);
myChannel.emit("test-event");
expect(listener).toHaveBeenCalledTimes(1);
myOtherChannel.emit("test-event");
expect(listener).toHaveBeenCalledTimes(2);
myThirdChannel.emit("test-event");
expect(listener).toHaveBeenCalledTimes(3);
});
});
Yep, we removed pusher-js
and pusher-js-mock
from development dependencies of the gem.
It should now be lightweight and it won't interfere with any of the versions of pusher-js
you have.
This was largely inspired by https://github.com/nikolalsvk/pusher-js-mock/issues/25.
Published by nikolalsvk over 4 years ago
This version brings:
pusher:member:removed
eventTo listen for this event (and others for members), you can do something like this:
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);
});
Published by nikolalsvk over 4 years ago
You can now mock a presence channel like this. Let's say you have a create-client file similar to this one:
// 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;
You would test it like this:
// 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" },
});
});
Published by nikolalsvk almost 5 years ago
This package is now 100% Typescript 🎉
Published by nikolalsvk almost 6 years ago
Now you can unsubscribe from a channel after you've subscribed.
You can also unbind callbacks for a specific event from your channel!
Published by nikolalsvk about 6 years ago
You can now actually import modules from the package!