From 2bbbbf9656537328ec87c282ab5070953daa06ed Mon Sep 17 00:00:00 2001 From: kubk Date: Tue, 15 Dec 2020 12:18:26 +0300 Subject: [PATCH 1/3] Add unbind_all() method This method is used to unsubscribe from all the events. Original PusherChannel extends from `EventDispatcher`: https://github.com/pusher/pusher-js/blob/a55efb22433c3c303e348cdda4d763dc55cc2771/src/core/channels/channel.ts#L20 Method `unbind_all` is located in the `EventDispatcher`: https://github.com/pusher/pusher-js/blob/a55efb22433c3c303e348cdda4d763dc55cc2771/src/core/events/dispatcher.ts#L50 --- src/pusher-channel-mock.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/pusher-channel-mock.ts b/src/pusher-channel-mock.ts index 92bfa4a..8a405db 100644 --- a/src/pusher-channel-mock.ts +++ b/src/pusher-channel-mock.ts @@ -35,6 +35,13 @@ class PusherChannelMock { cb => cb !== callback ); } + + /** + * Unbind callbacks from all the events. + */ + public unbind_all() { + this.callbacks = {}; + } /** * Emit event with data. From a0bcdf822757a6e8113caaf3a5b3d32b1024ee0a Mon Sep 17 00:00:00 2001 From: Egor Gorbachev <7gorbachevm@gmail.com> Date: Thu, 17 Dec 2020 21:17:42 +0300 Subject: [PATCH 2/3] Add tests for unbind_all() --- src/__tests__/pusher-channel-mock.spec.ts | 17 +++++++++++++++++ src/pusher-channel-mock.ts | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/__tests__/pusher-channel-mock.spec.ts b/src/__tests__/pusher-channel-mock.spec.ts index 5b356f4..60eee29 100644 --- a/src/__tests__/pusher-channel-mock.spec.ts +++ b/src/__tests__/pusher-channel-mock.spec.ts @@ -21,6 +21,23 @@ describe("PusherChannelMock", () => { }); }); + describe("#unbind_all", () => { + it("clears callbacks from all the events", () => { + const firstCallback = jest.fn(); + const secondCallback = jest.fn(); + channelMock.bind("first-channel", firstCallback); + channelMock.bind("second-channel", secondCallback); + + channelMock.unbind_all(); + channelMock.emit("first-channel"); + channelMock.emit("second-channel"); + + expect(firstCallback).not.toHaveBeenCalled(); + expect(secondCallback).not.toHaveBeenCalled(); + expect(channelMock.callbacks).toEqual({}); + }); + }); + describe("#unbind", () => { describe("with callbacks defined for the event", () => { it("removes name: callback from callbacks object", () => { diff --git a/src/pusher-channel-mock.ts b/src/pusher-channel-mock.ts index 8a405db..5cdd434 100644 --- a/src/pusher-channel-mock.ts +++ b/src/pusher-channel-mock.ts @@ -35,7 +35,7 @@ class PusherChannelMock { cb => cb !== callback ); } - + /** * Unbind callbacks from all the events. */ From 88ac3e7517de69cad0e3fffbd8e2adff45b85099 Mon Sep 17 00:00:00 2001 From: Egor Gorbachev <7gorbachevm@gmail.com> Date: Thu, 17 Dec 2020 21:19:24 +0300 Subject: [PATCH 3/3] Fix typo --- src/__tests__/pusher-channel-mock.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__tests__/pusher-channel-mock.spec.ts b/src/__tests__/pusher-channel-mock.spec.ts index 60eee29..b02980f 100644 --- a/src/__tests__/pusher-channel-mock.spec.ts +++ b/src/__tests__/pusher-channel-mock.spec.ts @@ -22,7 +22,7 @@ describe("PusherChannelMock", () => { }); describe("#unbind_all", () => { - it("clears callbacks from all the events", () => { + it("clears events from all the callbacks", () => { const firstCallback = jest.fn(); const secondCallback = jest.fn(); channelMock.bind("first-channel", firstCallback);