how can I write test cases in jest enzyme for the following If condition as I am new to the test cases

17 views Asked by At

I am checking the scenario where the totalQtyReceived>totalQtyRequested then the receipt should not be created I tried writing test cases but I am not able to cover the If path it shows error as If path not taken Thanks

if (totalQtyReceived > totalQtyRequired) {
      this.setState({
        receiptQuantityError: 'Received quantity exceeds Required Quantity',
        showIsCreateReceiptValidation: true,
        isReceiptCreating: false,
      });
      return;
    }

test cases I tried

 then ("Test isReceivedQuantityExceedsRequiredQunatityValidCheck function", ()=> {
    instance.setState({
      receiptCatelogueItemsStore: [
        { quantity_received: 10, quantity: 5 }, 
      ],
      receiptNonCatelogueItemsStore: [
        { quantity_received: 10, quantity: 8 },
      ],
    });
    instance.handleCreateReceipt();
    instance.isCreateReceiptValidCheck();
    
    expect(instance.state.receiptQuantityError).toEqual("");
    instance.setState({
      receiptQuantityError: "Received quantity exceeds Required Quantity",
    });
    expect(instance.state.showIsCreateReceiptValidation).toBe(true);
    expect(instance.state.isReceiptCreating).toBe(false);

  });

I need to get the coverage report as 80% but I got 40% with the coving of this path I can achive the desired result. As I am new to enzyme I got stucked. Any help would be much appreciated. Thanks a lot!

0

There are 0 answers