| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566 |
- import { runSaga } from "redux-saga";
- import * as api from "../../request/processesReguest";
- import { render } from "@testing-library/react";
- import * as redux from "react-redux";
- import SelectionProcessPage from "../../pages/selectionProcessPage/selectionProcessPage";
- ("../../pages/SelectionProcessPage/SelectionProcessPage");
- import store from "../../store";
- import "../../i18n";
- import { mockState } from "../../mockState";
- import { FETCH_PROCESSES_REQ } from "../../store/actions/processes/processesActionConstants";
- import { Router } from "react-router-dom";
- import history from "../../store/utils/history";
- import {
- getProcesses,
- getFilteredProcesses,
- finishProcess,
- changeStatus,
- changeInterviewer,
- getApplicantProcesses,
- } from "../../store/saga/processSaga";
- import {
- setProcesses,
- setProcessesError,
- } from "../../store/actions/processes/processesAction";
- import { SelectionProvider } from "../../context/SelectionContext";
- import {
- setDoneProcess,
- setDoneProcessError,
- setUpdateInterviewerErr,
- setUpdateInterviewerSucc,
- setUpdateStatusErr,
- setUpdateStatusSucc,
- } from "../../store/actions/processes/processAction";
- import {
- setApplicant,
- setApplicantError,
- } from "../../store/actions/processes/applicantAction";
-
- describe("SelectionProcessPage render tests", () => {
- const cont = (
- <redux.Provider store={store}>
- <SelectionProvider>
- <Router history={history}>
- <SelectionProcessPage />
- </Router>
- </SelectionProvider>
- </redux.Provider>
- );
-
- let spyOnUseSelector;
- let spyOnUseDispatch;
- let mockDispatch;
-
- beforeEach(() => {
- // Mock useSelector hook
- spyOnUseSelector = jest.spyOn(redux, "useSelector");
- spyOnUseSelector
- .mockReturnValueOnce(mockState.selections)
- .mockReturnValueOnce(mockState.selections.processes)
- .mockReturnValueOnce(mockState.selections.statuses);
- // 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 processes request when rendered", () => {
- render(cont);
- expect(mockDispatch).toHaveBeenCalledWith({
- type: FETCH_PROCESSES_REQ,
- });
- });
-
- it("should load and handle levels with processes in case of success", async () => {
- // we push all dispatched actions to make assertions easier
- // and our tests less brittle
- const dispatchedActions = [];
-
- // we don't want to perform an actual api call in our tests
- // so we will mock the getAllUsers api with jest
- // this will mutate the dependency which we may reset if other tests
- // are dependent on it
- const mockedCall = { data: mockState.selections.processes };
- api.getAllLevels = jest.fn(() => Promise.resolve(mockedCall));
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- // wait for saga to complete
- await runSaga(fakeStore, getProcesses).done;
- expect(api.getAllLevels.mock.calls.length).toBe(1);
- expect(dispatchedActions).toContainEqual(setProcesses(mockedCall.data));
- });
-
- it("should handle processes 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.selections.fetchSelectionsErrorMessage },
- },
- };
- api.getAllLevels = jest.fn(() => Promise.reject(error));
-
- const fakeStore = {
- getState: () => mockState.users.users,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- await runSaga(fakeStore, getProcesses).done;
-
- expect(api.getAllLevels.mock.calls.length).toBe(1);
- expect(dispatchedActions).toContainEqual(
- setProcessesError(error.response.data.message)
- );
- });
-
- it("should load and handle levels with filtered processes in case of success", async () => {
- // we push all dispatched actions to make assertions easier
- // and our tests less brittle
- const dispatchedActions = [];
- const filter = {
- statuses: ["Zakazan", "Odrađen"],
- dateStart: new Date(2023, 0, 5),
- dateEnd: new Date(2024, 1, 1),
- };
- const filteredData = [];
- mockState.selections.processes.forEach((level) => {
- const filteredLevel = level;
- filteredLevel.selectionProcesses = level.selectionProcesses.filter(
- (v) =>
- v.date >= filter.dateStart &&
- v.date <= filter.dateEnd &&
- filter.statuses.includes(v.status)
- );
- filteredData.push(filteredLevel);
- });
- // we don't want to perform an actual api call in our tests
- // so we will mock the getAllUsers api with jest
- // this will mutate the dependency which we may reset if other tests
- // are dependent on it
- const mockedCall = { data: filteredData };
- api.getAllFilteredProcessesReq = jest.fn(() => Promise.resolve(mockedCall));
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- // wait for saga to complete
- await runSaga(fakeStore, getFilteredProcesses, filter).done;
- expect(api.getAllFilteredProcessesReq.mock.calls.length).toBe(1);
- expect(dispatchedActions).toContainEqual(setProcesses(filteredData));
- });
-
- it("should handle error in case of exception", async () => {
- const dispatchedActions = [];
-
- const filter = {
- statuses: ["Zakazan", "Odrađen"],
- dateStart: new Date(2023, 0, 5),
- dateEnd: new Date(2024, 1, 1),
- };
-
- const error = {
- response: {
- data: { message: mockState.selections.fetchSelectionsErrorMessage },
- },
- };
- api.getAllFilteredProcessesReq = jest.fn(() => Promise.reject(error));
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- // wait for saga to complete
- await runSaga(fakeStore, getFilteredProcesses, filter).done;
- expect(dispatchedActions).toContainEqual(
- setProcessesError(error.response.data.message)
- );
- });
-
- it("should handle process to set it done in case of success", async () => {
- // we push all dispatched actions to make assertions easier
- // and our tests less brittle
- const dispatchedActions = [];
- const filter = {
- statuses: ["Zakazan", "Odrađen"],
- dateStart: new Date(2023, 0, 5),
- dateEnd: new Date(2024, 1, 1),
- };
- const filteredData = [];
- mockState.selections.processes.forEach((level) => {
- const filteredLevel = level;
- filteredLevel.selectionProcesses = level.selectionProcesses.filter(
- (v) =>
- v.date >= filter.dateStart &&
- v.date <= filter.dateEnd &&
- filter.statuses.includes(v.status)
- );
- filteredData.push(filteredLevel);
- });
- // we don't want to perform an actual api call in our tests
- // so we will mock the getAllUsers api with jest
- // this will mutate the dependency which we may reset if other tests
- // are dependent on it
- const mockedCall = { data: filteredData };
- api.getAllFilteredProcessesReq = jest.fn(() => Promise.resolve(mockedCall));
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- // wait for saga to complete
- await runSaga(fakeStore, getFilteredProcesses, filter).done;
- expect(api.getAllFilteredProcessesReq.mock.calls.length).toBe(1);
- expect(dispatchedActions).toContainEqual(setProcesses(filteredData));
- });
-
- it("should handle process to set it done in case of finish success", async () => {
- const dispatchedActions = [];
- const mockedCall = { data: { isSuccess: true } };
- api.doneProcess = jest.fn(() => Promise.resolve(mockedCall));
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- // wait for saga to complete
- await runSaga(fakeStore, finishProcess, {
- payload: {
- id: 1,
- name: "Some random name",
- applicantId: 5,
- schedulerId: 10,
- },
- }).done;
-
- expect(api.doneProcess.mock.calls.length).toBe(1);
- expect(dispatchedActions).toContainEqual(setDoneProcess(mockedCall.data));
- });
-
- it("should handle error in case of finish process exception", async () => {
- const dispatchedActions = [];
-
- const error = {
- response: {
- data: { message: mockState.selections.fetchSelectionsErrorMessage },
- },
- };
- api.doneProcess = jest.fn(() => Promise.reject(error));
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- // wait for saga to complete
- await runSaga(fakeStore, finishProcess, {
- payload: {
- id: 1,
- name: "Some random name",
- applicantId: 5,
- schedulerId: 10,
- },
- }).done;
-
- expect(api.doneProcess.mock.calls.length).toBe(1);
- expect(dispatchedActions).toContainEqual(
- setDoneProcessError(error.response.data.message)
- );
- });
-
- it("should handle process status update", async () => {
- const dispatchedActions = [];
- const mockedCall = { data: { isSuccess: true } };
- api.updateStatus = jest.fn(() => Promise.resolve(mockedCall));
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- // wait for saga to complete
- await runSaga(fakeStore, changeStatus, {
- payload: {
- data: {
- schedulerId: 3,
- appointment: "22-10-2023",
- newStatus: "Odrađen",
- processId: 1,
- },
- },
- }).done;
-
- expect(api.updateStatus.mock.calls.length).toBe(1);
- expect(dispatchedActions).toContainEqual(setUpdateStatusSucc());
- });
-
- it("should call responsehandler while handling process status update", async () => {
- const dispatchedActions = [];
- const mockedCall = { data: { isSuccess: true } };
- api.updateStatus = jest.fn(() => Promise.resolve(mockedCall));
-
- const mockfn = jest.fn();
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- // wait for saga to complete
- await runSaga(fakeStore, changeStatus, {
- payload: {
- data: {
- schedulerId: 3,
- appointment: "22-10-2023",
- newStatus: "Odrađen",
- processId: 1,
- },
- responseHandler: mockfn,
- },
- }).done;
-
- expect(mockfn).toHaveBeenCalled();
- });
-
- it("should handle process status update error if exception is thrown", async () => {
- const dispatchedActions = [];
-
- const error = {
- response: {
- data: { message: mockState.selections.fetchSelectionsErrorMessage },
- },
- };
-
- api.updateStatus = jest.fn(() => Promise.reject(error));
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- // wait for saga to complete
- await runSaga(fakeStore, changeStatus, {
- payload: {
- data: {
- schedulerId: 3,
- appointment: "22-10-2023",
- newStatus: "Odrađen",
- processId: 1,
- },
- },
- }).done;
-
- expect(api.updateStatus.mock.calls.length).toBe(1);
- expect(dispatchedActions).toContainEqual(
- setUpdateStatusErr(error.response.data.message)
- );
- });
-
- it("should not call responseHandler if exception is thrown", async () => {
- const dispatchedActions = [];
- const error = {
- response: {
- data: { message: mockState.selections.fetchSelectionsErrorMessage },
- },
- };
- api.updateStatus = jest.fn(() => Promise.reject(error));
-
- const mockfn = jest.fn();
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- // wait for saga to complete
- await runSaga(fakeStore, changeStatus, {
- payload: {
- data: {
- schedulerId: 3,
- appointment: "22-10-2023",
- newStatus: "Odrađen",
- processId: 1,
- },
- responseHandler: mockfn,
- },
- }).done;
-
- expect(mockfn).not.toHaveBeenCalled();
- });
-
- it("should handle interviewer update", async () => {
- const dispatchedActions = [];
- const mockedCall = { data: { isSuccess: true } };
- api.updateInterviewer = jest.fn(() => Promise.resolve(mockedCall));
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- // wait for saga to complete
- await runSaga(fakeStore, changeInterviewer, {
- payload: {
- data: {
- schedulerId: 1,
- processId: 2,
- },
- // responseHandler: apiSuccess,
- },
- }).done;
-
- expect(api.updateInterviewer.mock.calls.length).toBe(1);
- expect(dispatchedActions).toContainEqual(setUpdateInterviewerSucc());
- });
-
- it("should call response handler on change success", async () => {
- const dispatchedActions = [];
- const mockedCall = { data: { isSuccess: true } };
- api.updateInterviewer = jest.fn(() => Promise.resolve(mockedCall));
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- const mockfn = jest.fn();
-
- // wait for saga to complete
- await runSaga(fakeStore, changeInterviewer, {
- payload: {
- data: {
- schedulerId: 1,
- processId: 2,
- },
- responseHandler: mockfn,
- },
- }).done;
-
- expect(mockfn).toHaveBeenCalled();
- });
-
- it("should handle interviewer update error if exception is thrown", async () => {
- const dispatchedActions = [];
- const error = {
- response: {
- data: { message: mockState.selections.fetchSelectionsErrorMessage },
- },
- };
- api.updateInterviewer = jest.fn(() => Promise.reject(error));
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- // wait for saga to complete
- await runSaga(fakeStore, changeInterviewer, {
- payload: {
- data: {
- schedulerId: 1,
- processId: 2,
- },
- // responseHandler: mockfn,
- },
- }).done;
-
- expect(api.updateInterviewer.mock.calls.length).toBe(1);
- expect(dispatchedActions).toContainEqual(
- setUpdateInterviewerErr(error.response.data.message)
- );
- });
-
- it("should not call response handler if exception is thrown", async () => {
- const dispatchedActions = [];
- const error = {
- response: {
- data: { message: mockState.selections.fetchSelectionsErrorMessage },
- },
- };
- api.updateInterviewer = jest.fn(() => Promise.reject(error));
-
- const mockfn = jest.fn();
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- // wait for saga to complete
- await runSaga(fakeStore, changeInterviewer, {
- payload: {
- data: {
- schedulerId: 1,
- processId: 2,
- },
- responseHandler: mockfn,
- },
- }).done;
-
- expect(mockfn).not.toHaveBeenCalled();
- });
-
- it("should handle applicant processess if succeeded", async () => {
- const dispatchedActions = [];
- const mockedCall = { data: {} };
- api.getProcessesOfApplicant = jest.fn(() => Promise.resolve(mockedCall));
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- // wait for saga to complete
- await runSaga(fakeStore, getApplicantProcesses, {
- payload: {
- id: 1,
- },
- }).done;
-
- expect(api.getProcessesOfApplicant.mock.calls.length).toBe(1);
- expect(dispatchedActions).toContainEqual(setApplicant(mockedCall.data));
- });
-
- it("should handle applicant processess error if exception is thrown", async () => {
- const dispatchedActions = [];
- const error = {
- response: {
- data: { message: mockState.selections.fetchSelectionsErrorMessage },
- },
- };
- api.getProcessesOfApplicant = jest.fn(() => Promise.reject(error));
-
- const fakeStore = {
- getState: () => mockState.selections.processes,
- dispatch: (action) => dispatchedActions.push(action),
- };
-
- // wait for saga to complete
- await runSaga(fakeStore, getApplicantProcesses, {
- payload: {
- id: 1,
- },
- }).done;
-
- expect(api.getProcessesOfApplicant.mock.calls.length).toBe(1);
- expect(dispatchedActions).toContainEqual(
- setApplicantError(error.response.data.message)
- );
- });
- });
|