forked from nikolalsvk/pusher-js-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpusher-js-mock.spec.ts
199 lines (162 loc) · 6.11 KB
/
pusher-js-mock.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { PusherMock } from "../index";
import PusherChannelMock from "../pusher-channel-mock";
import PusherPresenceChannelMock from "../pusher-presence-channel-mock";
import { createClient } from "./pusher-presence-channel-mock.spec";
import PusherMockInstance from "../pusher-js-mock-instance";
describe("PusherMock", () => {
let pusherMock: PusherMock;
beforeEach(() => {
PusherMockInstance.reset();
pusherMock = new PusherMock();
});
it("initializes channels object", () => {
expect(pusherMock.channels).toEqual({});
});
describe("#channel", () => {
it("returns instance of PusherChannelMock", () => {
expect(pusherMock.channel("my-channel")).toBeDefined();
});
it("adds new channel to channels object", () => {
pusherMock.channel("my-channel");
expect(pusherMock.channels).toMatchObject({ "my-channel": {} });
});
it("returns the correct type of channel based on channel name", () => {
expect(pusherMock.channel("public-channel")).toBeInstanceOf(
PusherChannelMock
);
expect(pusherMock.channel("presence-channel")).toBeInstanceOf(
PusherPresenceChannelMock
);
pusherMock.unsubscribe("public-channel");
pusherMock.unsubscribe("presence-channel");
});
describe("channel is already added to channels object", () => {
beforeEach(() => {
pusherMock.channel("my-channel");
});
it("returns instance of PusherChannelMock", () => {
expect(pusherMock.channel("my-channel")).toBeDefined();
});
it("adds new channel to channels object", () => {
expect(pusherMock.channels).toMatchObject({ "my-channel": {} });
});
});
});
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);
});
});
describe("#unbind", () => {
it("removes name: callback from callbacks object", () => {
const callback = () => {};
const myChannel = pusherMock.channel("my-channel");
const myOtherChannel = pusherMock.channel("my-other-channel");
const myThirdChannel = pusherMock.channel("my-third-channel");
pusherMock.bind("test-event", callback);
expect(myChannel.callbacks).toEqual({
"test-event": [callback]
});
expect(myOtherChannel.callbacks).toEqual({
"test-event": [callback]
});
expect(myThirdChannel.callbacks).toEqual({
"test-event": [callback]
});
pusherMock.unbind("test-event", callback);
expect(myChannel.callbacks).toEqual({
"test-event": []
});
expect(myOtherChannel.callbacks).toEqual({
"test-event": []
});
expect(myThirdChannel.callbacks).toEqual({
"test-event": []
});
});
});
describe("#subscribe", () => {
it("returns instance of PusherChannelMock", () => {
expect(pusherMock.channel("my-channel")).toBeDefined();
});
it("adds new channel to channels object", () => {
pusherMock.subscribe("my-channel");
expect(pusherMock.channels).toMatchObject({ "my-channel": {} });
});
it("subscribes to a presence channel", () => {
pusherMock.subscribe("presence-channel");
expect(pusherMock.channels).toMatchObject({ "presence-channel": {} });
});
});
describe("#unsubscribe", () => {
describe("channel name is inside channels object", () => {
it("removes channel from channels object", () => {
pusherMock.subscribe("my-channel");
pusherMock.unsubscribe("my-channel");
expect(pusherMock.channels).toEqual({});
});
});
describe("channel name is not inside channels object", () => {
it("removes channel from channels object", () => {
pusherMock.unsubscribe("my-channel");
expect(pusherMock.channels).toEqual({});
});
});
describe("presence channels", () => {
it("removes only own callbacks from the callbacks object", async () => {
// arrange
const client = new PusherMock("my-id");
const otherClient = createClient("your-id", {});
const channel = client.subscribe("presence-channel");
const otherChannel = otherClient.subscribe("presence-channel");
const listener = jest.fn();
const otherListener = jest.fn();
// act
channel.bind("some-event", listener);
otherChannel.bind("some-event", otherListener);
otherClient.unsubscribe("presence-channel");
PusherMockInstance.channels["presence-channel"].emit("some-event", {
data: "testing"
});
// assert
expect(listener).toHaveBeenCalledWith({ data: "testing" });
expect(otherListener).not.toHaveBeenCalled();
expect(
PusherMockInstance.channels["presence-channel"].callbacks
).toEqual({ "some-event": [listener] });
// expect channel removed
client.unsubscribe("presence-channel");
expect(PusherMockInstance.channels).toEqual({});
});
});
});
describe("authorizer", () => {
it("should set a default ID and info if presence channel used and no auth config passed", () => {
pusherMock.subscribe("presence-channel");
expect(pusherMock.id).toBeDefined();
expect(pusherMock.info).toBeDefined();
});
it("should leave id and info undefined if auth errored", () => {
pusherMock.config = {
authorizer: () => ({
authorize: (socketId, callback) => {
callback(true, undefined as any);
}
})
};
pusherMock.subscribe("presence-channel");
expect(pusherMock.id).toBeUndefined();
expect(pusherMock.info).toBeUndefined();
});
});
});