您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

userManagementReducer.test.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 UsersPage from "../../pages/UsersPage/UsersPage";
  8. import * as api from "../../request/usersRequest";
  9. import { runSaga } from "redux-saga";
  10. import { FETCH_USERS_REQ } from "../../store/actions/users/usersActionConstants";
  11. import { setUsersError } from "../../store/actions/users/usersActions";
  12. import { getUsers } from "../../store/saga/usersSaga";
  13. describe("Stats reducer tests", () => {
  14. const cont = (
  15. <redux.Provider store={store}>
  16. <Router history={history}>
  17. <UsersPage />
  18. </Router>
  19. </redux.Provider>
  20. );
  21. let spyOnUseSelector;
  22. let spyOnUseDispatch;
  23. let mockDispatch;
  24. beforeEach(() => {
  25. // Mock useSelector hook
  26. spyOnUseSelector = jest.spyOn(redux, "useSelector");
  27. spyOnUseSelector.mockReturnValueOnce(mockState.users);
  28. // Mock useDispatch hook
  29. spyOnUseDispatch = jest.spyOn(redux, "useDispatch");
  30. // Mock dispatch function returned from useDispatch
  31. mockDispatch = jest.fn();
  32. spyOnUseDispatch.mockReturnValue(mockDispatch);
  33. });
  34. afterEach(() => {
  35. jest.restoreAllMocks();
  36. });
  37. it("Should dispatch get users request when rendered", () => {
  38. render(cont);
  39. expect(mockDispatch).toHaveBeenCalledWith({
  40. type: FETCH_USERS_REQ,
  41. });
  42. });
  43. // it('should handle users load errors in case of failure', async () => {
  44. // const dispatchedActions = [];
  45. // // we simulate an error by rejecting the promise
  46. // // then we assert if our saga dispatched the action(s) correctly
  47. // const error = { response: { data: { message: mockState.users.errorMessage } } };
  48. // api.getAllUsers = jest.fn(() => Promise.reject(error));
  49. // const fakeStore = {
  50. // getState: () => (mockState.users.users),
  51. // dispatch: action => dispatchedActions.push(action),
  52. // };
  53. // await runSaga(fakeStore, getUsers).done;
  54. // expect(api.getAllUsers.mock.calls.length).toBe(1);
  55. // expect(dispatchedActions).toContainEqual(setUsersError(error.response.data.message));
  56. // });
  57. });