Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
Unit testing delete method that is called after mat dialog is closed

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!