forked from brendon1982/ts-mocking-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_PubSub.jasmine.ts
158 lines (133 loc) · 4.92 KB
/
06_PubSub.jasmine.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { PubSub } from '../tests-to-implement/06_PubSub'
import { promisify } from 'util';
describe('PubSub', () => {
describe('subscribe', () => {
beforeEach(function () {
jasmine.clock().uninstall();
jasmine.clock().install();
})
afterEach(function() {
jasmine.clock().uninstall();
})
it('calls subscription callback when publish occurs on channel', async () => {
// Arrange
let sut = new PubSub();
let payloadSent = "";
sut.subscribe("channel1", (payload) => payloadSent = payload);
// Act
await sut.publish("channel1", "hello");
jasmine.clock().tick(600);
// Assert
expect(payloadSent).toEqual("hello");
})
it('calls all subscription callbacks when publish occurs on channel', async () => {
// Arrange
let sut = new PubSub();
let payloadsSent: String[] = [];
sut.subscribe("channel1", (payload) => payloadsSent.push(payload));
sut.subscribe("channel1", (payload) => payloadsSent.push(payload));
sut.subscribe("channel1", (payload) => payloadsSent.push(payload));
// Act
await sut.publish("channel1", "hello");
jasmine.clock().tick(600);
// Assert
expect(payloadsSent).toEqual(['hello', 'hello', 'hello']);
})
})
describe('subscribe promise', () => {
it('calls subscription callback when publish occurs on channel', async () => {
// Arrange
let sut = new PubSub();
let payloadSent = "";
let callback: (...params: any[]) => any = (s) => s;
let callbackPromise = new Promise((resolve, reject) => {
callback = (payload:string) => {
payloadSent = payload;
resolve(payloadSent);
};
});
sut.subscribe("channel1", callback);
// Act
await sut.publish("channel1", "hello");
// Assert
await expectAsync(callbackPromise).toBeResolved();
expect(payloadSent).toEqual("hello");
})
it('calls all subscription callbacks when publish occurs on channel', async () => {
// Arrange
let sut = new PubSub();
let payloadsSent: string[] = [];
let callback1: (...params: any[]) => any = (s) => s;
let callback2: (...params: any[]) => any = (s) => s;
let callbackPromise1 = new Promise((resolve, reject) => {
callback1 = (payload:string) => {
payloadsSent.push(payload);
resolve(payload);
};
});
let callbackPromise2 = new Promise((resolve, reject) => {
callback2 = (payload:string) => {
payloadsSent.push(payload);
resolve(payload);
};
});
sut.subscribe("channel1", callback1);
sut.subscribe("channel1", callback2);
// Act
await sut.publish("channel1", "hello");
// Assert
await expectAsync(callbackPromise1).toBeResolved();
await expectAsync(callbackPromise2).toBeResolved();
expect(payloadsSent).toEqual(['hello', 'hello']);
})
it('calls all subscription callbacks when publish occurs on channel (alt)', async () => {
// Arrange
let sut = new PubSub();
let payloadsSent: string[] = [];
let {callback:callback1, promise:callbackPromise1} = createCallback(payloadsSent);
let {callback:callback2, promise:callbackPromise2} = createCallback(payloadsSent);
sut.subscribe("channel1", callback1);
sut.subscribe("channel1", callback2);
// Act
await sut.publish("channel1", "hello");
// Assert
await expectAsync(callbackPromise1).toBeResolved();
await expectAsync(callbackPromise2).toBeResolved();
expect(payloadsSent).toEqual(['hello', 'hello']);
})
function createCallback(payloadsSent: string[]): {callback: (...params: any[]) => any, promise: Promise<any>} {
let callback: (...params: any[]) => any = (s) => s;
let callbackPromise = new Promise((resolve, reject) => {
callback = (payload:string) => {
payloadsSent.push(payload);
resolve(payload);
};
});
return {callback, promise: callbackPromise};
}
it('calls all subscription callbacks when publish occurs on channel (alt2)', async () => {
// Arrange
let sut = new PubSub();
var callback1 = jasmine.createSpy();
var callback1WasCalled = callBackWasCalled(callback1);
var callback2 = jasmine.createSpy();
var callback2WasCalled = callBackWasCalled(callback2);
sut.subscribe("channel1", callback1);
sut.subscribe("channel1", callback2);
// Act
await sut.publish("channel1", "hello");
// Assert
await callback1WasCalled;
expect(callback1).toHaveBeenCalledWith("hello");
await callback2WasCalled;
expect(callback2).toHaveBeenCalledWith("hello");
})
async function callBackWasCalled(callback: jasmine.Spy) {
return new Promise<void>((resolve => {
callback.and.callFake((payload) =>
resolve()
)
}))
}
})
})