Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda