Redux模拟存储仅在调度多个操作时返回一个操作

loading...


0

我正试图模仿这个axios电话:

export const fetchCountry = (query) => {
  return dispatch => {
    dispatch(fetchCountryPending());
    return axios.get(`${process.env.REACT_APP_API_URL}/api/v1/countries/?search=${query}`)
      .then(response => {
        const country = response.data;
        dispatch(fetchCountryFulfilled(country));
      })
      .catch(err => {
        dispatch(fetchCountryRejected());
        dispatch({type: "ADD_ERROR", error: err});
      })
  }
}

在成功通话中,应该同时发送动作创建者fetchCountryPending()和fetchCountryFullfilled(country) . 当我嘲笑它时:

const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);

// Async action tests
describe('country async actions', () => {
  let store;
  let mock;

  beforeEach(function () {
    mock = new MockAdapter(axios)
    store = mockStore({ country: [], fetching: false, fetched: true })
  });

  afterEach(function () {
    mock.restore();
    store.clearActions();
  });

  it('dispatches FETCH_COUNTRY_FULFILLED after axios request', () => {
    const query = 'Aland Islands'
    mock.onGet(`${process.env.REACT_APP_API_URL}/api/v1/countries/?search=${query}`).replyOnce(200, country)
    store.dispatch(countryActions.fetchCountry(query))
    const actions = store.getActions()
    console.log(actions)
    expect(actions[0]).toEqual(countryActions.fetchCountryPending())
    expect(actions[1]).toEqual(countryActions.fetchCountryFulfilled(country))
  });
});

第二个期望失败,console.log(actions)仅显示具有一个操作的数组,但它应包含两个操作fetchCountryPending和fetchCountrySuccess . 当我记录('dispatched')时,它显示第二个动作正在终端中调度 .

loading...

1回答

  • 0

    你可以尝试让它阻止异步并发送动作 . 我相信测试在你的get请求返回值之前运行

评论

暂时没有评论!