'how to unit test a function with multiple ifs inside?

I have this big function that I'm gonna show here, it is very simple, but somehow I cannot find how to do the unit tests for it. I don't need the exact unit test, just a general method for ifs and even ifs cause i'm really struggling with it. Also when I call the function in my ts (I'm using Angular.js) this line : this.nominativo = this.praticaDetail.operazioneDomiciliazioneDto.utente is the onli line that the coverage returns as covered and I dunno why cause it is identical to the followings like this this.variazione = this.praticaDetail.operazioneDomiciliazioneDto.dataOperazione;

updateIstanza() {
    
    this.nominativo = this.praticaDetail.operazioneDomiciliazioneDto.utente 
    this.variazione = this.praticaDetail.operazioneDomiciliazioneDto.dataOperazione;
    this._bc.set('@pratica', 'Pratica ' + this.praticaDetail.numeroIstanza);
    this.accordions.splice(4, 1);
    this.richiesta();
    if (
      this.praticaDetail.funzioniDto.urlRicevuta != ''
    ) {
      this.showRicevuta = true;
    }

    if(this.praticaDetail.statoDomiciliazioneDto.statoDomiciliazione == 4 || 
      this.praticaDetail.statoDomiciliazioneDto.statoDomiciliazione == 5 || 
      this.praticaDetail.statoDomiciliazioneDto.statoDomiciliazione == 9) {
      this.buttonDownload = true;
    }
    if(this.praticaDetail.statoDomiciliazioneDto.statoDomiciliazione == 4){
      this.buttonRevoca = true;
    }
  
    if (this.praticaDetail.statoDomiciliazioneDto.descrizione == STATI.ANNULLATA) {
      this.disabledStato = true;
      this.objectAlert = {
        title: 'Istanza annullata',
        text: this.praticaDetail.operazioneDomiciliazioneDto.noteOperazione
      }
      this.visualizzaAlert = true;
    }else if(this.praticaDetail.statoDomiciliazioneDto.descrizione == STATI.DA_DEFINIRE){
      this.notShow = true;
      if(this.showRicevuta){
        this.buttonDownload = true;
      }
    }else if(this.praticaDetail.statoDomiciliazioneDto.descrizione == STATI.IN_COMPILAZIONE){
      this.annullaButton = true;
    }else if(this.praticaDetail.statoDomiciliazioneDto.descrizione == STATI.SOSPESA) {
      this.showDefinizioneSospInteg = true;
      this.praticaSospesa = true;
    }else if(this.praticaDetail.statoDomiciliazioneDto.descrizione == STATI.ATTESA_SOTTOSCRIZIONE) {
      this.showSottoscrizione = true;
      this.objectAlert = {
        title: 'Attesa Sottoscrizione',
        text: "L'integrazione è stata inserita con successo: a breve verrà generata una ricevuta che completerà il processo d'integrazione"
      }
    }else if(this.praticaDetail.statoDomiciliazioneDto.descrizione == STATI.IN_DEFINIZIONE) {
      this.showDefinizione = true;
      this.disabledStato = true;
    }else if(this.praticaDetail.statoDomiciliazioneDto.descrizione == STATI.ACCETTATA) {
      this.notShow = true;
      this.notShowStato = true;
      this.objectAlert = {
        title: 'Domiciliazione Bancaria accettata',
        text: ''}
      if(this.showRicevuta) {
        this.buttonDownload = true;
      }
    }else if (
      this.praticaDetail.statoDomiciliazioneDto.descrizione == STATI.IN_REVOCA) {
      if(this.praticaDetail.inAttesa){
        this.disabledStato = true;
        this.objectAlert = {
          title: 'Attesa Sottoscrizione',
          text: "La richiesta di revoca è stata inserita con successo: a breve verrà completato il processo di sottoscrizione"
        }

      } else {
        this.showAsInRevocafunction();
        this.showAnnullata = true;
        this.accordionComponent?.openAccordion(
        this.accordionComponent.CONSTANTS.ID_ACCORDIONS.DOCUMENTI
        );
        this.sottoscrizioneCompletata = true;
      }
    }else if (this.praticaDetail.statoDomiciliazioneDto.descrizione == STATI.DA_REVOCARE) {
      this.showDefinizione = true;
      this.disabledStato = true;
      this.notShowStato = true;
      if(this.praticaDetail.inAttesa){
        this.objectAlert = {
          title: 'Attesa Istituto Bancario',
          text: "L'istanza si trova in attesa dell'esito da parte dell'istituto bancario"
        }
        this.visualizzaAlert = true;
        this.showSottoscrizione = true;
      }
    }
    }
     
    }
    
    if (this.dettaglio) {
      this.accordions = [
        new AccordionAssociazioneVeicolo(),
        new AccordionDatiRichiedente(),
        new AccordionDatiRichiesta(),
        new AccordionDocumenti(),
      ];
      this.notShowStato = true;
      this.showRettifica = false;
      this.inRettifica = false;
    }
    this.eventsSubject.next({
      istanza: this.praticaDetail,
      disabledStato: this.disabledStato,
    });
  } ```




Solution 1:[1]

  1. Setup object properties so that a specific path will be tested
  2. Call updateIstanza
  3. Test against expected values

Example:

it('should set showRicevuta true when urlRicevuta is not empty', () => {
  const foo = new Foo();
  foo.praticaDetail.funzioniDto.urlRicevuta = 'not empty';
  foo.updateIstanza();

  expect(foo.showRicevuta).toBeTrue();
});

BTW having such a large function with so many control paths is not good practice.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wlf