Currently, I am utilizing Angular 7 and endeavoring to create some unit tests for this basic component.
export class DialogComponent implements OnInit {
appError: any;
httpErrorResponse: HttpErrorResponse;
constructor(
public dialogRef: MatDialogRef<DialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: any) {
Eif (data && typeof AppError) {
this.appError = data;
} else {
throw new Error('unknown error type');
}
}
ngOnInit() {
}
navigate(url: string): void {
if (!url) {
throw new Error('url is undefined');
}
window.location.href = url;
}
}
Regrettably, my test cases are not successfully covering the
throw new Error('unknown error type');
or the entire navigate
function.
Here is an attempt that I have made: Although it has been successful in a sense, the coverage issue remains unresolved, indicating my lack of success.
it('should run #navigate()', async () => {
const dialog = spyOn(component, 'navigate');
component.navigate('http://dummy.com');
expect(dialog).toHaveBeenCalledWith('http://dummy.com');
});
I would greatly appreciate any guidance on how to approach this issue.
Edit: AppError
export class AppError {
title?: string;
customMessage?: string;
message?: string;
popUpCloseable: boolean;
navigation?: string;
details?: string;
status?: any;
code?: any;
name?: any;
url?: string;
statusText?: string;
customTextButton?: string;
}