add more tests

This commit is contained in:
Pig Fang 2020-02-04 12:28:35 +08:00
parent 54b948d01e
commit 09c5aae79a

View File

@ -1,14 +1,30 @@
import * as emitter from '@/scripts/event'
test('add listener and emit event', () => {
const mockA = jest.fn()
const mockA1 = jest.fn()
const mockA2 = jest.fn()
const mockB = jest.fn()
emitter.on('a', mockA)
emitter.on('a', mockA1)
emitter.on('a', mockA2)
emitter.on('b', mockB)
emitter.emit('a')
expect(mockA).toBeCalledTimes(1)
expect(mockA1).toBeCalledTimes(1)
expect(mockA2).toBeCalledTimes(1)
expect(mockB).not.toBeCalled()
})
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()
})