Calling ngOnDestroy on test does not complete subscription
See original GitHub issue"@angular/core": "9.0.2",
"rxjs": "6.5.4",
"@ngneat/until-destroy": "7.1.1",
"jasmine-core": "3.5.0",
On a component defined as
@UntilDestroy()
@Component({
templateUrl: 'none-improvement.html',
selector: 'cv-none-improvement-page',
styleUrls: ['none-improvement.scss']
})
export class NoneImprovementPageComponent implements OnInit, OnDestroy {
public firstName: string;
constructor(
private _user: UserProfileStore,
) {
}
public ngOnInit() {
this._user.profile$
.pipe(
untilDestroyed(this)
)
.subscribe(u => this.firstName = u.get('firstName'));
}
public ngOnDestroy() {
//
}
}
I write this test:
it('stops getting first name from user profile after destroy', () => {
expect(sut.firstName).toBeUndefined();
sut.ngOnInit();
mockUserProfileStore.profileSubject$.next(new UserProfile(<any>{ firstName: 'testName' }).toImmutable());
expect(sut.firstName).toEqual('testName');
sut.ngOnDestroy();
mockUserProfileStore.profileSubject$.next(new UserProfile(<any>{ firstName: 'newName' }).toImmutable());
expect(sut.firstName).toEqual('testName');
});
which fails. But if I use untilDestroyed(this, 'ngOnDestroy')
passes. Is ngOnDestroy
not the default method that triggers the unsubscription?
Issue Analytics
- State:
- Created 4 years ago
- Comments:16 (3 by maintainers)
Top Results From Across the Web
How to unit test unsubscribe function in angular
I figure out a way to make it work. First of all, I do not create a subscription instance for each subscription, I...
Read more >Unsubscribing in Angular, the right way | by Ashwin Sathian
All we do in this method is calling unsubscribe on the Subscription if it's defined, as and when the component is destroyed. Simple,...
Read more >Best practices and patterns
The best way to work with subscription properties is to initialize it with an empty subscription so you do not need to check...
Read more >Angular – Auto-unsubscribe for RxJS | by Ildar Timerbaev
What does it mean? With this subscription, we can't say that the current state is valid because it can be changed from an...
Read more >Angular: Managing RxJS Observable Subscriptions with ...
Approach Three: ngneat/until-destroy. If you haven't come across this operator yet, you definitely need to check out this repo. It's called ngneat/until-destroy ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I tested, it works for me… If you don’t provide any reproduction then I see no reason to continue the dialogue, because it’s like “shooting a stone”. 😉
Well, as many other teams, we are testing many components (those that have no need to test UI) in unit tests without using Testbed. The component is instantiated manually using new and triggering the lifecycles hooks on demand. The component does have ngOnDestroy defined.