forked from hubotio/hubot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlistener_test.js
379 lines (313 loc) · 12.4 KB
/
listener_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
'use strict'
/* global describe, beforeEach, it */
/* eslint-disable no-unused-expressions */
// Assertions and Stubbing
const chai = require('chai')
const sinon = require('sinon')
chai.use(require('sinon-chai'))
const expect = chai.expect
// Hubot classes
const EnterMessage = require('../src/message').EnterMessage
const TextMessage = require('../src/message').TextMessage
const Listener = require('../src/listener').Listener
const TextListener = require('../src/listener').TextListener
const Response = require('../src/response')
const User = require('../src/user')
describe('Listener', function () {
beforeEach(function () {
// Dummy robot
this.robot = {
// Re-throw AssertionErrors for clearer test failures
emit (name, err, response) {
if (err.constructor.name === 'AssertionError') {
return process.nextTick(function () {
throw err
})
}
},
// Ignore log messages
logger: {
debug () {}
},
// Why is this part of the Robot object??
Response
}
// Test user
this.user = new User({
id: 1,
name: 'hubottester',
room: '#mocha'
})
})
describe('Unit Tests', function () {
describe('#call', function () {
it('calls the matcher', function (done) {
const callback = sinon.spy()
const testMatcher = sinon.spy()
const testMessage = {}
const testListener = new Listener(this.robot, testMatcher, callback)
testListener.call(testMessage, function (_) {
expect(testMatcher).to.have.been.calledWith(testMessage)
done()
})
})
it('passes the matcher result on to the listener callback', function (done) {
const matcherResult = {}
const testMatcher = sinon.stub().returns(matcherResult)
const testMessage = {}
const listenerCallback = response => expect(response.match).to.be.equal(matcherResult)
// sanity check; matcherResult must be truthy
expect(matcherResult).to.be.ok
const testListener = new Listener(this.robot, testMatcher, listenerCallback)
testListener.call(testMessage, function (result) {
// sanity check; message should have been processed
expect(testMatcher).to.have.been.called
expect(result).to.be.ok
done()
})
})
describe('if the matcher returns true', function () {
beforeEach(function () {
this.createListener = function (cb) {
return new Listener(this.robot, sinon.stub().returns(true), cb)
}
})
it('executes the listener callback', function (done) {
const listenerCallback = sinon.spy()
const testMessage = {}
const testListener = this.createListener(listenerCallback)
testListener.call(testMessage, function (_) {
expect(listenerCallback).to.have.been.called
done()
})
})
it('returns true', function () {
const testMessage = {}
const testListener = this.createListener(function () {})
const result = testListener.call(testMessage)
expect(result).to.be.ok
})
it('calls the provided callback with true', function (done) {
const testMessage = {}
const testListener = this.createListener(function () {})
testListener.call(testMessage, function (result) {
expect(result).to.be.ok
done()
})
})
it('calls the provided callback after the function returns', function (done) {
const testMessage = {}
const testListener = this.createListener(function () {})
let finished = false
testListener.call(testMessage, function (result) {
expect(finished).to.be.ok
done()
})
finished = true
})
it('handles uncaught errors from the listener callback', function (done) {
const testMessage = {}
const theError = new Error()
const listenerCallback = function (response) {
throw theError
}
this.robot.emit = function (name, err, response) {
expect(name).to.equal('error')
expect(err).to.equal(theError)
expect(response.message).to.equal(testMessage)
done()
}
const testListener = this.createListener(listenerCallback)
testListener.call(testMessage, sinon.spy())
})
it('calls the provided callback with true if there is an error thrown by the listener callback', function (done) {
const testMessage = {}
const theError = new Error()
const listenerCallback = function (response) {
throw theError
}
const testListener = this.createListener(listenerCallback)
testListener.call(testMessage, function (result) {
expect(result).to.be.ok
done()
})
})
it('calls the listener callback with a Response that wraps the Message', function (done) {
const testMessage = {}
const listenerCallback = function (response) {
expect(response.message).to.equal(testMessage)
done()
}
const testListener = this.createListener(listenerCallback)
testListener.call(testMessage, sinon.spy())
})
it('passes through the provided middleware stack', function (testDone) {
const testMessage = {}
const testListener = this.createListener(function () {})
const testMiddleware = {
execute (context, next, done) {
expect(context.listener).to.be.equal(testListener)
expect(context.response).to.be.instanceof(Response)
expect(context.response.message).to.be.equal(testMessage)
expect(next).to.be.a('function')
expect(done).to.be.a('function')
testDone()
}
}
testListener.call(testMessage, testMiddleware, sinon.spy())
})
it('executes the listener callback if middleware succeeds', function (testDone) {
const listenerCallback = sinon.spy()
const testMessage = {}
const testListener = this.createListener(listenerCallback)
testListener.call(testMessage, function (result) {
expect(listenerCallback).to.have.been.called
// Matcher matched, so we true
expect(result).to.be.ok
testDone()
})
})
it('does not execute the listener callback if middleware fails', function (testDone) {
const listenerCallback = sinon.spy()
const testMessage = {}
const testListener = this.createListener(listenerCallback)
const testMiddleware = {
execute (context, next, done) {
// Middleware fails
done()
}
}
testListener.call(testMessage, testMiddleware, function (result) {
expect(listenerCallback).to.not.have.been.called
// Matcher still matched, so we true
expect(result).to.be.ok
testDone()
})
})
it('unwinds the middleware stack if there is an error in the listener callback', function (testDone) {
const listenerCallback = sinon.stub().throws(new Error())
const testMessage = {}
let extraDoneFunc = null
const testListener = this.createListener(listenerCallback)
const testMiddleware = {
execute (context, next, done) {
extraDoneFunc = sinon.spy(done)
next(context, extraDoneFunc)
}
}
testListener.call(testMessage, testMiddleware, function (result) {
// Listener callback was called (and failed)
expect(listenerCallback).to.have.been.called
// Middleware stack was unwound correctly
expect(extraDoneFunc).to.have.been.called
// Matcher still matched, so we true
expect(result).to.be.ok
testDone()
})
})
})
describe('if the matcher returns false', function () {
beforeEach(function () {
this.createListener = function (cb) {
return new Listener(this.robot, sinon.stub().returns(false), cb)
}
})
it('does not execute the listener callback', function (done) {
const listenerCallback = sinon.spy()
const testMessage = {}
const testListener = this.createListener(listenerCallback)
testListener.call(testMessage, function (_) {
expect(listenerCallback).to.not.have.been.called
done()
})
})
it('returns false', function () {
const testMessage = {}
const testListener = this.createListener(function () {})
const result = testListener.call(testMessage)
expect(result).to.not.be.ok
})
it('calls the provided callback with false', function (done) {
const testMessage = {}
const testListener = this.createListener(function () {})
testListener.call(testMessage, function (result) {
expect(result).to.not.be.ok
done()
})
})
it('calls the provided callback after the function returns', function (done) {
const testMessage = {}
const testListener = this.createListener(function () {})
let finished = false
testListener.call(testMessage, function (result) {
expect(finished).to.be.ok
done()
})
finished = true
})
it('does not execute any middleware', function (done) {
const testMessage = {}
const testListener = this.createListener(function () {})
const testMiddleware = { execute: sinon.spy() }
testListener.call(testMessage, result => {
expect(testMiddleware.execute).to.not.have.been.called
done()
})
})
})
})
describe('#constructor', function () {
it('requires a matcher', () => expect(function () {
return new Listener(this.robot, undefined, {}, sinon.spy())
}).to.throw(Error))
it('requires a callback', function () {
// No options
expect(function () {
return new Listener(this.robot, sinon.spy())
}).to.throw(Error)
// With options
expect(function () {
return new Listener(this.robot, sinon.spy(), {})
}).to.throw(Error)
})
it('gracefully handles missing options', function () {
const testMatcher = sinon.spy()
const listenerCallback = sinon.spy()
const testListener = new Listener(this.robot, testMatcher, listenerCallback)
// slightly brittle because we are testing for the default options Object
expect(testListener.options).to.deep.equal({ id: null })
expect(testListener.callback).to.be.equal(listenerCallback)
})
it('gracefully handles a missing ID (set to null)', function () {
const testMatcher = sinon.spy()
const listenerCallback = sinon.spy()
const testListener = new Listener(this.robot, testMatcher, {}, listenerCallback)
expect(testListener.options.id).to.be.null
})
})
describe('TextListener', () =>
describe('#matcher', function () {
it('matches TextMessages', function () {
const callback = sinon.spy()
const testMessage = new TextMessage(this.user, 'test')
testMessage.match = sinon.stub().returns(true)
const testRegex = /test/
const testListener = new TextListener(this.robot, testRegex, callback)
const result = testListener.matcher(testMessage)
expect(result).to.be.ok
expect(testMessage.match).to.have.been.calledWith(testRegex)
})
it('does not match EnterMessages', function () {
const callback = sinon.spy()
const testMessage = new EnterMessage(this.user)
testMessage.match = sinon.stub().returns(true)
const testRegex = /test/
const testListener = new TextListener(this.robot, testRegex, callback)
const result = testListener.matcher(testMessage)
expect(result).to.not.be.ok
expect(testMessage.match).to.not.have.been.called
})
})
)
})
})