test-subscribe.js
3.04 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
'use strict';
var PubSub = require('../src/pubsub'),
TestHelper = require('../test/helper'),
assert = require('referee').assert,
refute = require('referee').refute;
describe( 'subscribe method', function() {
it('should return token as String', function(){
var func = function(){ return undefined; },
message = TestHelper.getUniqueString(),
token = PubSub.subscribe( message , func );
assert.isString( token );
});
it('should return new token for several subscriptions with same function', function(){
var func = function(){ return undefined; },
tokens = [],
iterations = 10,
message = TestHelper.getUniqueString(),
i;
// build an array of tokens
for ( i = 0; i < iterations; i++ ){
tokens.push( PubSub.subscribe( message, func ) );
}
// make sure all tokens are different
TestHelper.assertAllTokensDifferent( tokens );
});
it('should return unique tokens for each namespaced subscription', function(){
var func = function(){ return undefined; },
tokens = [],
messages = ['library', 'library.music', 'library.music.jazz'],
i;
// build an array of tokens
for ( i = 0; i < messages.length; i++ ){
tokens.push( PubSub.subscribe( messages[i], func ) );
}
// make sure all tokens are different
TestHelper.assertAllTokensDifferent( tokens );
});
it('should return unique token for unique functions', function(){
var tokens = [],
iterations = 10,
message = TestHelper.getUniqueString(),
i;
function bakeFunc( value ){
return function(){
return value;
};
}
// build an array of tokens, passing in a different function for each subscription
for ( i = 0; i < iterations; i++ ){
tokens.push( PubSub.subscribe( message, bakeFunc( i ) ) );
}
// make sure all tokens are different
TestHelper.assertAllTokensDifferent( tokens );
});
it('should return false when subscriber argument is not a function', function(){
var invalidSubscribers = [undefined, null, 'a string', 123, [], {}, new Date()],
topic = TestHelper.getUniqueString(),
i;
for ( i = 0; i < invalidSubscribers.length; i++ ){
assert.equals(PubSub.subscribe(topic, invalidSubscribers[i]), false);
}
assert.equals(i, invalidSubscribers.length);
});
it('must not throw errors when publishing with invalid subscribers', function(){
var invalidSubscribers = [undefined, null, 'a string', 123, [], {}, new Date()],
topic = TestHelper.getUniqueString(),
i;
for (i = 0; i < invalidSubscribers.length; i++){
PubSub.subscribe(topic, invalidSubscribers[i]);
}
refute.exception(function(){
PubSub.publish(topic, TestHelper.getUniqueString());
});
});
});