2019-03-28 16:37:01 +08:00
|
|
|
import * as emitter from '@/scripts/event'
|
2018-09-07 10:14:31 +08:00
|
|
|
|
|
|
|
test('add listener and emit event', () => {
|
2020-02-04 12:28:35 +08:00
|
|
|
const mockA1 = jest.fn()
|
|
|
|
const mockA2 = jest.fn()
|
2019-03-15 11:42:41 +08:00
|
|
|
const mockB = jest.fn()
|
2018-09-07 10:14:31 +08:00
|
|
|
|
2020-02-04 12:28:35 +08:00
|
|
|
emitter.on('a', mockA1)
|
|
|
|
emitter.on('a', mockA2)
|
2019-03-15 11:42:41 +08:00
|
|
|
emitter.on('b', mockB)
|
2018-09-07 10:14:31 +08:00
|
|
|
|
2019-03-15 11:42:41 +08:00
|
|
|
emitter.emit('a')
|
2018-09-07 10:14:31 +08:00
|
|
|
|
2020-02-04 12:28:35 +08:00
|
|
|
expect(mockA1).toBeCalledTimes(1)
|
|
|
|
expect(mockA2).toBeCalledTimes(1)
|
2019-03-15 11:42:41 +08:00
|
|
|
expect(mockB).not.toBeCalled()
|
|
|
|
})
|
2020-02-04 12:28:35 +08:00
|
|
|
|
|
|
|
test('not throw for un-existed event', () => {
|
|
|
|
emitter.emit('c')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('unsubscribe event', () => {
|
|
|
|
const mock = jest.fn()
|
|
|
|
|
|
|
|
const off = emitter.on('c', mock)
|
|
|
|
off()
|
|
|
|
|
|
|
|
expect(mock).not.toBeCalled()
|
|
|
|
})
|