-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathChannelSpec.js
More file actions
330 lines (255 loc) · 9.38 KB
/
ChannelSpec.js
File metadata and controls
330 lines (255 loc) · 9.38 KB
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
var Mediator = require("../index").Mediator,
sinon = require('sinon'),
chai = require('chai'),
expect = require('chai').expect,
sinonChai = require("sinon-chai");
chai.use(sinonChai);
require('sinon-mocha').enhance(sinon);
describe("Channel", function() {
var mediator, channel;
beforeEach(function() {
channel = new Mediator.Channel();
});
describe("Initialization", function(){
it("should set its namespace property", function(){
expect(channel.namespace).to.equal("");
});
it("should set its namespace property to a given namespace", function(){
var namespacedChannel = new Mediator.Channel("test:coffee");
expect(namespacedChannel.namespace).to.equal("test:coffee");
});
it("should act like a constructor when called like a function", function(){
var fnChannel = Mediator.Channel("name");
expect(fnChannel).not.to.be.undefined;
});
});
describe("addSubscriber", function(){
it("should add a subscriber to the collection", function(){
var spy = sinon.spy();
channel.addSubscriber(spy);
expect(channel._subscribers.length).to.equal(1);
});
it("should give subscribers an id", function(){
var spy = sinon.spy();
channel.addSubscriber(spy);
expect(channel._subscribers[0].id).to.not.be.undefined;
expect(channel._subscribers[0].id).to.not.equal('');
});
it("should add a subscriber to the collection with context", function(){
var spy = sinon.spy(),
contextObj = { derp: "herp" };
channel.addSubscriber(spy, {}, contextObj);
expect(channel._subscribers[0].context).to.equal(contextObj);
});
it("should add a subscriber to the collection with options", function(){
var spy = sinon.spy(),
contextObj = {},
optionsObj = { derp: "herp" };
channel.addSubscriber(spy, optionsObj, contextObj);
expect(channel._subscribers[0].options).to.equal(optionsObj);
});
it("should be able to set top priority", function(){
var spy = sinon.spy(),
spy2 = sinon.spy(),
spy3 = sinon.spy();
channel.addSubscriber(spy);
channel.addSubscriber(spy2);
channel.addSubscriber(spy3, { priority: 1 });
expect(channel._subscribers[0].fn).to.equal(spy);
expect(channel._subscribers[1].fn).to.equal(spy3);
expect(channel._subscribers[2].fn).to.equal(spy2);
});
it("should be able to set arbitrary priority", function(){
var spy = sinon.spy(),
spy2 = sinon.spy(),
spy3 = sinon.spy();
channel.addSubscriber(spy);
channel.addSubscriber(spy2);
channel.addSubscriber(spy3, { priority: 1 });
expect(channel._subscribers[0].fn).to.equal(spy);
expect(channel._subscribers[1].fn).to.equal(spy3);
expect(channel._subscribers[2].fn).to.equal(spy2);
});
it("should be able to change priority after adding it", function(){
var spy = sinon.spy(),
spy2 = sinon.spy(),
spy3 = sinon.spy();
var sub = channel.addSubscriber(spy, { num: 1 });
channel.addSubscriber(spy2, { num: 2 });
channel.addSubscriber(spy3, { num: 3 });
channel.setPriority(sub.id, 2);
expect(channel._subscribers[0].fn).to.equal(spy2);
expect(channel._subscribers[1].fn).to.equal(spy3);
expect(channel._subscribers[2].fn).to.equal(spy);
});
});
describe("GetSubscriber", function(){
it("should get a subscriber by its id", function(){
var spy = sinon.spy();
channel.addSubscriber(spy);
expect(channel.getSubscriber(channel._subscribers[0].id)).to.not.be.undefined;
});
});
describe("addChannel", function(){
it("should add a channel to the collection", function(){
var channelName = "test";
channel.addChannel(channelName);
expect(channel._channels[channelName]).to.not.be.undefined;
});
});
describe("hasChannel", function(){
it("should return true if the channel exists", function(){
var channelName = "test";
channel.addChannel(channelName);
expect(channel.hasChannel(channelName)).to.equal(true);
});
it("should return true if the channel does not exist", function(){
var channelName = "test",
badChannelName = "herp";
channel.addChannel(channelName);
expect(channel.hasChannel(badChannelName)).to.equal(false);
});
});
describe("ReturnChannel", function(){
it("should return a reference to a channel by name", function(){
var channelName = "test";
channel.addChannel(channelName);
expect(channel.returnChannel(channelName)).to.equal(channel._channels[channelName]);
});
});
describe("removeSubscriber", function(){
it("should remove subscribers if no fn is given", function(){
var spy = sinon.spy();
channel.addSubscriber(spy);
expect(channel._subscribers.length).to.equal(1);
channel.removeSubscriber();
expect(channel._subscribers.length).to.equal(0);
});
it("should remove matching subscribers a valid fn is given", function(){
var spy = sinon.spy(),
spy2 = sinon.spy();
channel.addSubscriber(spy);
channel.addSubscriber(spy2);
expect(channel._subscribers.length).to.equal(2);
channel.removeSubscriber(spy);
expect(channel._subscribers.length).to.equal(1);
expect(channel._subscribers[0].fn).to.equal(spy2);
});
it("should remove matching subscribers a valid id is given", function(){
var spy = sinon.spy(),
spy2 = sinon.spy(),
spy3 = sinon.spy();
channel.addSubscriber(spy);
var sub2 = channel.addSubscriber(spy2);
expect(channel._subscribers.length).to.equal(2);
channel.addSubscriber(spy3);
expect(channel._subscribers.length).to.equal(3);
channel.removeSubscriber(sub2.id);
expect(channel._subscribers.length).to.equal(2);
expect(channel._subscribers[0].fn).to.equal(spy);
expect(channel._subscribers[1].fn).to.equal(spy3);
});
it("should do nothing if an invalid fn is given", function(){
var spy = sinon.spy(),
invalidFn = "derp";
channel.addSubscriber(spy);
channel.addSubscriber(function() {});
expect(channel._subscribers.length).to.equal(2);
channel.removeSubscriber(invalidFn);
expect(channel._subscribers.length).to.equal(2);
});
it("should do nothing if a non-matching fn is given", function(){
var spy = sinon.spy(),
spy2 = sinon.spy();
channel.addSubscriber(spy);
expect(channel._subscribers.length).to.equal(1);
channel.removeSubscriber(spy2);
expect(channel._subscribers.length).to.equal(1);
});
});
describe("publish", function(){
it("should call all matching subscribers", function(){
var spy = sinon.spy(),
data = ["data"];
channel.addSubscriber(spy);
channel.publish(data);
expect(spy).calledWith(data[0]);
});
it("should call all matching subscribers with predicates", function(){
var spy = sinon.spy(),
data = ["data"];
channel.addSubscriber(spy, {}, { predicate: function(data){ return data.length == 4 } });
channel.publish(data);
expect(spy).calledWith(data[0]);
});
it("should call all matching subscribers with context", function(){
var spy = sinon.spy(),
data = ["data"];
channel.addSubscriber(function() { this(); }, {}, spy );
channel.publish(data);
expect(spy).called;
});
it("should call all matching for parent channels", function(){
var channelName = "test",
spy = sinon.spy(),
spy2 = sinon.spy(),
data = ["data"];
channel.addSubscriber(spy);
channel.addChannel(channelName);
channel._channels[channelName].addSubscriber(spy2);
channel._channels[channelName].publish(data);
expect(spy).calledWith(data[0]);
expect(spy2).calledWith(data[0]);
});
it("should call all matching subscribers with context", function(){
var spy = sinon.spy(),
data = ["data"];
channel.addSubscriber(function() { this(); }, {}, spy );
channel.publish(data);
expect(spy).called;
});
it("should call subscribers in predefined priority", function(){
var sub1 = function(){
this.a += "1";
},
sub2 = function(){
this.a += "2";
},
sub3 = function(){
this.a += "3";
},
data = ["data"];
this.a = "0";
channel.addSubscriber(sub3, {}, this);
channel.addSubscriber(sub1, { priority: 2 }, this);
channel.addSubscriber(sub2, { priority: 1 }, this);
channel.publish(data);
expect(this.a).to.equal("0123");
});
it("should call subscribers and continue running, if one of subscribers throws an exception", function(){
var sub1 = function(){
this.a += "1";
},
sub2 = function(){
this.a += "2";
throw new Error('error');
this.a += "444";
},
sub3 = function(){
this.a += "3";
},
data = ["data"];
this.a = "0";
channel.addSubscriber(sub1, {}, this);
channel.addSubscriber(sub2, {}, this);
channel.addSubscriber(sub3, {}, this);
try {
channel.publish(data);
this.a += "4";
} catch(ex) {
this.a += "555";
}
expect(this.a).to.equal("01234");
});
});
});