|
1 | 1 | import { fakeAsync, tick } from '@angular/core/testing'; |
2 | 2 | import { BehaviorSubject, Notification, Observable, of } from 'rxjs'; |
| 3 | +import { map } from 'rxjs/operators'; |
3 | 4 | import { |
4 | 5 | DoTransferableWork, |
5 | 6 | DoTransferableWorkUnit, |
@@ -76,12 +77,6 @@ describe('runWorker', () => { |
76 | 77 | }), |
77 | 78 | ); |
78 | 79 |
|
79 | | - expect(postMessageSpy).toHaveBeenCalledWith( |
80 | | - jasmine.objectContaining({ |
81 | | - kind: 'C', |
82 | | - }), |
83 | | - ); |
84 | | - |
85 | 80 | sub.unsubscribe(); |
86 | 81 | }); |
87 | 82 |
|
@@ -128,13 +123,51 @@ describe('runWorker', () => { |
128 | 123 | [expected.buffer] as any, |
129 | 124 | ); |
130 | 125 |
|
| 126 | + sub.unsubscribe(); |
| 127 | + }); |
| 128 | + |
| 129 | + // https://github.com/cloudnc/observable-webworker/issues/116 |
| 130 | + it('should complete the notification stream when the worker completes', () => { |
| 131 | + const postMessageSpy = spyOn(window, 'postMessage'); |
| 132 | + postMessageSpy.calls.reset(); |
| 133 | + |
| 134 | + class TestWorker implements DoWork<number, number> { |
| 135 | + public work(input$: Observable<number>): Observable<number> { |
| 136 | + // here nothing should keep the subscription alive when input$ completes |
| 137 | + return input$.pipe(map(input => input * 2)); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + const sub = runWorker(TestWorker); |
| 142 | + |
| 143 | + const notificationEvent: WorkerMessageNotification<number> = new MessageEvent('message', { |
| 144 | + data: new Notification('N', 10), |
| 145 | + }); |
| 146 | + |
| 147 | + self.dispatchEvent(notificationEvent); |
| 148 | + |
| 149 | + expect(postMessageSpy).toHaveBeenCalledWith( |
| 150 | + jasmine.objectContaining({ |
| 151 | + kind: 'N', |
| 152 | + value: 20, |
| 153 | + }), |
| 154 | + ); |
| 155 | + |
| 156 | + const completeEvent: WorkerMessageNotification<number> = new MessageEvent('message', { |
| 157 | + data: new Notification('C'), |
| 158 | + }); |
| 159 | + |
| 160 | + self.dispatchEvent(completeEvent); |
| 161 | + |
131 | 162 | expect(postMessageSpy).toHaveBeenCalledWith( |
132 | 163 | jasmine.objectContaining({ |
133 | 164 | kind: 'C', |
134 | 165 | }), |
135 | 166 | ); |
136 | 167 |
|
137 | | - sub.unsubscribe(); |
| 168 | + // do note here that instead of manually closing the subscription |
| 169 | + // we check it's already closed as expected |
| 170 | + expect(sub.closed).toBeTrue(); |
138 | 171 | }); |
139 | 172 |
|
140 | 173 | it('should not complete the notification stream if the worker does not complete', () => { |
@@ -197,12 +230,6 @@ describe('runWorker', () => { |
197 | 230 | }), |
198 | 231 | ); |
199 | 232 |
|
200 | | - expect(postMessageSpy).toHaveBeenCalledWith( |
201 | | - jasmine.objectContaining({ |
202 | | - kind: 'C', |
203 | | - }), |
204 | | - ); |
205 | | - |
206 | 233 | sub.unsubscribe(); |
207 | 234 | })); |
208 | 235 | }); |
0 commit comments