Is there a way in which I can write a unit test to check whether I have unsubscribed successfully from my Observable subscription?
I am using ngx-auto-unsubscribe.
example:
@AutoUnsubscribe()
@Component
// Template and style urls
export class MyComp implements OnInit, OnDestroy {
mySub: Subscription;
constructor(private myService: MyService) { }
ngOnInit() {
this.myService.doSomething.subscribe(res => {
console.log(res);
})
}
ngOnDestroy() {
// Only used for AOT (ahead of time) compilation
}
}
Yes there's a way (even though I don't think it's necessary to test that).
I'm not going to write the whole test for you but here's the logic:
MockComponent) in your test with a basicngIfcondition to display yourMyCompor not.
MockComponentwith theMyCompin itngOnInitand subscribe to itMockComponent, set the condition to displayMyCompto false --> it'll be destroyed by thengIfevaluating to falsetoHaveBeenCalled