Issue
I want to cover the folowing code usingjest:
    @Debounce(100)
    private checkDataToPositionInStep(): void {
        
        const proposalConsultData = this.proposalConsultResponseStore.get();
        if(proposalConsultData?.documentProposalData?.length > 1) {
            this.fullScreenLoaderService.hideLoader();
            this.router.navigate(['proposta-enviada']);
            return;
        }
        if(proposalConsultData?.warrantyData?.plateUf) {
            this.fullScreenLoaderService.hideLoader();
            this.router.navigate(['upload']);
        }
        if(proposalConsultData?.bankData?.branchCode) {
            this.fullScreenLoaderService.hideLoader();
            this.scrollService.next(STEP_ACCORDION.DADOS_GARANTIA.STEP);
            this.stepperService.next(STEP_ACCORDION.DADOS_GARANTIA.ID);
            return;
        }
        
        this.fullScreenLoaderService.hideLoader();
        this.scrollService.next(STEP_ACCORDION.DADOS_BANCARIOS.STEP);
        this.stepperService.next(STEP_ACCORDION.DADOS_BANCARIOS.ID);
        return;
    }
And de debounce decorator is like this:
export function Debounce(timeout: number): Function {
    return function (target, propertyKey: string, descriptor: PropertyDescriptor) {
        const original = descriptor.value;
        descriptor.value = function debounce(...args) {
            setTimeout(() => {
                original.apply(this, args);
            }, timeout);
        }
        return descriptor;
    }
}
When i run npm run:coverage all lines that are below decorators are not covering. Is there anyway to cover this lines ?
I just tried to call the checkDataToPositionStep method like this:
it('Should call checkDataToPositionInStep with only bankData', () => {ons
    const = proposalConsultMock = <any> {
      bankData: {
        branchCode: '01901'
      }
    };
    (facade as any).checkDataToPositionInStep(proposalConsultMock );
  });
And i thought that jest should cover the checkDataToPositionStep method.
Solution
Thanks for your help guys, i found the solution using jest.useFakeTimers() and jest.runAllTimers();. The problem was with the setTimeout. The following code now is covering the checkDataToPositionInStep method.
 proposalConsultMock = <any> {
  documentProposalData: [{
    tpDocumentoProposta: 149
},{
    tpDocumentoProposta: 90
  }],
};
jest.useFakeTimers();
setTimeout(() => {
  (facade as any).checkDataToPositionInStep(proposalConsultMock);
}, 1000);
jest.runAllTimers();
Answered By - Felipe Ken
 
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.