angularjasminekarma-jasmineangular-unit-test

Why I cant call a component's function in .spec.ts?


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.


Solution

  • Maybe because this.filterData is undefined.

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