Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

adsReducer.test.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 AdsPage from "../../pages/AdsPage/AdsPage";
  8. import * as api from "../../request/adsRequest";
  9. import { runSaga } from "redux-saga";
  10. import { FETCH_ADS_REQ } from "../../store/actions/ads/adsActionConstants";
  11. import ColorModeProvider from "../../context/ColorModeContext";
  12. import * as fc from "../../store/saga/adsSaga";
  13. import { setAds, setFilteredAds } from "../../store/actions/ads/adsAction";
  14. import { setArchiveAds } from "../../store/actions/archiveAds/archiveAdsActions";
  15. import { setAd } from "../../store/actions/ad/adActions";
  16. describe("Ads reducer tests", () => {
  17. const cont = (
  18. <redux.Provider store={store}>
  19. <Router history={history}>
  20. <ColorModeProvider>
  21. <AdsPage />
  22. </ColorModeProvider>
  23. </Router>
  24. </redux.Provider>
  25. );
  26. let spyOnUseSelector;
  27. let spyOnUseDispatch;
  28. let mockDispatch;
  29. beforeEach(() => {
  30. spyOnUseSelector = jest.spyOn(redux, "useSelector");
  31. spyOnUseSelector.mockReturnValueOnce(mockState.ads);
  32. spyOnUseDispatch = jest.spyOn(redux, "useDispatch");
  33. mockDispatch = jest.fn();
  34. spyOnUseDispatch.mockReturnValue(mockDispatch);
  35. });
  36. afterEach(() => {
  37. jest.restoreAllMocks();
  38. });
  39. it("Should dispatch get ads request when rendered", () => {
  40. render(cont);
  41. expect(mockDispatch).toHaveBeenCalledWith({
  42. type: FETCH_ADS_REQ,
  43. });
  44. });
  45. it("Should load and handle ads in case of success", async () => {
  46. const dispatchedActions = [];
  47. const mockedCall = { data: mockState.ads.ads };
  48. api.getAllAds = jest.fn(() => Promise.resolve(mockedCall));
  49. const fakeStore = {
  50. getState: () => mockState.ads.ads,
  51. dispatch: (action) => dispatchedActions.push(action),
  52. };
  53. await runSaga(fakeStore, fc.getAds, {}).done;
  54. expect(api.getAllAds.mock.calls.length).toBe(1);
  55. expect(dispatchedActions).toContainEqual(setAds(mockedCall.data));
  56. });
  57. it("Should load and handle filtered ads in case of success", async () => {
  58. const dispatchedActions = [];
  59. const filter = {
  60. minimumExperience: 0,
  61. maximumExperience: 0,
  62. technologies: [1],
  63. workHour: "FullTime",
  64. employmentType: "Work",
  65. };
  66. const filteredData = mockState.ads.ads.filter(
  67. (ad) =>
  68. ad.minimumExperience >= filter.minimumExperience &&
  69. ad.minimumExperience <= filter.maximumExperience &&
  70. ad.workHour === filter.workHour &&
  71. ad.employmentType === filter.employmentType
  72. );
  73. const mockedCall = { data: filteredData };
  74. api.getAllFilteredAds = jest.fn(() => Promise.resolve(mockedCall));
  75. const fakeStore = {
  76. getState: () => mockState.ads.ads,
  77. dispatch: (action) => dispatchedActions.push(action),
  78. };
  79. await runSaga(fakeStore, fc.getFilteredAds, filter).done;
  80. expect(api.getAllFilteredAds.mock.calls.length).toBe(1);
  81. expect(dispatchedActions).toContainEqual(setFilteredAds(mockedCall.data));
  82. });
  83. it("Should load and handle archived ads in case of success", async () => {
  84. const dispatchedActions = [];
  85. const date = new Date();
  86. const filteredData = mockState.ads.ads.filter((ad) => ad.expiredAt < date);
  87. const mockedCall = { data: filteredData };
  88. api.getAllArchiveAds = jest.fn(() => Promise.resolve(mockedCall));
  89. const fakeStore = {
  90. getState: () => mockState.ads.ads,
  91. dispatch: (action) => dispatchedActions.push(action),
  92. };
  93. await runSaga(fakeStore, fc.getArchiveAds, {}).done;
  94. expect(api.getAllArchiveAds.mock.calls.length).toBe(1);
  95. expect(dispatchedActions).toContainEqual(setArchiveAds(mockedCall.data));
  96. });
  97. it("Should load and handle ad by id in case of success", async () => {
  98. const dispatchedActions = [];
  99. const id = 1;
  100. const filteredData = mockState.ads.ads.filter((ad) => ad.id === id);
  101. const mockedCall = { data: filteredData };
  102. api.getAdDetailsById = jest.fn(() => Promise.resolve(mockedCall));
  103. const fakeStore = {
  104. getState: () => mockState.ads.ads,
  105. dispatch: (action) => dispatchedActions.push(action),
  106. };
  107. await runSaga(fakeStore, fc.getAd, { payload: id }).done;
  108. expect(api.getAdDetailsById.mock.calls.length).toBe(1);
  109. expect(dispatchedActions).toContainEqual(setAd(mockedCall.data));
  110. });
  111. });