-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAsyncEventEmitter.test.ts
46 lines (43 loc) · 1.35 KB
/
AsyncEventEmitter.test.ts
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
import 'mocha';
import {expect} from 'chai';
import {AsyncEventEmitter} from '../../src';
describe('AsyncEventEmitter', () => {
let listenerResult;
let emitter: AsyncEventEmitter;
const callback = (data) => {
return new Promise((resolve) => {
setTimeout(() => {
listenerResult = data - 1;
resolve();
}, 500);
})
};
beforeEach(function () {
listenerResult = null;
emitter = new AsyncEventEmitter();
emitter.on('test', callback);
emitter.on('invalid', () => {
throw Error('Should not run!');
});
emitter.once('test', (data) => {
return new Promise((resolve) => {
setTimeout(() => {
expect(listenerResult).to.equal(data - 1);
listenerResult += 1;
resolve();
}, 200);
});
});
});
describe('#emit()', () => {
it('should execute Promise instances', () => {
return emitter.emit('test', 12345678).then(() => {
expect(listenerResult).to.equal(12345678);
}).then(() => {
return emitter.emit('test', 12345678).then(() => {
expect(listenerResult).to.equal(12345677);
});
});
});
});
});