3

I've been writing some redux-saga tests using redux-saga-test-plan but having some issues when trying to provide some selects to fake some values to the expectSaga test. It seems as though the provider isn't replacing our reselect function call with our value but actually trying to run the reselect function. The project is using the react-boilerplate template.

I have already tried different variants of faking the selectors by using matchers instead but still run into the same problem.

Here is the saga I want to test:

export function* categoriseA() {
  const sku = yield select(makeSelectSKU());
  const { ql, sscc } = yield select(makeSelectItemData());
  const site = yield select(makeSelectSite());

  const params = {
    url: `refurbishment/api/sites/${site}/refurbishments`,
    method: 'put',
    data: {
      quality_label: ql,
      classification_type: 'A_GOODS',
      ...(sku && { sku }), // a way of conditionally creating a property
      ...(sscc && { sscc }), // a way of conditionally creating a property
    },
  };
  yield put({ type: PUT_A_GOODS_REQUEST, ...params });
  yield put(push(SCAN_BARCODE_URL));
}

Using this test:

it('it pushes SCAN_BARCODE_URL and create a PUT_A_GOODS_REQUEST', () => {
  expectSaga(saga)
    .provide([
      [select(selectors.makeSelectSKU()), 'sku'],
      [select(selectors.makeSelectItemData()), mockItemData],
      [select(selectors.makeSelectSite()), 'site'],
    ])
    .put(push(SCAN_BARCODE_URL))
    .dispatch(chooseACategory())
    .run();
});

The error I get is: It looks as though the reselect selector function is running and trying to access the global state from the reducer which I thought would be faked with the provider?

 TypeError: Cannot read property 'global' of undefined
          at selectGlobal (/Users/ccc/development/workspace/grip/refurb-frontend/app/containers/App/selectors.js:945:38)
          at /Users/ccc/development/workspace/grip/refurb-frontend/node_modules/reselect/lib/index.js:87:37
          at /Users/ccc/development/workspace/grip/refurb-frontend/node_modules/reselect/lib/index.js:39:25
          at runSelectEffect (/Users/ccc/development/workspace/grip/refurb-frontend/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:761:26)
          at runEffect (/Users/amurray/development/workspace/grip/refurb-frontend/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1234:7)
          at digestEffect (/Users/amurray/development/workspace/grip/refurb-frontend/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1301:5)
          at next (/Users/ccc/development/workspace/grip/refurb-frontend/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1191:9)
          at proc (/Users/ccc/development/workspace/grip/refurb-frontend/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:1138:3)
          at /Users/ccc/development/workspace/grip/refurb-frontend/node_modules/@redux-saga/core/dist/redux-saga-core.dev.cjs.js:614:17
          at immediately ...
LocalMagic
  • 107
  • 1
  • 7

1 Answers1

0

You should not call the selector you are trying to mock. Instead of

select(selectors.makeSelectSKU()), 'sku']

you should omit the brackets and do:

select(selectors.makeSelectSKU), 'sku']
Andrii Rudavko
  • 370
  • 1
  • 7