I am not able to test the behaviour of a method that calls an API service and subscribes to it.
The code of my method looks something like this:
verify(): void {
this.state = VerifyState.VERIFYING;
this.apiService.verify(this.formGroup.get('code').value).subscribe({
next: () => {
this.state = VerifyState.SUCCESS;
},
error: (err) => {
if (err.status === 'some_error') {
this.state = VerifyState.INVALID_OTP;
}
},
});
}
So basically on verify the variable state changes to VERIFYING and after an API call success it should change to SUCCESS . Simple right?
My test looks like this...
it('should set state to VERIFYING and then SUCCESS on successful verification', () => {
const apiService = spectator.inject(ApiService);
jest.spyOn(apiService, 'verify').mockReturnValue(of(null));
spectator.component.verify();
expect(spectator.component.state).toBe(VerifyState.VERIFYING);
spectator.detectChanges();
expect(spectator.component.state).toBe(VerifyState.SUCCESS);
});
In theory this should work, but I add a console.log inside the method subcriptions but it is not logged. Something is very wrong and I can't figure out what. I tried multiple things, fakeAsync with tick, or fixture.whenStable, nothing makes it work.
I tried multiple ways of providing the service,
mocks: [ApiService],
or
providers: [{
provide: ApiService,
useValue: {
verify: jest.fn(),
}
}]
and others.
Am I missing something obvious or ??
This won't fix your problem but a better way to do the
VERIFYINGandSUCCESSstate would be to use aSubjectbecauseof(null);can emit instantly and we won't have the opportunity to see that it isVERIFYING.I have added some comments for stuff that may work.
Something like this:
To debug why it does not go inside of your subscribe, make sure that the
const apiServiceis the sameApiServicein the component.