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

1 Answer 1

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'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.