test-hierarchical-addressing.js
6.99 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
'use strict';
var PubSub = global.PubSub || require('../src/pubsub'),
TestHelper = global.TestHelper || require('../test/helper'),
assert = require('referee').assert,
sinon = require('sinon');
describe( 'Hierarchical addressing', function () {
beforeEach(function(){
this.clock = sinon.useFakeTimers();
});
afterEach(function(){
this.clock.restore();
});
it('publish method should not call any children in a namespace', function( done ) {
var messages = ['library', 'library.music'],
spy = sinon.spy(),
data = TestHelper.getUniqueString();
PubSub.subscribe( messages[0], spy ); //This should be called
PubSub.subscribe( messages[1], spy );
PubSub.publish( messages[0], data );
assert.equals( spy.callCount, 0 );
this.clock.tick(1);
assert.equals( spy.callCount, 1 );
done();
});
it('publish method should call a parent namespace', function( done ) {
// Publishing library.music should trigger parent library
var messages = ['library', 'library.music'],
spy = sinon.spy(),
data = TestHelper.getUniqueString();
PubSub.subscribe( messages[0], spy ); //This should be called
PubSub.subscribe( messages[1], spy ); //This should be called
PubSub.publish( messages[1], data );
assert.equals( spy.callCount, 0 );
this.clock.tick(1);
assert.equals( spy.callCount, 2 );
done();
});
it('publish method should call only a parent namespace', function( done ) {
//Publishing library.music should only trigger parents descendants
//Even if it has a child
var messages = ['library', 'library.music', 'library.music.jazz'],
spy = sinon.spy(),
data = TestHelper.getUniqueString();
PubSub.subscribe( messages[0], spy ); //This should be called
PubSub.subscribe( messages[1], spy ); //This should be called
PubSub.subscribe( messages[2], spy );
PubSub.publish( messages[1], data );
assert.equals( spy.callCount, 0 );
this.clock.tick(1);
assert.equals( spy.callCount, 2 );
done();
});
it('publish method should call all parent namespaces', function( done ) {
//Publishing library.music.jazz should trigger all parents
var messages = ['library', 'library.music', 'library.music.jazz'],
spy = sinon.spy(),
data = TestHelper.getUniqueString();
PubSub.subscribe( messages[0], spy ); //This should be called
PubSub.subscribe( messages[1], spy ); //This should be called
PubSub.subscribe( messages[2], spy ); //This should be called
PubSub.publish( messages[2], data );
assert.equals( spy.callCount, 0 );
this.clock.tick(1);
assert.equals( spy.callCount, 3 );
done();
});
it('publish method should call only parent descendants', function( done ) {
//Publishing library.music.jazz should trigger only all parents descendants
//Skipping library.playlist and library.playlist.*
var messages = [
'library',
'library.music',
'library.music.jazz',
'library.playlist',
'library.playlist.mine'
],
spy = sinon.spy(),
data = TestHelper.getUniqueString();
PubSub.subscribe( messages[0], spy ); //This should be called
PubSub.subscribe( messages[1], spy ); //This should be called
PubSub.subscribe( messages[2], spy ); //This should be called
PubSub.subscribe( messages[3], spy );
PubSub.subscribe( messages[4], spy );
PubSub.publish( messages[2], data );
assert.equals( spy.callCount, 0 );
this.clock.tick(1);
assert.equals( spy.callCount, 3 );
done();
});
it('publish method should call all parent descendants deeply', function( done ) {
//Publishing library.music.jazz.soft.swing should trigger all but
//library.music.playlist.jazz
var messages = [
'library',
'library.music',
'library.music.jazz',
'library.music.jazz.soft',
'library.music.jazz.soft.swing',
'library.music.playlist.jazz'
],
spy = sinon.spy(),
data = TestHelper.getUniqueString();
PubSub.subscribe( messages[0], spy ); //This should be called
PubSub.subscribe( messages[1], spy ); //This should be called
PubSub.subscribe( messages[2], spy ); //This should be called
PubSub.subscribe( messages[3], spy ); //This should be called
PubSub.subscribe( messages[4], spy ); //This should be called
PubSub.subscribe( messages[5], spy ); //This should be called
PubSub.subscribe( messages[6], spy );
PubSub.publish( messages[4], data );
assert.equals( spy.callCount, 0 );
this.clock.tick(1);
assert.equals( spy.callCount, 5 );
done();
});
it('publish method should still call all parents, even when middle child is unsubscribed', function( done ) {
var messages = ['library', 'library.music', 'library.music.jazz'],
spy = sinon.spy(),
data = TestHelper.getUniqueString(),
token;
PubSub.subscribe( messages[0], spy ); //This should be called
PubSub.subscribe( messages[2], spy ); //This should be called
token = PubSub.subscribe( messages[1], spy );
PubSub.unsubscribe( token ); //Take out middle child
PubSub.publish( messages[2], data );
assert.equals( spy.callCount, 0 );
this.clock.tick(1);
assert.equals( spy.callCount, 2 );
done();
});
it('unsubscribe method should return tokens when succesfully removing namespaced message', function(){
var func = function(){ return undefined; },
messages = ['playlist.music', 'playlist.music.jazz'],
token1 = PubSub.subscribe( messages[0], func),
token2 = PubSub.subscribe( messages[1], func ),
result1 = PubSub.unsubscribe( token1 ),
result2 = PubSub.unsubscribe( token2 );
assert.equals( result1, token1 );
assert.equals( result2, token2 );
});
it('unsubscribe method should unsubscribe parent without affecting orphans', function( done ){
var data = TestHelper.getUniqueString(),
spy = sinon.spy(),
messages = ['playlist', 'playlist.music', 'playlist.music.jazz'],
token;
token = PubSub.subscribe( messages[0], spy ); //Gets unsubscribed
PubSub.subscribe( messages[1], spy ); //This should be called
PubSub.subscribe( messages[2], spy ); //This should be called
PubSub.unsubscribe( token );
PubSub.publish( messages[2], data );
assert.equals( spy.callCount, 0 );
this.clock.tick(1);
assert.equals( spy.callCount, 2 );
done();
});
});