jeudi 7 janvier 2021

Mocking Angular Dialog afterClosed() for unit test

I have troubles with mocking popup data for unit testing. Getting an error like: TypeError: Cannot read property 'showFilterModal' of undefined What am I doing wrong?

FilterComponent

public openFilterModal() {
  this.filterModal = this.modalService.showFilterModal(this.filtersOptions, this.userRole, 'bills');
  this.filterModal.afterClosed
    .subscribe(
      (result) => {
        this.selectedFiltersAmount = result.total;
        this.filtersOptions = result.filters;
      },
      (err) => {

      });
}

not working unit test example

it('should create subscription after close modal', () => {
  component.openFilterModal();
  const spy = spyOn(component.filterModal, 'afterClosed').and.returnValue(
    of({
      total: 1,
      filters: null,
      reqObj: {
        status: 'active',
      }
    })
  );

  expect(component.filterModal).toBeDefined();
  expect(spy).toHaveBeenCalled();
});

Aucun commentaire:

Enregistrer un commentaire