|  | 
| 8 | 8 | 
 | 
| 9 | 9 | import {ApplicationRef, ExperimentalPendingTasks} from '@angular/core'; | 
| 10 | 10 | import {TestBed} from '@angular/core/testing'; | 
| 11 |  | -import {EMPTY, of} from 'rxjs'; | 
| 12 |  | -import {map, take, withLatestFrom} from 'rxjs/operators'; | 
|  | 11 | +import {EMPTY, firstValueFrom, of} from 'rxjs'; | 
|  | 12 | +import {filter, map, take, withLatestFrom} from 'rxjs/operators'; | 
| 13 | 13 | 
 | 
| 14 | 14 | import {PendingTasks} from '../../src/pending_tasks'; | 
| 15 | 15 | 
 | 
| @@ -80,10 +80,57 @@ describe('public ExperimentalPendingTasks', () => { | 
| 80 | 80 |  TestBed.inject(ApplicationRef).tick(); | 
| 81 | 81 |  await expectAsync(applicationRefIsStable(appRef)).toBeResolvedTo(true); | 
| 82 | 82 |  }); | 
|  | 83 | + | 
|  | 84 | + it('should allow blocking stability with run', async () => { | 
|  | 85 | + const appRef = TestBed.inject(ApplicationRef); | 
|  | 86 | + const pendingTasks = TestBed.inject(ExperimentalPendingTasks); | 
|  | 87 | + | 
|  | 88 | + let resolveFn: () => void; | 
|  | 89 | + pendingTasks.run(() => { | 
|  | 90 | + return new Promise<void>((r) => { | 
|  | 91 | + resolveFn = r; | 
|  | 92 | + }); | 
|  | 93 | + }); | 
|  | 94 | + await expectAsync(applicationRefIsStable(appRef)).toBeResolvedTo(false); | 
|  | 95 | + resolveFn!(); | 
|  | 96 | + await expectAsync(TestBed.inject(ApplicationRef).whenStable()).toBeResolved(); | 
|  | 97 | + }); | 
|  | 98 | + | 
|  | 99 | + it('should return the result of the run function', async () => { | 
|  | 100 | + const appRef = TestBed.inject(ApplicationRef); | 
|  | 101 | + const pendingTasks = TestBed.inject(ExperimentalPendingTasks); | 
|  | 102 | + | 
|  | 103 | + const result = await pendingTasks.run(async () => { | 
|  | 104 | + await expectAsync(applicationRefIsStable(appRef)).toBeResolvedTo(false); | 
|  | 105 | + return 1; | 
|  | 106 | + }); | 
|  | 107 | + | 
|  | 108 | + expect(result).toBe(1); | 
|  | 109 | + await expectAsync(applicationRefIsStable(appRef)).toBeResolvedTo(false); | 
|  | 110 | + await expectAsync(TestBed.inject(ApplicationRef).whenStable()).toBeResolved(); | 
|  | 111 | + }); | 
|  | 112 | + | 
|  | 113 | + xit('should stop blocking stability if run promise rejects', async () => { | 
|  | 114 | + const appRef = TestBed.inject(ApplicationRef); | 
|  | 115 | + const pendingTasks = TestBed.inject(ExperimentalPendingTasks); | 
|  | 116 | + | 
|  | 117 | + let rejectFn: () => void; | 
|  | 118 | + const task = pendingTasks.run(() => { | 
|  | 119 | + return new Promise<void>((_, reject) => { | 
|  | 120 | + rejectFn = reject; | 
|  | 121 | + }); | 
|  | 122 | + }); | 
|  | 123 | + await expectAsync(applicationRefIsStable(appRef)).toBeResolvedTo(false); | 
|  | 124 | + try { | 
|  | 125 | + rejectFn!(); | 
|  | 126 | + await task; | 
|  | 127 | + } catch {} | 
|  | 128 | + await expectAsync(applicationRefIsStable(appRef)).toBeResolvedTo(true); | 
|  | 129 | + }); | 
| 83 | 130 | }); | 
| 84 | 131 | 
 | 
| 85 | 132 | function applicationRefIsStable(applicationRef: ApplicationRef) { | 
| 86 |  | - return applicationRef.isStable.pipe(take(1)).toPromise(); | 
|  | 133 | + return firstValueFrom(applicationRef.isStable); | 
| 87 | 134 | } | 
| 88 | 135 | 
 | 
| 89 | 136 | function hasPendingTasks(pendingTasks: PendingTasks): Promise<boolean> { | 
|  | 
0 commit comments