0

I'm trying to unit test a function that calls another function. This is the called function

detailsFiltersClosed(recentSearch: boolean): void {
        this.arrowUp = false;
        this.filterData = this.loadsFilter.getData();
        const isTypeAll: boolean = this.filterData.loadType === Capacity.Both;
        const loadType: string = isTypeAll ? DetailsFiltersResources.All : this.filterData.loadType;

    }

and this is the function that calls it:

searchAndCloseDetails(){
    this.detailsFiltersClosed(false);
    setTimeout(() => {
        this.trigger.closeMenu();
    },0);    
}

variable arrowUp is a booelan property of the component.

I wrote this unit test:

describe('searchAndCloseDetails', () => {
   let arrow = component.arrowUp = true;
   component.searchAndCloseDetails();
   expect(component.arrowUp).toEqual(false);
});

however it fails saying: TypeError: "Cannot read property 'loadType' of undefined"

May you help me figure out where is my error?

Thanks.

Julio Rodríguez
  • 447
  • 1
  • 8
  • 23
  • It seems to be that filter data is undefined. Is there something wrong with the `this.loadsFilter.getData();` method? Is `this.loadsFilter` being injected in the component constructor? – Derrick Rose Aug 12 '21 at 03:30

1 Answers1

0

Maybe because this.filterData is undefined.

const loadType: string = isTypeAll ? DetailsFiltersResources.All : this.filterData.loadType;