forked from StephanHoyer/smoke-signal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
97 lines (88 loc) · 2.31 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
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
/* eslint-env mocha */
var signal = require('./')
var expect = require('expect.js')
describe('smoke signal', function () {
it('should allow to listen to events', function (done) {
var onTrigger = signal()
onTrigger.push(done)
onTrigger.trigger()
})
it('should be called with arguments', function (done) {
var onTrigger = signal()
onTrigger.push(function (arg1, arg2) {
expect(arg1).to.be(1)
expect(arg2).to.be(2)
done()
})
onTrigger.trigger(1, 2)
})
it('should be able to unlisten', function (done) {
var onTrigger = signal()
function unlistend () {
done('should not be called')
}
onTrigger.push(unlistend)
onTrigger.pull(unlistend)
onTrigger.push(done)
onTrigger.trigger()
})
it('should be able to pause by reference', function (done) {
var onTrigger = signal()
function paused () {
done('should not be called')
}
var listener = onTrigger.push(paused)
listener.pause()
onTrigger.push(done)
onTrigger.trigger()
})
it('should be able to resume by reference', function (done) {
var onTrigger = signal()
var listener = onTrigger.push(done)
listener.pause()
listener.resume()
onTrigger.trigger()
})
it('should be able to resume by reference', function () {
var collect = '--'
var onTrigger = signal()
onTrigger.once(function (str) {
collect += str
})
onTrigger.trigger('++')
onTrigger.trigger('||')
onTrigger.trigger('==')
expect(collect).to.be('--++')
})
it('should be able to unlisten all', function (done) {
var onTrigger = signal()
function unlistend () {
done('should not be called')
}
onTrigger.push(unlistend)
onTrigger.clear()
onTrigger.trigger()
done()
})
it('should handle exceptions', function (done) {
var onTrigger = signal()
onTrigger.push(function () {
throw new Error('should not bubble up')
})
onTrigger.push(done)
onTrigger.trigger()
})
it('should allow to add custom exception handler', function (done) {
var error = new Error('should land in error handler')
var onTrigger = signal({
onError: function (err) {
expect(error).to.eql(err)
done()
}
})
onTrigger.push(function () {
throw error
})
onTrigger.trigger()
})
})