Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

statsPageReducer.test.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import * as redux from "react-redux";
  2. import store from "../../store";
  3. import { Router } from "react-router-dom";
  4. import history from "../../store/utils/history";
  5. import { mockState } from "../../mockState";
  6. import { render } from "@testing-library/react";
  7. import StatsPage from "../../pages/StatsPage/StatsPage";
  8. import * as api from "../../request/statsRequests";
  9. import { runSaga } from "redux-saga";
  10. import {
  11. getStatsSuccess,
  12. getStatsError,
  13. } from "../../store/actions/stats/statsActions";
  14. import { FETCH_STATS_REQ } from "../../store/actions/stats/statsActionConstants";
  15. import { getAppStats } from "../../store/saga/statsSaga";
  16. import * as helper from "../../util/helpers/rejectErrorCodeHelper";
  17. describe("Stats reducer tests", () => {
  18. const cont = (
  19. <redux.Provider store={store}>
  20. <Router history={history}>
  21. <StatsPage />
  22. </Router>
  23. </redux.Provider>
  24. );
  25. let spyOnUseSelector;
  26. let spyOnUseDispatch;
  27. let mockDispatch;
  28. beforeEach(() => {
  29. // Mock useSelector hook
  30. spyOnUseSelector = jest.spyOn(redux, "useSelector");
  31. spyOnUseSelector.mockReturnValueOnce(mockState);
  32. // Mock useDispatch hook
  33. spyOnUseDispatch = jest.spyOn(redux, "useDispatch");
  34. // Mock dispatch function returned from useDispatch
  35. mockDispatch = jest.fn();
  36. spyOnUseDispatch.mockReturnValue(mockDispatch);
  37. });
  38. afterEach(() => {
  39. jest.restoreAllMocks();
  40. });
  41. it("Should dispatch get stats request when rendered", () => {
  42. render(cont);
  43. expect(mockDispatch).toHaveBeenCalledWith({
  44. type: FETCH_STATS_REQ,
  45. });
  46. });
  47. it("should load and handle stats in case of success", async () => {
  48. const dispatchedActions = [];
  49. const mockedCall = { data: mockState };
  50. api.getStats = jest.fn(() => Promise.resolve(mockedCall));
  51. const fakeStore = {
  52. getState: () => mockState,
  53. dispatch: (action) => dispatchedActions.push(action),
  54. };
  55. await runSaga(fakeStore, getAppStats).done;
  56. expect(api.getStats.mock.calls.length).toBe(1);
  57. expect(dispatchedActions).toContainEqual(getStatsSuccess(mockedCall.data));
  58. });
  59. it("should handle stats load errors in case of failure", async () => {
  60. const dispatchedActions = [];
  61. helper.rejectErrorCodeHelper = jest.fn(
  62. () => mockState.stats.fetchStatsErrorMessage
  63. );
  64. const error = {
  65. response: {
  66. data: { message: mockState.stats.fetchStatsErrorMessage },
  67. },
  68. };
  69. api.getStats = jest.fn(() => Promise.reject(error));
  70. const fakeStore = {
  71. getState: () => mockState,
  72. dispatch: (action) => dispatchedActions.push(action),
  73. };
  74. await runSaga(fakeStore, getAppStats).done;
  75. expect(api.getStats.mock.calls.length).toBe(1);
  76. expect(dispatchedActions).toContainEqual(
  77. getStatsError(error.response.data.message)
  78. );
  79. });
  80. });