Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda