I am currently in the process of unit testing a function that looks like this:
async fetchGreatHouseByName(name: string) {
const [house] = await this.httpGetHouseByName(name);
const currentLord = house.currentLord ? house.currentLord : '957';
const result: any = await this.httpGetLord(currentLord);
const character = Characters.default.find(char => char.characterName === result.name);
return {...house, character};
}
Within this function, there are several HTTP GET calls. While I have a grasp on how to test them individually, I am unsure how to handle nested HTTP Calls, each of which returns a Promise.
This is what I have tried so far:
it('should get house from http', done => {
const httpMock: HttpTestingController = TestBed.inject(HttpTestingController);
const mockResponse = {
name: 'House One',
currentLord: 'Some Lord'
};
service.fetchGreatHouseByName('House One').then((house: House) => {
expect(house).toBeTruthy();
console.log(house);
});
const mockRequest = httpMock.expectOne(
'https://www.anapioficeandfire.com/api/houses?name=House%20Lannister%20of%20Casterly%20Rock'
);
mockRequest.flush(mockResponse);
});
So typically, httpGetHouseByName
would return something similar to mockRequest
. Whereas, httpGetLord
returns an object containing name
among other properties. Do I need another mock for this particular HTTP call and if so, how should I approach it?