test-unsubscribe.js
5.08 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
'use strict';
var PubSub = require('../src/pubsub'),
TestHelper = require('../test/helper'),
assert = require('referee').assert,
refute = require('referee').refute,
sinon = require('sinon');
describe( 'unsubscribe method', function() {
it('should return token when succesful', function(){
var func = function(){ return undefined; },
message = TestHelper.getUniqueString(),
token = PubSub.subscribe( message, func),
result = PubSub.unsubscribe( token );
assert.equals( result, token );
});
it('should return false when unsuccesful', function(){
var unknownToken = 'my unknown token',
result = PubSub.unsubscribe( unknownToken ),
func = function(){ return undefined; },
message = TestHelper.getUniqueString(),
token = PubSub.subscribe( message, func );
// first, let's try a completely unknown token
assert.equals( result, false );
// now let's try unsubscribing the same method twice
PubSub.unsubscribe( token );
assert.equals( PubSub.unsubscribe( token ), false );
});
it('with function argument should return true when succesful', function(){
var func = function(){ return undefined; },
message = TestHelper.getUniqueString(),
result;
PubSub.subscribe( message, func);
result = PubSub.unsubscribe( func );
assert.equals( result, true );
});
it('with function argument should return false when unsuccesful', function(){
var func = function(){ return undefined; },
message = TestHelper.getUniqueString(),
unknownToken = 'my unknown token',
result = PubSub.unsubscribe( unknownToken );
// first, let's try a completely unknown token
assert.equals( result, false );
// now let's try unsubscribing the same method twice
PubSub.subscribe( message, func );
PubSub.subscribe( message, func );
PubSub.subscribe( message, func );
// unsubscribe once, this should remove all subscriptions for message
PubSub.unsubscribe( func );
// unsubscribe again
assert.equals( PubSub.unsubscribe( func ), false );
});
it('with topic argument, must clear all exactly matched subscriptions', function(){
var topic = TestHelper.getUniqueString(),
spy1 = sinon.spy(),
spy2 = sinon.spy();
PubSub.subscribe(topic, spy1);
PubSub.subscribe(topic, spy2);
PubSub.unsubscribe(topic);
PubSub.publishSync(topic, TestHelper.getUniqueString());
refute(spy1.called);
refute(spy2.called);
});
it('with topic argument, must only clear matched subscriptions', function(){
var topic1 = TestHelper.getUniqueString(),
topic2 = TestHelper.getUniqueString(),
spy1 = sinon.spy(),
spy2 = sinon.spy();
PubSub.subscribe(topic1, spy1);
PubSub.subscribe(topic2, spy2);
PubSub.unsubscribe(topic1);
PubSub.publishSync(topic1, TestHelper.getUniqueString());
PubSub.publishSync(topic2, TestHelper.getUniqueString());
refute(spy1.called);
assert(spy2.called);
});
it('with topic argument, must clear all matched hierarchical subscriptions', function(){
var topic = TestHelper.getUniqueString(),
topicA = topic + '.a',
topicB = topic + '.a.b',
topicC = topic + '.a.b.c',
spyA = sinon.spy(),
spyB = sinon.spy(),
spyC = sinon.spy();
PubSub.subscribe(topicA, spyA);
PubSub.subscribe(topicB, spyB);
PubSub.subscribe(topicC, spyC);
PubSub.unsubscribe(topicB);
PubSub.publishSync(topicC, TestHelper.getUniqueString());
assert(spyA.called);
refute(spyB.called);
refute(spyC.called);
});
it('with parent topic argument, must clear all child subscriptions', function() {
var topic = TestHelper.getUniqueString(),
topicA = topic + '.a',
topicB = topic + '.a.b',
topicC = topic + '.a.b.c',
spyB = sinon.spy(),
spyC = sinon.spy();
// subscribe only to children:
PubSub.subscribe(topicB, spyB);
PubSub.subscribe(topicC, spyC);
// but unsubscribe from a parent:
PubSub.unsubscribe(topicA);
PubSub.publishSync(topicB, TestHelper.getUniqueString());
PubSub.publishSync(topicC, TestHelper.getUniqueString());
refute(spyB.called);
refute(spyC.called);
});
it('must not throw exception when unsubscribing as part of publishing', function(){
refute.exception(function(){
var topic = TestHelper.getUniqueString(),
sub1 = function(){
PubSub.unsubscribe(sub1);
},
sub2 = function(){ return undefined; };
PubSub.subscribe( topic, sub1 );
PubSub.subscribe( topic, sub2 );
PubSub.publishSync( topic, 'hello world!' );
});
});
});