samedi 3 août 2019

How to test Promise then method

How do I test the code inside the then() method of a Promise. I do not see code coverage for the code inside the then(..) method of my code. My unit test passes and throws no errors, but when I look at line coverage, the code inside toPromise.then(//this code) is not covered. How do I test that code.

//service file
getDataFromAPI(num: string): Promise<Model> {
  return this._http
   .get<Model>('url')
   .toPromise()
   .then(resolve => {
       const { item1, item2, item3 } = resolve;
       return { item1, item2, item3 };
  });
}


//unit test
describe('getDataFromAPI', () => {
it('should get data', () => {

  const service: Service = TestBed.get(Service);
  const httpClientGetSpy = jest.spyOn(httpClient, 'get');

  service.getDataFromAPI(num).then(response => {
    expect(response.item1).toEqual('one');
    expect(response.item2).toEqual('two');
    expect(response.item3).toEqual('three');
  });

  expect(httpClientGetSpy).toHaveBeenCalled();
});
});

Aucun commentaire:

Enregistrer un commentaire