testing issue in redux saga method using jest

138 views Asked by At

I am testing my redux saga method using jest. I am facing some issues. Attaching the saga method, test case & the console response.

Let me know where I missed it. Tried lots of googling. But couldn’t find any solution.

Any help would be appreciated.

thanks!

/* saga method starts*/
export function* fetchStripeLocations() {
    console.log('saga fetchStripeLocations called:');
    try {
        const BE = yield select((state) => state.BE);
        const PaymentRequestFormReducer = yield select(state => state.PaymentRequestFormReducer.toJS());
        const { stripeLocationsMap } = PaymentRequestFormReducer;
        const paymentsEnabled = isPaymentsEnabled(BE);

        if (!stripeLocationsMap.data && !stripeLocationsMap.isLoading && paymentsEnabled) {
            yield put(actionCreator.fetchingStripeLocations());
            const url = actionCreator.URL.FETCH_STRIPE_LOCATIONS;
            const response = yield call(beAPIResource.get, url, {}, { topLoader: false, isPrimaryAPI: false, cancellable: false });
            const { qcrEnabled, stripeLocationsMap } = response.data;
            yield put(actionCreator.setStripeLocations({ qcrEnabled, stripeLocationsMap }));
        }
    } catch (e) {
        yield put(actionCreator.setStripeLocations({ stripeLocationsMap: {} }));
        yield put(dataFetchingError(e, "Something went wrong please try again later"));
    }
}
/* saga method ends*/

/* test case starts*/
import { testData } from "./TestReducerData";
import { commonTestData } from "../../../../../../__test__/commonTestData";

test('fetchStripeLocations testing should pass', async () => {
    const dispatchedActions = [];
    const stripeDummyData = iMap({
        isLoading: false,
        data: null,
        qcrEnabled: false
    });

    const mockData = { 
        qcrEnabled : false, 
        stripeLocationsMap: {
            isLoading: false,
            data: null,
            qcrEnabled: false
        }
    };

    const mRequest = jest.fn(() => Promise.resolve(mockData));
    const mockState = {
        BE: commonTestData,
        PaymentRequestFormReducer: testData.initialState
    };
    await runSaga({
            dispatch: (action) => dispatchedActions.push(action),
            getState: () => mockState,
        }, PaymentRequestFormSaga.fetchStripeLocations).done;
    console.log('dispatchedActions', dispatchedActions);
    console.log('mRequest.mock:', mRequest.mock);
    expect(mRequest.mock.calls.length).toBe(1);

    expect(dispatchedActions).toEqual([ActionCreator.setStripeLocations(mockData)]);
});
/* test case ends*/

enter image description here enter image description here

1

There are 1 answers

0
dube On

toBe is like a reference check, but of course these two objects are not the same instance. toBe is rarely the right choice for objects

Use .toBe to compare primitive values or to check referential identity of object instances. It calls Object.is to compare values, which is even better for testing than === strict equality operator.

You can add jest-immutable-matchers to your project to compare immutable-js objects for value equality:

import * as matchers from 'jest-immutable-matchers';

describe('My suite', function () {
  beforeEach(function () {
    expect.extend(matchers);
  });

  it('passes if the immutable objects are equal', function () {
    expect(Immutable.Map({a: 1})).toEqualImmutable(Immutable.Map({a: 1}));
  });
});

If you want to test equality of two immutable objects yourself, use Immutable.is, e.g. Immutable.is(Immutable.Map({a: 1}), Immutable.Map({a: 1}))