forked from graphql/graphql-js
/
simplePubSub-test.js
55 lines (43 loc) · 1.53 KB
/
simplePubSub-test.js
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
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { SimplePubSub } from './simplePubSub';
describe('SimplePubSub', () => {
it('subscribe async-iterator mock', async () => {
const pubsub = new SimplePubSub();
const iterator = pubsub.getSubscriber((x) => x);
// Queue up publishes
expect(pubsub.emit('Apple')).to.equal(true);
expect(pubsub.emit('Banana')).to.equal(true);
// Read payloads
expect(await iterator.next()).to.deep.equal({
done: false,
value: 'Apple',
});
expect(await iterator.next()).to.deep.equal({
done: false,
value: 'Banana',
});
// Read ahead
const i3 = iterator.next().then((x) => x);
const i4 = iterator.next().then((x) => x);
// Publish
expect(pubsub.emit('Coconut')).to.equal(true);
expect(pubsub.emit('Durian')).to.equal(true);
// Await out of order to get correct results
expect(await i4).to.deep.equal({ done: false, value: 'Durian' });
expect(await i3).to.deep.equal({ done: false, value: 'Coconut' });
// Read ahead
const i5 = iterator.next().then((x) => x);
// Terminate queue
await iterator.return();
// Publish is not caught after terminate
expect(pubsub.emit('Fig')).to.equal(false);
// Find that cancelled read-ahead got a "done" result
expect(await i5).to.deep.equal({ done: true, value: undefined });
// And next returns empty completion value
expect(await iterator.next()).to.deep.equal({
done: true,
value: undefined,
});
});
});