0

Function to be tested

changeIva(idTax: number, index: number): void {
    this.documentService.getTaxById(idTax).subscribe(tax => {
      if (tax.codigo === 'ISE') this.openModal(index);
    });
  }

coverage

How far I've come

it('should test whether the tax is exempt or not', () => {
    const taxMockSuccess = taxMock[0];
    const idTax = 106;
    const index = 1;
    const modalOpenSpy = spyOn(component['modal'], 'open');
    const documentServiceSpy = spyOn(documentService, 'getTaxById').and.returnValue(
      of({ taxMockSuccess })
    );

    component.changeIva(idTax, index);

    expect(documentServiceSpy).toBeCalledWith(idTax);
    expect(modalOpenSpy).toBeCalled();
  });

I need help, I don't know how to test this IF within the subscribe

Michael Rovinsky
  • 6,807
  • 7
  • 15
  • 30

1 Answers1

1

You need to set tax.codigo to 'ISE'. This will then pass through the IF statement properly.

Change the spy so the returned value handles this

    const documentServiceSpy = spyOn(documentService, 'getTaxById').and.returnValue(
      of({ taxMockSuccess })
    );

Whatever taxMockSuccess is, it should have the value for the codigo property set to 'ISE'.

PMO1948
  • 2,210
  • 11
  • 34