-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
160 lines (144 loc) · 3.99 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
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
/* eslint-env mocha */
const signal = require('./')
const expect = require('expect.js')
describe('smoke signal', function () {
it('should allow to listen to events', function (done) {
const onTrigger = signal()
onTrigger.push(done)
onTrigger.trigger()
})
it('should run all handlers that are active when calling the event, no matter if they where paused by an other event', function () {
let handlerCalled = false
const onTrigger = signal()
function handler() {
handlerCalled = true
}
// first handler, that pauses second handler
onTrigger.push(() => {
onTrigger.pull(handler)
})
// second handler
onTrigger.push(handler)
// trigger event
onTrigger.trigger()
// both handlers should be called
expect(handlerCalled).to.be(true)
})
it('should be called with arguments', function (done) {
const 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) {
const onTrigger = signal()
function unheard() {
done('should not be called')
}
onTrigger.push(unheard)
onTrigger.pull(unheard)
onTrigger.push(done)
onTrigger.trigger()
})
it('should be able to pause by reference', function (done) {
const onTrigger = signal()
function paused() {
done('should not be called')
}
const listener = onTrigger.push(paused)
listener.pause()
onTrigger.push(done)
onTrigger.trigger()
})
it('should be able to resume by reference', function (done) {
const onTrigger = signal()
const listener = onTrigger.push(done)
listener.pause()
listener.resume()
onTrigger.trigger()
})
it('should be able to listen only once', function () {
let collect = '--'
const 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) {
const onTrigger = signal()
function unheard() {
done('should not be called')
}
onTrigger.push(unheard)
onTrigger.clear()
onTrigger.trigger()
done()
})
it('should handle exceptions', function (done) {
const 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) {
const error = new Error('should land in error handler')
const onTrigger = signal({
onError: function (err) {
expect(error).to.eql(err)
done()
},
})
onTrigger.push(function () {
throw error
})
onTrigger.trigger()
})
})
describe('async smoke-signal', function () {
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
it('should run wait for all async listeners to settle', async function () {
let triggered = false
const onTrigger = signal({ logExceptions: true })
onTrigger.push(async function () {
await sleep(1)
triggered = true
})
expect(triggered).to.be(false)
await onTrigger.triggerAsync()
expect(triggered).to.be(true)
})
it('should handle exceptions', async function () {
const onTrigger = signal()
onTrigger.push(async function () {
await sleep(1)
throw new Error('should not bubble up')
})
await onTrigger.triggerAsync()
})
it('should allow to add custom exception handler', async function () {
let catchedError
const error = new Error('should land in error handler')
const onTrigger = signal({
onError: function (err) {
catchedError = err
},
})
onTrigger.push(async function () {
await sleep(1)
throw error
})
await onTrigger.triggerAsync()
expect(catchedError).to.be(error)
})
})