test-publish.js
6.31 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
'use strict';
var PubSub = require('../src/pubsub'),
TestHelper = require('../test/helper'),
assert = require('referee').assert,
refute = require('referee').refute,
sinon = require('sinon');
describe( 'publish method', function () {
it('should return false if there are no subscribers', function(){
var message = TestHelper.getUniqueString();
assert.equals( PubSub.publish( message ), false );
});
it('should return true if there are subscribers to a message', function(){
var message = TestHelper.getUniqueString(),
func = function(){ return undefined; };
PubSub.subscribe( message, func );
assert( PubSub.publish( message ) );
});
it('should return false, when there are no longer any subscribers to a message', function(){
var message = TestHelper.getUniqueString(),
func = function(){ return undefined; },
token = PubSub.subscribe(message, func);
PubSub.unsubscribe(token);
assert.equals( PubSub.publish(message), false );
});
it('should call all subscribers for a message exactly once', function(){
var message = TestHelper.getUniqueString(),
spy1 = sinon.spy(),
spy2 = sinon.spy();
PubSub.subscribe( message, spy1 );
PubSub.subscribe( message, spy2 );
PubSub.publishSync( message, 'my payload' ); // force sync here, easier to test
assert( spy1.calledOnce );
assert( spy2.calledOnce );
});
it('should call all ONLY subscribers of the published message', function(){
var message1 = TestHelper.getUniqueString(),
message2 = TestHelper.getUniqueString(),
spy1 = sinon.spy(),
spy2 = sinon.spy();
PubSub.subscribe( message1, spy1 );
PubSub.subscribe( message2, spy2 );
PubSub.publishSync( message1, 'some payload' );
// ensure the first subscriber IS called
assert( spy1.called );
// ensure the second subscriber IS NOT called
assert.equals( spy2.callCount, 0 );
});
it('should call subscribers with message as first argument', function(){
var message = TestHelper.getUniqueString(),
spy = sinon.spy();
PubSub.subscribe( message, spy );
PubSub.publishSync( message, 'some payload' );
assert( spy.calledWith( message ) );
});
it('should call subscribers with data as second argument', function(){
var message = TestHelper.getUniqueString(),
spy = sinon.spy(),
data = TestHelper.getUniqueString();
PubSub.subscribe( message, spy );
PubSub.publishSync( message, data );
assert( spy.calledWith( message, data ) );
});
it('should publish method asyncronously', function( done ){
var message = TestHelper.getUniqueString(),
spy = sinon.spy(),
data = TestHelper.getUniqueString(),
clock = sinon.useFakeTimers();
PubSub.subscribe( message, spy );
PubSub.publish( message, data );
assert.equals( spy.callCount, 0 );
clock.tick(1);
assert.equals( spy.callCount, 1 );
done();
clock.restore();
});
it('publishSync method should allow syncronous publication', function(){
var message = TestHelper.getUniqueString(),
spy = sinon.spy(),
data = TestHelper.getUniqueString();
PubSub.subscribe( message, spy );
PubSub.publishSync( message, data );
assert.equals( spy.callCount, 1 );
});
it('should call all subscribers, even if there are exceptions', function( done ){
var message = TestHelper.getUniqueString(),
func1 = function(){
throw('some error');
},
spy1 = sinon.spy(),
spy2 = sinon.spy(),
clock = sinon.useFakeTimers();
PubSub.subscribe( message, func1 );
PubSub.subscribe( message, spy1 );
PubSub.subscribe( message, spy2 );
assert.exception( function(){
PubSub.publishSync( message, 'some data' );
clock.tick(1);
});
assert( spy1.called );
assert( spy2.called );
done();
clock.restore();
});
it('should fail immediately on exceptions when immediateExceptions is true', function(){
var message = TestHelper.getUniqueString(),
func1 = function(){
throw('some error');
},
spy1 = sinon.spy(),
spy2 = sinon.spy();
PubSub.subscribe( message, func1 );
PubSub.subscribe( message, spy1 );
PubSub.immediateExceptions = true;
assert.exception( function(){
PubSub.publishSync( message, 'some data' );
});
refute( spy1.called );
refute( spy2.called );
// make sure we restore PubSub to it's original state
delete PubSub.immediateExceptions;
});
it('should fail immediately on exceptions in namespaces when immediateExceptions is true', function(){
var func1 = function(){
throw('some error');
},
spy1 = sinon.spy();
PubSub.subscribe( 'buy', func1 );
PubSub.subscribe( 'buy', spy1 );
PubSub.immediateExceptions = true;
assert.exception( function(){
PubSub.publishSync( 'buy.tomatoes', 'some data' );
});
refute( spy1.called );
// make sure we restore PubSub to it's original state
delete PubSub.immediateExceptions;
});
it('should call all subscribers, even when there are unsubscriptions within', function(done){
var topic = TestHelper.getUniqueString(),
spy1 = sinon.spy(),
func1 = function func1(){
PubSub.unsubscribe(func1);
spy1();
},
spy2 = sinon.spy(),
func2 = function func2(){
PubSub.unsubscribe(func2);
spy2();
},
clock = sinon.useFakeTimers();
PubSub.subscribe(topic, func1);
PubSub.subscribe(topic, func2);
PubSub.publish(topic, 'some data');
clock.tick(1);
assert(spy1.called, 'expected spy1 to be called');
assert(spy2.called, 'expected spy2 to be called');
clock.restore();
done();
});
});