'test case not working for callback setState function

I am calling two APIs using two different function. First, I am fetching order details using getOrder method. Once I get the order, I am calling another method for fetching driver location using updateDriverLocation method.

It's working perfect, as expected. But I am getting issue in test cases.

async getOrder(trackingID) {
    this.setState({
        loading:true
    });

    const result = await orderRepo.getTracker(trackingID);
    const { orderTracker } = result.data;

    this.setState({
        loading: false, 
        order: orderTracker
    }, this.updateDriverLocation);
}

async updateDriverLocation() {
  this.getDriverLocation();
  this.interval = setInterval(() => this.getDriverLocation(), 30000);
}

async getDriverLocation() {
  const driverLocationResult = await orderRepo.getDriverLocation(this.state.order.id);
  const { driverLocation } = driverLocationResult.data;

  this.setState({
    driverLocation: driverLocation
  });
}

Test case :

it('should get the order details', async () => {

    const component = shallow(<OrderTracker match={matchStub} />);

    const instance = component.instance();
    jest.spyOn(instance, 'setState');

    // component.setState = jest.fn();

    await instance.getOrder(trackingIDStub);

    expect(instance.setState).toHaveBeenNthCalledWith(1, expect.objectContaining({
        loading: true
    }));

    expect(instance.setState).toHaveBeenNthCalledWith(2, expect.objectContaining({
        loading: false,
        order: orderStub
    }));
});

it('should get driver live location', async () => {

    const mockStore = configureMockStore();
    const store = mockStore(driverLocationStub);

    orderRepo.getDriverLocation.mockReturnValue({driverLocationStub});
    const  result = await orderRepo.getDriverLocation(2010158066);

    const {driverLocation} = result.data;

    expect(driverLocation).toEqual(driverLocationStub);
});

Here I am getting errors in both the function of test cases.

In first test case : expected result does not match

Expected: ObjectContaining {"loading": false, "order": {"deliveryTime": "2020-07-30 15:00:00", "id": 2010158066, "orderTypeId": 2, "shipment": {"estimatedDeliveryDate": "2020-10-19 14:00:00", "tracking": [{"location": "Cleveland, OH", "status": "Ready to ship", "time": "2020-10-19 19:49:00"}]}, "shippingAddress": {"address1": "1084 East Lancaster Ave", "city": "Bryn Mawr", "postcode": "12345", "state": "PA"}, "store": {"lat": 40.1234, "lng": -75.1234, "storefrontImage": "http://cookies.test/uploads/store/abc.jpg"}, "trackingID": 123}}
Received
       1: {"loading": true}
->     2: {"loading": false, "order": {"deliveryTime": "2020-07-30 15:00:00", "id": 2010158066, "orderTypeId": 2, "shipment": {"estimatedDeliveryDate": "2020-10-19 14:00:00", "tracking": [{"location": "Cleveland, OH", "status": "Ready to ship", "time": "2020-10-19 19:49:00"}]}, "shippingAddress": {"address1": "1084 East Lancaster Ave", "city": "Bryn Mawr", "postcode": "12345", "state": "PA"}, "store": {"lat": 40.1234, "lng": -75.1234, "storefrontImage": "http://cookies.test/uploads/store/abc.jpg"}, "trackingID": 123}}, [Function updateDriverLocation]
       3: {"loading": false, "order": {"deliveryTime": "2020-07-30 15:00:00", "id": 2010158066, "orderTypeId": 2, "shipment": {"estimatedDeliveryDate": "2020-10-19 14:00:00", "tracking": [{"location": "Cleveland, OH", "status": "Ready to ship", "time": "2020-10-19 19:49:00"}]}, "shippingAddress": {"address1": "1084 East Lancaster Ave", "city": "Bryn Mawr", "postcode": "12345", "state": "PA"}, "store": {"lat": 40.1234, "lng": -75.1234, "storefrontImage": "http://cookies.test/uploads/store/abc.jpg"}, "trackingID": 123}}, [Function updateDriverLocation]

In second test case : TypeError: Cannot read property 'driverLocation' of undefined

Please help me to write test cases for above two methods. Thanks in advance.



Sources

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

Source: Stack Overflow

Solution Source