-
Notifications
You must be signed in to change notification settings - Fork 26
/
ddp.js
346 lines (285 loc) · 10.3 KB
/
ddp.js
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import chai, {expect} from "chai";
import sinon from "sinon";
import sinonChai from "sinon-chai";
chai.use(sinonChai);
import DDP from "../../src/ddp";
import Socket from "../../src/socket";
class SocketConstructorMock {
send () {}
close () {}
}
const options = {
SocketConstructor: SocketConstructorMock
};
describe("`DDP` class", () => {
describe("`constructor` method", () => {
beforeEach(() => {
sinon.stub(Socket.prototype, "on");
sinon.stub(Socket.prototype, "open");
});
afterEach(() => {
Socket.prototype.on.restore();
Socket.prototype.open.restore();
});
it("instantiates a `Socket`", () => {
const ddp = new DDP(options);
expect(ddp.socket).to.be.an.instanceOf(Socket);
});
it("registers handlers for `socket` events", () => {
const ddp = new DDP(options);
expect(ddp.socket.on).to.have.always.been.calledWithMatch(
sinon.match.string,
sinon.match.func
);
});
it("opens a connection to the server (by calling `socket.open`) unless `options.autoConnect === false`", () => {
const ddp = new DDP(options);
expect(ddp.socket.open).to.have.callCount(1);
});
it("does not open a connection when `options.autoConnect === false`", () => {
const ddp = new DDP({
...options,
autoConnect: false
});
expect(ddp.socket.open).to.have.callCount(0);
});
it("sets the instance `reconnectInterval` to `options.reconnectInterval` if specified", () => {
const ddp = new DDP({
...options,
reconnectInterval: 1,
autoConnect: false
});
expect(ddp.reconnectInterval).to.equal(1);
});
it("sets the instance `reconnectInterval` to a default value if `options.reconnectInterval` is not specified", () => {
const ddp = new DDP({
...options,
autoConnect: false
});
expect(ddp.reconnectInterval).to.equal(10000);
});
});
describe("`method` method", () => {
it("sends a DDP `method` message", () => {
const ddp = new DDP(options);
ddp.messageQueue.push = sinon.spy();
const id = ddp.method("name", ["param"]);
expect(ddp.messageQueue.push).to.have.been.calledWith({
msg: "method",
id: id,
method: "name",
params: ["param"]
});
});
it("returns the method's `id`", () => {
const ddp = new DDP(options);
ddp.messageQueue.push = sinon.spy();
const id = ddp.method("name", ["param"]);
expect(id).to.be.a("string");
});
});
describe("`sub` method", () => {
it("sends a DDP `sub` message", () => {
const ddp = new DDP(options);
ddp.messageQueue.push = sinon.spy();
const id = ddp.sub("name", ["param"]);
expect(ddp.messageQueue.push).to.have.been.calledWith({
msg: "sub",
id: id,
name: "name",
params: ["param"]
});
});
it("returns the sub's `id`", () => {
const ddp = new DDP(options);
ddp.messageQueue.push = sinon.spy();
const id = ddp.sub("name", ["param"]);
expect(id).to.be.a("string");
});
it("generates unique id when not specified", () => {
const ddp = new DDP(options);
var ids = [];
ids.push(ddp.sub("echo", [ 0 ]));
ids.push(ddp.sub("echo", [ 0 ]));
expect(ids[0]).to.be.a("string");
expect(ids[1]).to.be.a("string");
expect(ids[0]).not.to.equal(ids[1]);
});
it("allows manually specifying sub's id", () => {
const ddp = new DDP(options);
const subId = ddp.sub("echo", [ 0 ], "12345");
expect(subId).to.equal("12345");
});
});
describe("`unsub` method", () => {
it("sends a DDP `unsub` message", () => {
const ddp = new DDP(options);
ddp.messageQueue.push = sinon.spy();
const id = ddp.unsub("id");
expect(ddp.messageQueue.push).to.have.been.calledWith({
msg: "unsub",
id: id
});
});
it("returns the sub's `id`", () => {
const ddp = new DDP(options);
ddp.messageQueue.push = sinon.spy();
const id = ddp.unsub("id");
expect(id).to.be.a("string");
expect(id).to.equal("id");
});
});
describe("`connect` method", () => {
beforeEach(() => {
sinon.stub(Socket.prototype, "open");
});
afterEach(() => {
Socket.prototype.open.restore();
});
it("opens the WebSocket connection", () => {
const ddp = new DDP(options);
Socket.prototype.open.reset();
ddp.connect();
expect(ddp.socket.open).to.have.callCount(1);
});
});
describe("`disconnect` method", () => {
beforeEach(() => {
sinon.stub(Socket.prototype, "close");
});
afterEach(() => {
Socket.prototype.close.restore();
});
it("closes the WebSocket connection", () => {
const ddp = new DDP(options);
ddp.disconnect();
expect(ddp.socket.close).to.have.callCount(1);
});
it("sets the `autoReconnect` flag to false", () => {
const ddp = new DDP(options);
ddp.disconnect();
expect(ddp.autoReconnect).to.equal(false);
});
});
describe("`socket` `open` handler", () => {
beforeEach(() => {
sinon.stub(global, "setTimeout").callsFake(fn => fn());
});
afterEach(() => {
global.setTimeout.restore();
});
it("sends the `connect` DDP message", () => {
const ddp = new DDP(options);
ddp.socket.send = sinon.spy();
ddp.socket.emit("open");
expect(ddp.socket.send).to.have.been.calledWith({
msg: "connect",
version: "1",
support: ["1"]
});
});
});
describe("`socket` `close` handler", () => {
beforeEach(() => {
sinon.stub(global, "setTimeout").callsFake(fn => fn());
});
afterEach(() => {
global.setTimeout.restore();
});
it("emits the `disconnected` event", () => {
const ddp = new DDP(options);
ddp.emit = sinon.spy();
ddp.socket.emit("close");
expect(ddp.emit).to.have.been.calledWith("disconnected");
});
it("sets the status to `disconnected`", () => {
const ddp = new DDP(options);
ddp.status = "connected";
ddp.emit = sinon.spy();
ddp.socket.emit("close");
expect(ddp.status).to.equal("disconnected");
});
it("schedules a reconnection unless `options.autoReconnect === false`", () => {
const ddp = new DDP(options);
ddp.socket.open = sinon.spy();
ddp.socket.emit("close");
expect(ddp.socket.open).to.have.callCount(1);
});
it("doesn't schedule a reconnection when `options.autoReconnect === false`", () => {
const ddp = new DDP({
...options,
autoReconnect: false
});
ddp.socket.open = sinon.spy();
ddp.socket.emit("close");
expect(ddp.socket.open).to.have.callCount(0);
});
});
describe("`socket` `message:in` handler", () => {
beforeEach(() => {
sinon.stub(global, "setTimeout").callsFake(fn => fn());
});
afterEach(() => {
global.setTimeout.restore();
});
it("responds to `ping` DDP messages", () => {
const ddp = new DDP(options);
ddp.socket.send = sinon.spy();
ddp.socket.emit("message:in", {
id: "id",
msg: "ping"
});
expect(ddp.socket.send).to.have.been.calledWith({
id: "id",
msg: "pong"
});
});
it("triggers `messageQueue` processing upon connection", () => {
const ddp = new DDP(options);
ddp.emit = sinon.spy();
ddp.messageQueue.process = sinon.spy();
ddp.socket.emit("message:in", {msg: "connected"});
expect(ddp.messageQueue.process).to.have.callCount(1);
});
it("sets the status to `connected` upon connection", () => {
const ddp = new DDP(options);
ddp.emit = sinon.spy();
ddp.socket.emit("message:in", {msg: "connected"});
expect(ddp.status).to.equal("connected");
});
it("emits public DDP messages as events", () => {
const ddp = new DDP(options);
ddp.emit = sinon.spy();
const message = {
id: "id",
msg: "result"
};
ddp.socket.emit("message:in", message);
expect(ddp.emit).to.have.been.calledWith("result", message);
});
it("ignores unknown (or non public) DDP messages", () => {
const ddp = new DDP(options);
ddp.emit = sinon.spy();
const message = {
id: "id",
msg: "not-a-ddp-message"
};
ddp.socket.emit("message:in", message);
expect(ddp.emit).to.have.callCount(0);
});
});
describe("`messageQueue` consumer", () => {
it("acks if `status` is `connected`", () => {
const ddp = new DDP(options);
ddp.status = "connected";
const ack = ddp.messageQueue.consumer({});
expect(ack).to.equal(true);
});
it("doesn't ack if `status` is `disconnected`", () => {
const ddp = new DDP(options);
ddp.status = "disconnected";
const ack = ddp.messageQueue.consumer({});
expect(ack).to.equal(false);
});
});
});