Skip to content

Commit e3cae37

Browse files
committed
test(eventable): add tests for registering multiple event names
1 parent bbd5551 commit e3cae37

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

spec/eventable.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,45 @@ describe('eventable', function () {
8383
this.obj.notify('publish', 'success')
8484
expect(called).toEqual(1)
8585
})
86+
87+
it('accepts multiple whitespace separated event names', function () {
88+
let called = 0
89+
this.obj.on('publish unpublish', () => {
90+
called++
91+
})
92+
93+
this.obj.notify('publish')
94+
this.obj.notify('unpublish')
95+
this.obj.notify('foo') // should do nothing
96+
expect(called).toEqual(2)
97+
})
98+
99+
it('accepts an object to register multiple events', function () {
100+
let published = 0
101+
let unpublished = 0
102+
103+
this.obj.on({
104+
publish: () => { published++ },
105+
unpublish: () => { unpublished++ }
106+
})
107+
108+
this.obj.notify('publish')
109+
this.obj.notify('unpublish')
110+
expect(published).toEqual(1)
111+
expect(unpublished).toEqual(1)
112+
})
113+
114+
it('accepts multiple event names in object form', function () {
115+
let called = 0
116+
117+
this.obj.on({
118+
'publish unpublish': () => { called++ }
119+
})
120+
121+
this.obj.notify('publish')
122+
this.obj.notify('unpublish')
123+
expect(called).toEqual(2)
124+
})
86125
})
87126

88127
describe('off()', function () {

src/eventable.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ export default function eventable (obj, notifyContext) {
3535
function getEventableModule (notifyContext) {
3636
let listeners = {}
3737

38-
function addListener (event, listener) {
39-
event.split(' ').forEach(value => {
40-
listeners[value] = listeners[value] || []
41-
listeners[value].unshift(listener)
38+
function addListener (events, listener) {
39+
events.split(' ').forEach(event => {
40+
listeners[event] = listeners[event] || []
41+
listeners[event].unshift(listener)
4242
})
4343
}
4444

0 commit comments

Comments
 (0)