I have an async function in my angular2 app for which I want to write a unit test. Imagine my function is like this:
myFunc(a: int): Promise<void> {
if (a == 1)
throw new Error('a should not be 1');
let body = {
value: a
};
return apiService.patch('/url/', JSON.stringify(body)).toPromise();
}
Now, I'm thinking of checking that if condition. I tried the following code; but, this test always fails since my code actually does not wait for any results:
it('should throw error if a = 1', () => {
expect(() => {
mySerivce.myFunc(1);
}).toThrow(new Error('a should not be 1'));
})
I don't know how I should write unit tests for these types of logics...