Add test for EventDispatcher
This commit is contained in:
43
backend/src/Model/spec/EventDispatcher.spec.ts
Normal file
43
backend/src/Model/spec/EventDispatcher.spec.ts
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import 'mocha'
|
||||||
|
import { EventDispatcher } from '../../../../events'
|
||||||
|
|
||||||
|
import { expect } from 'chai'
|
||||||
|
import { doesNotReject } from 'assert'
|
||||||
|
|
||||||
|
describe('EventDispatcher', async () => {
|
||||||
|
it('should dispatch', async function () {
|
||||||
|
const dispatcher = new EventDispatcher<string, string>('me')
|
||||||
|
this.timeout(300)
|
||||||
|
|
||||||
|
setTimeout(() => dispatcher.dispatch('hello'), 5)
|
||||||
|
const response = await new Promise((resolve) => {
|
||||||
|
dispatcher.subscribe((msg) => {
|
||||||
|
resolve(msg)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(response).to.eq('hello')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should unsubscribe', async function () {
|
||||||
|
const dispatcher = new EventDispatcher<string, string>('me')
|
||||||
|
this.timeout(300)
|
||||||
|
let incrementee = 0
|
||||||
|
const callback = (msg: any) => {
|
||||||
|
incrementee += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatcher.subscribe(callback)
|
||||||
|
dispatcher.subscribe(callback)
|
||||||
|
dispatcher.unsubscribe(callback)
|
||||||
|
|
||||||
|
// Should increment to 1
|
||||||
|
dispatcher.dispatch('hello')
|
||||||
|
dispatcher.unsubscribe(callback)
|
||||||
|
|
||||||
|
// should not cause any increment
|
||||||
|
dispatcher.dispatch('hello')
|
||||||
|
|
||||||
|
expect(incrementee).to.eq(1)
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user