forked from indiesquidge/javascript-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-eventing-test.js
72 lines (51 loc) · 2.19 KB
/
add-eventing-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
const test = require('tape')
const sinon = require('sinon')
const addEventing = require('./add-eventing')
test('it should call the callback on the trigger event', assert => {
const obj = addEventing({ name: 'Kelsey', age: 30 })
const ageChangeEvent = sinon.spy()
obj.on('ageChange', ageChangeEvent)
obj.trigger('ageChange')
assert.ok(ageChangeEvent.called, 'ageChangeEvent should have been called')
assert.end()
})
test.skip('it should be able to handle multiple callback functions for the same event name', assert => {
const obj = addEventing({ name: 'Kelsey', age: 30 })
const ageChangeEvent = sinon.spy()
const otherChangeEvent = sinon.spy()
obj.on('ageChange', ageChangeEvent)
obj.on('ageChange', otherChangeEvent)
obj.trigger('ageChange')
assert.ok(ageChangeEvent.called, 'ageChangeEvent should have been called')
assert.ok(otherChangeEvent.called, 'otherChangeEvent should have been called')
assert.end()
})
test.skip('if it is called with an additional argument it should pass it to the listeners', assert => {
const obj = addEventing({ name: 'Kelsey', age: 30 })
const ageChangeEvent = sinon.spy()
const newAge = 31
const message = `ageChangeEvent should have been called with ${newAge}`
obj.on('ageChange', ageChangeEvent)
obj.trigger('ageChange', newAge)
assert.ok(ageChangeEvent.calledWith(newAge), message)
assert.end()
})
test.skip('if it is called with an multiple arguments it should pass them to the listeners', assert => {
const obj = addEventing({ name: 'Kelsey', age: 30 })
const ageChangeEvent = sinon.spy()
const newName = 'Alice'
const newAge = 31
const message = `ageChangeEvent should have been called with ${newName} and ${newAge}`
obj.on('ageChange', ageChangeEvent)
obj.trigger('ageChange', newName, newAge)
assert.ok(ageChangeEvent.calledWith(newName, newAge), message)
assert.end()
})
test.skip('it should not trigger events that were not set to listen', assert => {
const obj = addEventing({ name: 'Kelsey', age: 30 })
const notCalledEvent = sinon.spy()
obj.on('somethingElse', notCalledEvent)
obj.trigger('ageChange')
assert.notOk(notCalledEvent.called, 'notCalledEvent should not have been called')
assert.end()
})