Redux Saga 测试计划 - 集成测试无法运行

时间:2020-12-29 19:01:43

标签: javascript reactjs redux-saga redux-saga-test-plan

我在开始使用 redux-saga-test-plan 时遇到问题。我试图实现的目标是集成测试我的传奇。我正在使用 create-react-app,所以没有独特的设置。这是我唯一的测试,我无法让它运行。

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';

这是我的传奇:

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);
}

测试如下:

    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);

我希望它失败;但是,我什至无法开始。

我使用 react-scripts test (React 17)

当我的测试运行时,我得到 - 我真的不明白为什么 TYPES 未定义

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

以及

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:

我认为 API 永远不会被调用。我在这里做错了什么?我应该如何测试这个传奇?

0 个答案:

没有答案