-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathobservable.spec.ts
More file actions
262 lines (191 loc) · 7.59 KB
/
Copy pathobservable.spec.ts
File metadata and controls
262 lines (191 loc) · 7.59 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { BufferedObservable, mergeObservables, Observable } from './observable'
describe('observable', () => {
let observable: Observable<void>
let subscriber: jasmine.Spy<jasmine.Func>
beforeEach(() => {
observable = new Observable()
subscriber = jasmine.createSpy('sub')
})
it('should allow to subscribe and be notified', () => {
observable.subscribe(subscriber)
expect(subscriber).not.toHaveBeenCalled()
observable.notify()
expect(subscriber).toHaveBeenCalledTimes(1)
observable.notify()
expect(subscriber).toHaveBeenCalledTimes(2)
})
it('should notify multiple clients', () => {
const otherSubscriber = jasmine.createSpy('sub2')
observable.subscribe(subscriber)
observable.subscribe(otherSubscriber)
observable.notify()
expect(subscriber).toHaveBeenCalled()
expect(otherSubscriber).toHaveBeenCalled()
})
it('should allow to unsubscribe', () => {
const subscription = observable.subscribe(subscriber)
subscription.unsubscribe()
observable.notify()
expect(subscriber).not.toHaveBeenCalled()
})
it('should execute onFirstSubscribe callback', () => {
const onFirstSubscribe = jasmine.createSpy('callback')
const otherSubscriber = jasmine.createSpy('sub2')
observable = new Observable(onFirstSubscribe)
expect(onFirstSubscribe).not.toHaveBeenCalled()
observable.subscribe(subscriber)
expect(onFirstSubscribe).toHaveBeenCalledTimes(1)
observable.subscribe(otherSubscriber)
expect(onFirstSubscribe).toHaveBeenCalledTimes(1)
})
it('should notify the first subscriber if the onFirstSubscribe callback notifies synchronously ', () => {
const onFirstSubscribe = jasmine.createSpy('callback').and.callFake((observable: Observable<void>) => {
observable.notify()
})
observable = new Observable(onFirstSubscribe)
observable.subscribe(subscriber)
expect(onFirstSubscribe).toHaveBeenCalledTimes(1)
expect(subscriber).toHaveBeenCalledTimes(1)
})
it('should pass the observable instance to the onFirstSubscribe callback', () => {
const onFirstSubscribe = jasmine.createSpy('callback')
observable = new Observable(onFirstSubscribe)
observable.subscribe(subscriber)
expect(onFirstSubscribe).toHaveBeenCalledWith(observable)
})
it('should execute onLastUnsubscribe callback', () => {
const onLastUnsubscribe = jasmine.createSpy('callback')
const otherSubscriber = jasmine.createSpy('sub2')
observable = new Observable(() => onLastUnsubscribe)
const subscription = observable.subscribe(subscriber)
const otherSubscription = observable.subscribe(otherSubscriber)
expect(onLastUnsubscribe).not.toHaveBeenCalled()
subscription.unsubscribe()
expect(onLastUnsubscribe).not.toHaveBeenCalled()
otherSubscription.unsubscribe()
expect(onLastUnsubscribe).toHaveBeenCalled()
})
})
describe('mergeObservables', () => {
let observableOne: Observable<void>
let observableTwo: Observable<void>
let mergedObservable: Observable<void>
let subscriber: jasmine.Spy<jasmine.Func>
beforeEach(() => {
observableOne = new Observable<void>()
observableTwo = new Observable<void>()
mergedObservable = mergeObservables(observableOne, observableTwo)
subscriber = jasmine.createSpy('subscriber')
})
it('should notify when one of the merged observable notifies', () => {
mergedObservable.subscribe(subscriber)
observableOne.notify()
observableTwo.notify()
expect(subscriber).toHaveBeenCalledTimes(2)
})
it('should allow to unsubscribe to all merged observables', () => {
const subscription = mergedObservable.subscribe(subscriber)
subscription.unsubscribe()
observableOne.notify()
observableTwo.notify()
expect(subscriber).not.toHaveBeenCalled()
})
})
describe('BufferedObservable', () => {
it('invokes the observer with buffered data', async () => {
const observable = new BufferedObservable<string>(100)
observable.notify('first')
observable.notify('second')
const observer = jasmine.createSpy('observer')
observable.subscribe(observer)
await nextMicroTask()
expect(observer).toHaveBeenCalledTimes(2)
})
it('invokes the observer asynchronously', async () => {
const observable = new BufferedObservable<string>(100)
observable.notify('first')
const observer = jasmine.createSpy('observer')
observable.subscribe(observer)
expect(observer).not.toHaveBeenCalled()
await nextMicroTask()
expect(observer).toHaveBeenCalledWith('first')
})
it('invokes the observer when new data is notified after subscription', async () => {
const observable = new BufferedObservable<string>(100)
const observer = jasmine.createSpy('observer')
observable.subscribe(observer)
observable.notify('first')
await nextMicroTask()
observable.notify('second')
expect(observer).toHaveBeenCalledTimes(2)
expect(observer).toHaveBeenCalledWith('first')
expect(observer).toHaveBeenCalledWith('second')
})
it('drops data when the buffer is full', async () => {
const observable = new BufferedObservable<string>(2)
observable.notify('first') // This should be dropped
observable.notify('second')
observable.notify('third')
const observer = jasmine.createSpy('observer')
observable.subscribe(observer)
await nextMicroTask()
expect(observer).toHaveBeenCalledTimes(2)
expect(observer).toHaveBeenCalledWith('second')
expect(observer).toHaveBeenCalledWith('third')
})
it('allows to unsubscribe from the observer, the middle of buffered data', async () => {
const observable = new BufferedObservable<string>(100)
observable.notify('first')
observable.notify('second')
const observer = jasmine.createSpy('observer').and.callFake(() => {
subscription.unsubscribe()
})
const subscription = observable.subscribe(observer)
await nextMicroTask()
expect(observer).toHaveBeenCalledTimes(1)
})
it('allows to unsubscribe before the buffered data', async () => {
const observable = new BufferedObservable<string>(100)
observable.notify('first')
const observer = jasmine.createSpy('observer')
const subscription = observable.subscribe(observer)
subscription.unsubscribe()
await nextMicroTask()
expect(observer).not.toHaveBeenCalled()
})
it('allows to unsubscribe after the buffered data', async () => {
const observable = new BufferedObservable<string>(100)
const observer = jasmine.createSpy('observer')
const subscription = observable.subscribe(observer)
await nextMicroTask()
subscription.unsubscribe()
observable.notify('first')
expect(observer).not.toHaveBeenCalled()
})
it('calling unbuffer() removes buffered data', async () => {
const observable = new BufferedObservable<string>(2)
observable.notify('first')
observable.notify('second')
observable.unbuffer()
await nextMicroTask()
const observer = jasmine.createSpy('observer')
observable.subscribe(observer)
await nextMicroTask()
expect(observer).not.toHaveBeenCalled()
})
it('when calling unbuffer() right after subscription, buffered data should still be notified', async () => {
const observable = new BufferedObservable<string>(2)
observable.notify('first')
observable.notify('second')
const observer = jasmine.createSpy('observer')
observable.subscribe(observer)
observable.unbuffer()
await nextMicroTask()
expect(observer).toHaveBeenCalledTimes(2)
expect(observer).toHaveBeenCalledWith('first')
expect(observer).toHaveBeenCalledWith('second')
})
})
function nextMicroTask() {
return Promise.resolve()
}