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*/


toBeis like a reference check, but of course these two objects are not the same instance.toBeis rarely the right choice for objectsYou can add
jest-immutable-matchersto your project to compare immutable-js objects for value equality: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}))