-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathtests.ts
127 lines (105 loc) · 3.58 KB
/
tests.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// chai style expect().to.be.true violates no-unused-expression
/* tslint:disable:no-unused-expression */
import * as sinon from 'sinon';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as sinonChai from 'sinon-chai';
import { PubSub } from '../pubsub';
const isAsyncIterableIterator = (input: unknown): input is AsyncIterableIterator<unknown> => {
return input != null && typeof input[Symbol.asyncIterator] === 'function';
};
chai.use(chaiAsPromised);
chai.use(sinonChai);
const expect = chai.expect;
const assert: Chai.AssertStatic = chai.assert;
describe('PubSub', function() {
it('can subscribe and is called when events happen', () => {
const ps = new PubSub();
return ps.subscribe('a', payload => {
expect(payload).to.equals('test');
}).then(() => {
return ps.publish('a', 'test');
});
});
it('can unsubscribe', async () => {
const ps = new PubSub();
let subId = await ps.subscribe('a', payload => { assert(false); });
ps.unsubscribe(subId);
let succeed = await ps.publish('a', 'test');
expect(succeed).to.be.undefined;
});
});
describe('AsyncIterator', () => {
it('should expose valid asyncIterator for a specific event', () => {
const eventName = 'test';
const ps = new PubSub();
const iterator = ps.asyncIterableIterator(eventName);
expect(iterator).to.not.be.undefined;
expect(isAsyncIterableIterator(iterator)).to.be.true;
});
it('should trigger event on asyncIterator when published', done => {
const eventName = 'test';
const ps = new PubSub();
const iterator = ps.asyncIterableIterator(eventName);
iterator.next().then(result => {
expect(result).to.not.be.undefined;
expect(result.value).to.not.be.undefined;
expect(result.done).to.not.be.undefined;
done();
});
ps.publish(eventName, { test: true });
});
it('should not trigger event on asyncIterator when publishing other event', () => {
const eventName = 'test2';
const ps = new PubSub();
const iterator = ps.asyncIterableIterator('test');
const spy = sinon.spy();
iterator.next().then(spy);
ps.publish(eventName, { test: true });
expect(spy).not.to.have.been.called;
});
it('register to multiple events', done => {
const eventName = 'test2';
const ps = new PubSub();
const iterator = ps.asyncIterableIterator(['test', 'test2'] as const);
const spy = sinon.spy();
iterator.next().then(() => {
spy();
expect(spy).to.have.been.called;
done();
});
ps.publish(eventName, { test: true });
});
it('should not trigger event on asyncIterator already returned', done => {
const eventName = 'test';
const ps = new PubSub();
const iterator = ps.asyncIterableIterator(eventName);
iterator.next().then(result => {
expect(result).to.deep.equal({
value: undefined,
done: true,
});
}).catch(done);
ps.publish(eventName, { test: true });
iterator.next().then(result => {
expect(result).to.deep.equal({
value: undefined,
done: true,
});
done();
}).catch(done);
iterator.return();
ps.publish(eventName, { test: true });
});
it('should not register event listeners before next() is called', () => {
const testEventName = 'test';
class TestPubSub extends PubSub {
public listenerCount(eventName: string): number {
return this.ee.listenerCount(eventName);
}
}
const ps = new TestPubSub();
ps.asyncIterableIterator(testEventName);
expect(ps.listenerCount(testEventName)).to.equal(0);
});
});