I am going through some articles on unit testing and have found some pretty helpful examples. Most examples I am running across seem to be unit testing a function that does something with the Angular HttpClient and most look like this:
it('should return an Observable<User[]>', () => {
const dummyUsers = [
{ login: 'John' },
{ login: 'Doe' }
];
service.getUsers().subscribe(users => {
expect(users.length).toBe(2);
expect(users).toEqual(dummyUsers);
});
const req = httpMock.expectOne(`${service.API_URL}/users`);
expect(req.request.method).toBe("GET");
req.flush(dummyUsers);
});
https://medium.com/netscape/testing-with-the-angular-httpclient-api-648203820712
This means that .getUsers() returns an observable. However, in my application, I am not returning an observable. Almost all of my HttpClient calls are similar to this:
async getCurrentUser(): Promise<UserInfo> {
return await this._httpClient.get(`/api/me`).toPromise();
}
How do you go about unit testing the function calling the HttpPost or HttpGet within Karma?
Attempt:
it('should get current user', async () => {
const result = service.getCurrentUser();
console.log('RESULT', result);
const req = httpMock.expectOne(`/api/me`);
const test = req.flush({test: 'ing'});
console.log('REQUEST', test);
expect('a').toEqual('a');
});