-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples-typescript.ts
130 lines (122 loc) · 4.81 KB
/
examples-typescript.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
128
129
130
import * as Chai from 'chai';
import { Suite } from 'supposed';
import { Topic, ITopic, IEventMeta, ISubscriptionResult } from '.';
const test = Suite({
name: 'polyn-pubsub-ts',
assertionLibrary: Chai.expect,
reporter: 'tap'
})
test('using TypeScript', {
'given @polyn/async-events': {
'when I subscribe to multiple events in one call, and then publish to that topic': {
given: async () => {
const topic: ITopic = new Topic({ topic: 'ts' })
const eventNames: string[] = ['e1', 'e2', 'e3']
const events: { event: any, meta: IEventMeta }[] = []
const handler = (event: any, meta: IEventMeta) => events.push({ event, meta })
const subscriptions: ISubscriptionResult | ISubscriptionResult[] = await topic.subscribe(
[eventNames[0], eventNames[1], eventNames[2]],
handler
)
// these should not be published to
await topic.subscribe('foo1', handler)
await topic.subscribe('foo2', handler)
return { topic, eventNames, events, subscriptions }
},
when: async (
{ topic, eventNames, events, subscriptions }:
{
topic: ITopic;
eventNames: string[];
events: { event: any, meta: IEventMeta }[];
subscriptions: string[];
}
) => {
const expected = ['expected1', 'expected2', 'expected3']
const publishResults = [
await topic.publish(eventNames[0], expected[0]),
await topic.publish(eventNames[1], expected[1], { foo: 'bar' }),
await topic.publish(eventNames[2], expected[2]),
]
return {
expected,
topic,
eventNames,
events,
subscriptions,
publishResults
}
},
'it should return the count of emissions': (expect: Chai.ExpectStatic) => (err: Error | null, actual: any) => {
expect(err).to.be.null
const { publishResults } = actual
expect(publishResults[0].count).to.equal(1)
expect(publishResults[1].count).to.equal(1)
expect(publishResults[2].count).to.equal(1)
},
'it should publish data to each subscription': (expect: Chai.ExpectStatic) => (err: Error | null, actual: any) => {
expect(err).to.be.null
const { expected, events, subscriptions } = actual
expect(events.length).to.equal(subscriptions.length)
events.forEach(({ event }: { event: any, meta: IEventMeta }, idx: number) =>
expect(event).to.equal(expected[idx])
)
}
},
'when I subscribe to multiple events in one call, and then emit to that topic': {
given: async () => {
const topic: ITopic = new Topic({ topic: 'ts' })
const eventNames: string[] = ['e1', 'e2', 'e3']
const events: { event: any, meta: IEventMeta }[] = []
const handler = (event: any, meta: IEventMeta) => events.push({ event, meta })
const subscriptions: ISubscriptionResult | ISubscriptionResult[] = await topic.subscribe(
[eventNames[0], eventNames[1], eventNames[2]],
handler
)
// these should not be published to
await topic.subscribe('foo1', handler)
await topic.subscribe('foo2', handler)
return { topic, eventNames, events, subscriptions }
},
when: async (
{ topic, eventNames, events, subscriptions }:
{
topic: ITopic;
eventNames: string[];
events: { event: any, meta: IEventMeta }[];
subscriptions: string[];
}
) => new Promise(async (resolve) => {
const expected = ['expected1', 'expected2', 'expected3']
const publishResults = [
await topic.emit(eventNames[0], expected[0]),
await topic.emit(eventNames[1], expected[1], { foo: 'bar' }),
await topic.emit(eventNames[2], expected[2]),
]
setTimeout(() => resolve({
expected,
topic,
eventNames,
events,
subscriptions,
publishResults
}), 50) // wait for subscriptions to finish
}),
'it should return the count of emissions': (expect: Chai.ExpectStatic) => (err: Error | null, actual: any) => {
expect(err).to.be.null
const { publishResults } = actual
expect(publishResults[0].count).to.equal(1)
expect(publishResults[1].count).to.equal(1)
expect(publishResults[2].count).to.equal(1)
},
'it should emit data to each subscription': (expect: Chai.ExpectStatic) => (err: Error | null, actual: any) => {
expect(err).to.be.null
const { expected, events, subscriptions } = actual
expect(events.length).to.equal(subscriptions.length)
events.forEach(({ event }: { event: any, meta: IEventMeta }, idx: number) =>
expect(event).to.equal(expected[idx])
)
}
},
}
})