You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

registerSagaTest.test.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 * as api from '../../request/registerRequest'
  7. import { render } from "@testing-library/react";
  8. import UserDetails from "../../pages/UsersPage/UserDetails";
  9. import ColorModeProvider from "../../context/ColorModeContext";
  10. import {
  11. USER_DETAILS_REQ,
  12. } from "../../store/actions/users/usersActionConstants";
  13. import { registerSuccess } from "../../store/actions/register/registerActions";
  14. import { runSaga } from "redux-saga";
  15. import { userDetails } from "../../store/saga/usersSaga";
  16. import { registerUser } from "../../store/saga/registerSaga";
  17. describe("UserDetails reducer tests", () => {
  18. let spyOnUseSelector;
  19. let spyOnUseDispatch;
  20. let mockDispatch;
  21. beforeEach(() => {
  22. // Mock useSelector hook
  23. spyOnUseSelector = jest.spyOn(redux, "useSelector");
  24. spyOnUseSelector.mockReturnValueOnce(mockState.user.user);
  25. // Mock useDispatch hook
  26. spyOnUseDispatch = jest.spyOn(redux, "useDispatch");
  27. // Mock dispatch function returned from useDispatch
  28. mockDispatch = jest.fn();
  29. spyOnUseDispatch.mockReturnValue(mockDispatch);
  30. });
  31. afterEach(() => {
  32. jest.restoreAllMocks();
  33. });
  34. it("should run register saga function with actions", async () => {
  35. const dispatchedActions = [];
  36. api.register = jest.fn(() => Promise.resolve());
  37. const fakeStore = {
  38. getState: () => ({
  39. isSuccess: false,
  40. errorMessage: "",
  41. }),
  42. dispatch: (action) => dispatchedActions.push(action),
  43. };
  44. await runSaga(fakeStore, registerUser, {
  45. payload: {
  46. password:'',
  47. email:'',
  48. confirm:'',
  49. position:'',
  50. linkedIn:'',
  51. phone:'',
  52. token:'',
  53. },
  54. }).done;
  55. expect(api.register.mock.calls.length).toBe(1);
  56. expect(dispatchedActions).toContainEqual(registerSuccess());
  57. });
  58. });