Тестирование Redux-Thunk с запросом Axios - PullRequest
0 голосов
/ 08 октября 2018

Я пытаюсь проверить следующее действие, имитирующее успешный поток:

export const search = searchTerm => async dispatch => {
  dispatch(searchRequest(searchTerm));
  try {
    const searchResults = await getSearchData(searchTerm);
    dispatch(searchSuccess(searchResults));
    dispatch(searchComplete());
  } catch (e) {
    dispatch(searchFail(e));
    dispatch(searchComplete());
  }
};

getSearchData:

export const getSearchData = async searchTerm => {
  const {
    data: {
      data: { results: booksFromSearch },
    },
  } = await get(`/search?title=${searchTerm}`);

  return booksFromSearch;
}; 

Конечная точка URL-адреса соответствует http://localhost:8080/search?title=Great (например).

Ниже приведен мой тестовый пример, я высмеиваю объект ответа:

const mock = new MockAdapter(axios);
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
const store = mockStore({});

it("should fire a successful search", () => {
    const response = [{ id: 1 }, { id: 2 }];
    mock.onGet("http://localhost:8080/search?title=Great").reply(200, response);
    store.dispatch(search("Great"));
    expect(store.getActions()).toEqual([
      { type: SEARCH_REQUEST, payload: { searchTerm: "Great" } },
      { type: SEARCH_SUCCESS },
      { type: SEARCH_COMPLETE },
    ]);
  });

Однако, когда я запускаю тестовый пример, я вижу только запуск SEARCH_REQUEST.

Expected value to equal:
      [{"payload": {"searchTerm": "Great"}, "type": "SEARCH_REQUEST"}, {"type": "SEARCH_SUCCESS"}, {"type": "SEARCH_COMPLETE"}]
    Received:
      [{"payload": {"searchTerm": "Great"}, "type": "SEARCH_REQUEST"}]
...