Skip to content

Releases: nikolalsvk/pusher-js-mock

Return mock channel instance from a few methods

06 Nov 17:28
Compare
Choose a tag to compare

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 #57

Add allChannels method

27 Oct 13:24
Compare
Choose a tag to compare

You can now get all channels by doing pusherMock.allChannels(). Thanks, @ruipserra for the idea and this PR #55

Fix a bug with trigger function being echoed to the originator

06 Aug 11:22
Compare
Choose a tag to compare

▶️ Add support for channel.trigger

25 Apr 16:10
92fbc5c
Compare
Choose a tag to compare

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.

Add a method to unbind all callbacks

22 Feb 12:19
Compare
Choose a tag to compare

You can now unbind all callbacks form a channel mock like so:

channelMock.unbind_all();

Thanks to @kubk, here is his PR #35

Add mock for pusher.connection

19 Jul 14:47
Compare
Choose a tag to compare

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! 🙇

Support for pusher.bind and less dependencies

27 Jun 16:41
Compare
Choose a tag to compare

The new version of pusher-js-mock brings a couple of changes

pusher.bind() 🔗

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 #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);
  });
});

Less dependencies 📦

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 #25.

Add event when member is removed from presence channels

12 Apr 08:45
Compare
Choose a tag to compare

This version brings:

  • added pusher:member:removed event
  • updated unsubscribe to only remove own callbacks, so if other clients are 'connected' theirs stay intact

To 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);
});

Support for mocking presence channels

03 Mar 17:17
Compare
Choose a tag to compare

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" },
  });
});

Convert to Typescript

22 Dec 10:55
fb9c8a9
Compare
Choose a tag to compare

This package is now 100% Typescript 🎉