Chasen Bettinger
Chasen Bettinger

Reputation: 7454

Redux Saga Test Plan - Integration Tests Fail To Run

I am having trouble getting started with redux-saga-test-plan. The goal that I'm trying to achieve is integration testing my sagas. I'm using create-react-app, so no unique setup. This is my only test and I cannot get it to run.

CatActions

import * as types from './CatTypes';

//Replace action name and update action types
export const getCats = () => ({
  type: types.GET_CATS,
});

export const setCats = payload => ({
  type: types.SET_CATS,
  payload,
});

CatTypes

export const GET_CATS = 'GET_CATS';
export const SET_CATS = 'SET_CATS';

Here is my saga:

import { call, put, takeLatest } from 'redux-saga/effects';
import API from './CatApis';
import * as ACTIONS from './CatAction';
import { dispatchSnackbarError } from '../../utils/Shared';
import * as TYPES from './CatTypes';

export function* sagasRequestExample() {
  try {
    console.log('callin!');
    const response = yield call(API.apiExampleRequest);

    console.log('response :>> ', response);
    yield put(ACTIONS.setCats(response));
  } catch (err) {
    dispatchSnackbarError(err.response.data);
  }
}

export function* GetCats() {
  console.log('TYPES :>> ', TYPES);
  // On the first call here, TYPES is undefined - why?
  yield takeLatest('GET_CATS', sagasRequestExample);
  // yield takeLatest(TYPES.GET_CATS, sagasRequestExample);
}

Here is the test:

    import { expectSaga } from 'redux-saga-test-plan';
import { GetCats } from './CatSagas';

expectSaga.DEFAULT_TIMEOUT = 10000;

test('just works!', () => {
  return (
    expectSaga(GetCats)
      .take('GET_CATS')
      .put({
        type: 'RECEIVE_USER',
        payload: { id: 42, name: 'Tucker' },
      })

      // // Dispatch any actions that the saga will `take`.
      // .dispatch({ type: 'REQUEST_USER', payload: 42 })

      // Start the test. Returns a Promise.
      .run()
  );
}, 10000);

I expect it to fail; but, I can't even get started.

I use react-scripts test (React 17)

When my tests run, I get - I really don't understand why TYPES is undefined

console.log src/store/Cats/CatSagas.js:21
    TYPES :>>  undefined

as well as

Timeout - Async callback was not invoked within the 10000ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 10000ms timeout specified by jest.setTimeout.Error:

I don't think the API ever gets called. What am I doing wrong here? How am I supposed to be testing this saga?

Upvotes: 1

Views: 434

Answers (0)

Related Questions