| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import * as redux from "react-redux";
- import store from "../../store";
- import { Router } from "react-router-dom";
- import history from "../../store/utils/history";
- import { mockState } from "../../mockState";
- import { render } from "@testing-library/react";
- import UsersPage from "../../pages/UsersPage/UsersPage";
- import * as api from "../../request/usersRequest";
- import { runSaga } from "redux-saga";
- import { FETCH_USERS_REQ } from "../../store/actions/users/usersActionConstants";
- import { setUsersError } from "../../store/actions/users/usersActions";
- import { getUsers } from "../../store/saga/usersSaga";
-
- describe("Stats reducer tests", () => {
- const cont = (
- <redux.Provider store={store}>
- <Router history={history}>
- <UsersPage />
- </Router>
- </redux.Provider>
- );
-
- let spyOnUseSelector;
- let spyOnUseDispatch;
- let mockDispatch;
-
- beforeEach(() => {
- // Mock useSelector hook
- spyOnUseSelector = jest.spyOn(redux, "useSelector");
- spyOnUseSelector.mockReturnValueOnce(mockState.users);
-
- // Mock useDispatch hook
- spyOnUseDispatch = jest.spyOn(redux, "useDispatch");
-
- // Mock dispatch function returned from useDispatch
- mockDispatch = jest.fn();
- spyOnUseDispatch.mockReturnValue(mockDispatch);
- });
-
- afterEach(() => {
- jest.restoreAllMocks();
- });
-
- it("Should dispatch get users request when rendered", () => {
- render(cont);
- expect(mockDispatch).toHaveBeenCalledWith({
- type: FETCH_USERS_REQ,
- });
- });
-
- // it('should handle users load errors in case of failure', async () => {
- // const dispatchedActions = [];
-
- // // we simulate an error by rejecting the promise
- // // then we assert if our saga dispatched the action(s) correctly
- // const error = { response: { data: { message: mockState.users.errorMessage } } };
- // api.getAllUsers = jest.fn(() => Promise.reject(error));
-
- // const fakeStore = {
- // getState: () => (mockState.users.users),
- // dispatch: action => dispatchedActions.push(action),
- // };
-
- // await runSaga(fakeStore, getUsers).done;
-
- // expect(api.getAllUsers.mock.calls.length).toBe(1);
- // expect(dispatchedActions).toContainEqual(setUsersError(error.response.data.message));
- // });
- });
|