I have a method
delete(document: IDocument): void {
const headerMessage = ''
const message = ''
const confirm = ''
const reject = ''
this.dialogService.openDialog(headerMessage, message, confirm, reject);
this.dialogService.isClosed().subscribe(confirmed => {
if (confirmed && document.id) {
this.documentService.delete(document.id).subscribe(() => {
this.loadPage();
});
}
});
}
I simply cant test it right.
Unit test is this:
it('should call delete service using confirmDialog',() => {
// GIVEN
const dialogService = TestBed.inject(DialogService);
jest.spyOn(dialogService.dialogRef, 'afterClosed').mockReturnValue(of({}));
const result = dialogService.isClosed();
expect(result instanceof Observable).toBe(true);
expect(dialogService.dialogRef.afterClosed).toHaveBeenCalled();
jest.spyOn(service, 'delete').mockReturnValue(of(new HttpResponse({})));
// WHEN
comp.delete({ id: 123 });
// THEN
expect(service.delete).toHaveBeenCalledWith(123);
});
I get error:
Cannot spyOn on a primitive value; undefined given
139 |
140 | const dialogService = TestBed.inject(DialogService);
> 141 | jest.spyOn(dialogService.dialogRef, 'afterClosed').mockReturnValue(of({}));
Since I can't just test delete method, I need to mock dialog and delete if its accepted but dont understand the error. Below is the confirm dialog service:
export class DialogService {
dialogRef!: MatDialogRef<ConfirmationDialogComponent>;
constructor(private dialog: MatDialog) {}
openDialog(headerMsg: string, msg: string, confirm: string, reject: string): void {
this.dialogRef = this.dialog.open(ConfirmationDialogComponent, {
data: {
header: headerMsg,
message: msg,
buttonText: {
ok: confirm,
cancel: reject,
},
},
});
}
isClosed(): Observable<any> {
return this.dialogRef.afterClosed();
}
}
So basically, I just need mock that accept is clicked and then check the delete method