0
votes

I am trying to mock and trying to satisfy else condition for below method but I am getting error as Expected spy modalService.open not to have been called

Here is the component code

After updating the below line component.isError = true;

Highlighting the If block was not coming but still the error is available

public importDeals(upload, list) {
  this.fileName = '';
   let ngbModalOptions: NgbModalOptions = {
    backdrop : 'static',
    keyboard : false,
    windowClass: 'custom-class'
};   
if (!this.isError) {
  this.uploadModalRef = this.modalService.open(upload, ngbModalOptions);
}
this.tempContingency = list;
}

Below is the current unit test case (Jasmine)

it('should import deals', () => {
  // component.importDeals;
  // expect(component.importDeals('upload','list')).toBeUndefined();
  component.importDeals;
  component.uploadModalRef = jasmine.createSpyObj('uploadModalRef', ['close']);
  let mockOptions: NgbModalOptions = {
    backdrop : 'static',
    keyboard : false,
    windowClass: 'custom-class'
  };
  const mockConfirm = 'confirm-template';
  component.importDeals(mockConfirm,'');
  expect(modalService.open).toHaveBeenCalledWith(mockConfirm, mockOptions);
});
it('should not import deals', () => { 
  component.importDeals; 
  component.modalService = jasmine.createSpyObj('modalService',['open'])
  const mockConfirm = 'confirm-template'; 

  component.importDeals(mockConfirm,''); 
  expect(modalService.open).not.toHaveBeenCalled(); 
  });

Kindly let me know what I am doing wrong here

1

1 Answers

1
votes

Set the value of isError to true otherwise that if block will always execute.

it('should not import deals', () => { 
  component.importDeals; 
  component.modalService = jasmine.createSpyObj('modalService',['open']);
  component.isError = true; // <- this line
  const mockConfirm = 'confirm-template'; 

  component.importDeals(mockConfirm,''); 
  expect(modalService.open).not.toHaveBeenCalled(); 
});