|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {DestroyRef, EnvironmentInjector, Injector, runInInjectionContext} from '../../src/core'; |
10 | | -import {BehaviorSubject} from 'rxjs'; |
| 9 | +import { |
| 10 | + Component, |
| 11 | + DestroyRef, |
| 12 | + EnvironmentInjector, |
| 13 | + inject, |
| 14 | + Injector, |
| 15 | + runInInjectionContext, |
| 16 | +} from '../../src/core'; |
| 17 | +import {BehaviorSubject, finalize} from 'rxjs'; |
11 | 18 |
|
12 | 19 | import {takeUntilDestroyed} from '../src/take_until_destroyed'; |
| 20 | +import {TestBed} from '@angular/core/testing'; |
13 | 21 |
|
14 | 22 | describe('takeUntilDestroyed', () => { |
15 | 23 | it('should complete an observable when the current context is destroyed', () => { |
@@ -76,4 +84,39 @@ describe('takeUntilDestroyed', () => { |
76 | 84 |
|
77 | 85 | expect(unregisterFn).toHaveBeenCalled(); |
78 | 86 | }); |
| 87 | + |
| 88 | + // https://github.com/angular/angular/issues/54527 |
| 89 | + it('should unsubscribe after the current context has already been destroyed', async () => { |
| 90 | + const recorder: any[] = []; |
| 91 | + |
| 92 | + // Note that we need a "real" view for this test because, in other cases, |
| 93 | + // `DestroyRef` would resolve to the root injector rather than to the |
| 94 | + // `NodeInjectorDestroyRef`, where `lView` is used. |
| 95 | + @Component({template: ''}) |
| 96 | + class TestComponent { |
| 97 | + destroyRef = inject(DestroyRef); |
| 98 | + |
| 99 | + source$ = new BehaviorSubject(0); |
| 100 | + |
| 101 | + ngOnDestroy(): void { |
| 102 | + Promise.resolve().then(() => { |
| 103 | + this.source$ |
| 104 | + .pipe( |
| 105 | + takeUntilDestroyed(this.destroyRef), |
| 106 | + finalize(() => recorder.push('finalize()')), |
| 107 | + ) |
| 108 | + .subscribe((value) => recorder.push(value)); |
| 109 | + }); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + const fixture = TestBed.createComponent(TestComponent); |
| 114 | + fixture.destroy(); |
| 115 | + |
| 116 | + // Wait until the `ngOnDestroy` microtask is run. |
| 117 | + await Promise.resolve(); |
| 118 | + |
| 119 | + // Ensure the `value` is not recorded, but unsubscribed immediately. |
| 120 | + expect(recorder).toEqual(['finalize()']); |
| 121 | + }); |
79 | 122 | }); |
0 commit comments