I start to learn Angular testing recently and faced with a misunderstanding of the UNIT testing concept. For example, I have a dialog with with 2 buttons: Confirm delete and Cancel. When user click Confirm Delete button the http-service method is called and dialog closes. Sounds simple. Here is the code:
confirmDelete() {
this.httpService.delete(item)
.subscribe(() => this.dialogRef.close(),
(err) => this.message = 'Some text');
}
And my test for this function is:
it('should call service delete method and close the dialog', async( () => {
spyOn(dialogRef, 'close'); // I have mockDialogRef;
component.confirmDelete();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(deleteSpy.calls.any().toBe(true)); // Spy on delete method
expect(dialogRef.close).toHaveBeenCalled();
});
}));
And it worked fine. Seems to be the delete method has been called and dialog has been closed. But my question is. What we should test in such methods? Should we always test the stuff inside subscribe method or only that service methods is called? And should I Test here the case if server return error? So, ..sorry for stupid question, but I'm really confused. Hope you guys will help me(