Tuesday, 6 October 2020

PrimeNG and mocking confirmation service

I have a problem trying to mock the PrimeNG confirmation service, so I can test the accept function. I've got the following method on my component

deleteRow(rowData: any): void{

      this.confirmationService.confirm({
        message: 'Are you sure that you want to perform this deletion?',
        accept: (): void => {
          this.messageService.add({
            severity: 'success',
            summary: `Deleted`,
            detail: `Deleted ${rowData.name}`
          });

          const index: number = this.areas.indexOf(rowData);
          this.areas.splice(index, 1);
        }
    });
  }

and the following test

    const confirmationService: ConfirmationService = TestBed.get(ConfirmationService);

    const mockConfirm: any = spyOn(confirmationService, 'confirm').and.callFake((c: any) => {
      c.accept();
    });
    component.deleteRow({id: 'aaa'});

    expect(mockConfirm).toHaveBeenCalled();
    
  });

and here is my testbed setup

  beforeEach(async(() => {
      TestBed.configureTestingModule({
        schemas: [NO_ERRORS_SCHEMA],
        declarations: [AreasListComponent],
        imports: [FormsModule,
          ReactiveFormsModule,
          ConfirmDialogModule,
          FieldsetModule,
          TableModule,
          ToastModule,
          DropdownModule,
          NoopAnimationsModule
        ],
        providers: [
          ConfirmationService,
          MessageService
        ]
      })
        .compileComponents();
    })
  );

the mockconfirm toHaveBeenCalled fails, and when I debug it, doesn't seem to have the mock on the service.

Has anyone got any ideas?

Regards



from PrimeNG and mocking confirmation service

No comments:

Post a Comment