|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import {TestBed} from '@angular/core/testing'; |
| 10 | +import {EMPTY, of} from 'rxjs'; |
| 11 | +import {map, withLatestFrom} from 'rxjs/operators'; |
10 | 12 |
|
11 | 13 | import {InitialRenderPendingTasks} from '../../src/initial_render_pending_tasks'; |
12 | 14 |
|
13 | 15 | describe('InitialRenderPendingTasks', () => { |
14 | | - it('should resolve a promise even if there are no tasks', async () => { |
15 | | - const pendingTasks = TestBed.inject(InitialRenderPendingTasks); |
16 | | - expect(pendingTasks.completed).toBe(false); |
17 | | - await pendingTasks.whenAllTasksComplete; |
18 | | - expect(pendingTasks.completed).toBe(true); |
19 | | - }); |
20 | | - |
21 | 16 | it('should wait until all tasks are completed', async () => { |
22 | 17 | const pendingTasks = TestBed.inject(InitialRenderPendingTasks); |
23 | | - expect(pendingTasks.completed).toBe(false); |
24 | | - |
25 | 18 | const taskA = pendingTasks.add(); |
26 | 19 | const taskB = pendingTasks.add(); |
27 | 20 | const taskC = pendingTasks.add(); |
28 | | - expect(pendingTasks.completed).toBe(false); |
29 | 21 |
|
30 | 22 | pendingTasks.remove(taskA); |
31 | 23 | pendingTasks.remove(taskB); |
32 | 24 | pendingTasks.remove(taskC); |
33 | | - await pendingTasks.whenAllTasksComplete; |
34 | | - expect(pendingTasks.completed).toBe(true); |
| 25 | + expect(await hasPendingTasks(pendingTasks)).toBeFalse(); |
35 | 26 | }); |
36 | 27 |
|
37 | 28 | it('should allow calls to remove the same task multiple times', async () => { |
38 | 29 | const pendingTasks = TestBed.inject(InitialRenderPendingTasks); |
39 | | - expect(pendingTasks.completed).toBe(false); |
| 30 | + expect(await hasPendingTasks(pendingTasks)).toBeFalse(); |
40 | 31 |
|
41 | 32 | const taskA = pendingTasks.add(); |
42 | | - |
43 | | - expect(pendingTasks.completed).toBe(false); |
| 33 | + expect(await hasPendingTasks(pendingTasks)).toBeTrue(); |
44 | 34 |
|
45 | 35 | pendingTasks.remove(taskA); |
46 | 36 | pendingTasks.remove(taskA); |
47 | 37 | pendingTasks.remove(taskA); |
48 | 38 |
|
49 | | - await pendingTasks.whenAllTasksComplete; |
50 | | - expect(pendingTasks.completed).toBe(true); |
| 39 | + expect(await hasPendingTasks(pendingTasks)).toBeFalse(); |
51 | 40 | }); |
52 | 41 |
|
53 | 42 | it('should be tolerant to removal of non-existent ids', async () => { |
54 | 43 | const pendingTasks = TestBed.inject(InitialRenderPendingTasks); |
55 | | - expect(pendingTasks.completed).toBe(false); |
| 44 | + expect(await hasPendingTasks(pendingTasks)).toBeFalse(); |
56 | 45 |
|
57 | 46 | pendingTasks.remove(Math.random()); |
58 | 47 | pendingTasks.remove(Math.random()); |
59 | 48 | pendingTasks.remove(Math.random()); |
60 | 49 |
|
61 | | - await pendingTasks.whenAllTasksComplete; |
62 | | - expect(pendingTasks.completed).toBe(true); |
| 50 | + expect(await hasPendingTasks(pendingTasks)).toBeFalse(); |
63 | 51 | }); |
64 | 52 | }); |
| 53 | + |
| 54 | +function hasPendingTasks(pendingTasks: InitialRenderPendingTasks): Promise<boolean> { |
| 55 | + return of(EMPTY) |
| 56 | + .pipe( |
| 57 | + withLatestFrom(pendingTasks.hasPendingTasks), |
| 58 | + map(([_, hasPendingTasks]) => hasPendingTasks), |
| 59 | + ) |
| 60 | + .toPromise(); |
| 61 | +} |
0 commit comments