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 basic ngIf
condition to display your MyComp
or not.
const obsCleanUpFunction = jasmine.createSpy();
const obs$ = new Observable(observer => {
//following will be called when/if the observable if completed
return obsCleanUpFunction;
});
MockComponent
with the MyComp
in itngOnInit
and subscribe to itMockComponent
, set the condition to display MyComp
to false --> it'll be destroyed by the ngIf
evaluating to falsetoHaveBeenCalled