-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
60 lines (51 loc) · 1.46 KB
/
test.js
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
// Setup
var Subscribable = require('./subscribable.js');
var test = {};
/*
Tests
*/
// export
test['was Subscribable exported'] = typeof Subscribable === 'function';
// properties of Subscribable.prototype exist
test['Subscribable.prototype.on exists'] = typeof Subscribable.prototype.on === 'function';
test['Subscribable.prototype.off exists'] = typeof Subscribable.prototype.off === 'function';
test['Subscribable.prototype.trigger exists'] = typeof Subscribable.prototype.trigger === 'function';
// on / trigger
var s = new Subscribable();
var timesFired = 0;
var hasFailed = false;
s.on('made up events rock', function() {
timesFired++;
});
if (timesFired !== 0) hasFailed = true;
s.trigger('made');
if (timesFired !== 1) hasFailed = true;
s.trigger('up');
if (timesFired !== 2) hasFailed = true;
s.trigger('events');
if (timesFired !== 3) hasFailed = true;
test['on / trigger'] = !hasFailed;
// s.off('two events')
timesFired = 0;
s.off('made events');
s.trigger('made');
s.trigger('events');
test['s.off(\'two events\')'] = timesFired === 0;
// s.off()
timesFired = 0;
s.off();
s.trigger('up');
s.trigger('rock');
test['s.off()'] = timesFired === 0;
// Closing message:
var stat = {
complete: 0,
pass: 0,
fail: 0
};
for (var testName in test) {
stat.complete++;
stat[test[testName] ? 'pass' : 'fail']++;
if (!test[testName]) console.log('test "%s" failed.', testName);
}
console.log('%d assertions completed. %d passed, and %d failed.', stat.complete, stat.pass, stat.fail);