I am trying to unit test a service which gets a value from another service (returns a Promise) then execute http GET request.
I am using HttpTestingController from @angular/common/http/testing to mock the http request. Here is the test and prod code :
tested.service.spec.ts
it('should get base url from indexedDb then execute http get request', (async () => {
fakeParametresService.getBaseUrl.and.returnValue(Promise.resolve({valeur: 'http://host:port'}));
testedService.get<any>('/endpoint').then((response: any) => {
expect(response.key).toBe('value');
});
await http.expectOne('http://host:port/endpoint').flush({key: 'value'});
http.verify();
}));
tested.service.ts
async get<T>(endpoint: string): Promise<T> {
const baseUrl = await this.parametresService.getBaseUrl();
return this.http.get<T>(`${baseUrl.valeur}${endpoint}`).toPromise();
}
It seems that a http request nested within a promise cannot be mocked that way. But maybe it's a misunderstanding from me.