|
| 1 | +import { |
| 2 | + deleteMineReportPermitRequirement, |
| 3 | + updateMineReportPermitRequirement, |
| 4 | + mineReportPermitRequirementReducer |
| 5 | +} from "./mineReportPermitRequirementSlice"; |
| 6 | +import CustomAxios from "@mds/common/redux/customAxios"; |
| 7 | +import { configureStore } from "@reduxjs/toolkit"; |
| 8 | + |
| 9 | +const showLoadingMock = jest |
| 10 | + .fn() |
| 11 | + .mockReturnValue({ type: "SHOW_LOADING", payload: { show: true } }); |
| 12 | +const hideLoadingMock = jest |
| 13 | + .fn() |
| 14 | + .mockReturnValue({ type: "HIDE_LOADING", payload: { show: false } }); |
| 15 | +const notificationSuccessMock = jest.fn(); |
| 16 | + |
| 17 | +jest.mock("@mds/common/redux/customAxios"); |
| 18 | +jest.mock("react-redux-loading-bar", () => ({ |
| 19 | + showLoading: () => showLoadingMock, |
| 20 | + hideLoading: () => hideLoadingMock, |
| 21 | +})); |
| 22 | + |
| 23 | +describe("mineReportPermitRequirementSlice", () => { |
| 24 | + let store; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + store = configureStore({ |
| 28 | + reducer: { |
| 29 | + mineReportPermitRequirement: mineReportPermitRequirementReducer, |
| 30 | + }, |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + jest.clearAllMocks(); |
| 36 | + }); |
| 37 | + |
| 38 | + describe("deleteMineReportPermitRequirement", () => { |
| 39 | + const mockResponse = { |
| 40 | + data: "", |
| 41 | + status: 204, |
| 42 | + }; |
| 43 | + |
| 44 | + it("should successfully delete a report requirement", async () => { |
| 45 | + (CustomAxios as jest.Mock).mockImplementation(() => ({ |
| 46 | + delete: jest.fn().mockResolvedValue(mockResponse), |
| 47 | + })); |
| 48 | + |
| 49 | + const payload = { |
| 50 | + mineGuid: "f13d66db-8096-4e4b-85b8-6e0d231549fb", |
| 51 | + mine_report_permit_requirement_id: 5, |
| 52 | + }; |
| 53 | + |
| 54 | + await store.dispatch(deleteMineReportPermitRequirement(payload)); |
| 55 | + |
| 56 | + // Verify loading state management |
| 57 | + expect(showLoadingMock).toHaveBeenCalledTimes(1); |
| 58 | + expect(hideLoadingMock).toHaveBeenCalledTimes(1); |
| 59 | + |
| 60 | + expect(CustomAxios).toHaveBeenCalledWith({ |
| 61 | + errorToastMessage: "default", |
| 62 | + successToastMessage: "Successfully deleted report requirement", |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should handle API error when deleting a report requirement", async () => { |
| 67 | + const error = new Error("API Error"); |
| 68 | + (CustomAxios as jest.Mock).mockImplementation(() => ({ |
| 69 | + delete: jest.fn().mockRejectedValue(error), |
| 70 | + })); |
| 71 | + |
| 72 | + const payload = { |
| 73 | + mineGuid: "f13d66db-8096-4e4b-85b8-6e0d231549fb", |
| 74 | + mine_report_permit_requirement_id: 5, |
| 75 | + }; |
| 76 | + |
| 77 | + await store.dispatch(deleteMineReportPermitRequirement(payload)); |
| 78 | + |
| 79 | + expect(notificationSuccessMock).not.toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe("updateMineReportPermitRequirement", () => { |
| 84 | + const mockResponse = { |
| 85 | + data: { |
| 86 | + report_name: null, |
| 87 | + mine_report_permit_requirement_id: 5, |
| 88 | + due_date_period_months: 6, |
| 89 | + initial_due_date: null, |
| 90 | + cim_or_cpo: "BOTH", |
| 91 | + ministry_recipient: ["RO"], |
| 92 | + permit_condition_id: 37815, |
| 93 | + }, |
| 94 | + }; |
| 95 | + |
| 96 | + it("should successfully update a report requirement", async () => { |
| 97 | + (CustomAxios as jest.Mock).mockImplementation(() => ({ |
| 98 | + put: jest.fn().mockResolvedValue(mockResponse), |
| 99 | + })); |
| 100 | + |
| 101 | + const payload = { |
| 102 | + mineGuid: "5b415b64-58af-434b-8636-960d98e0654f", |
| 103 | + values: { |
| 104 | + report_name: null, |
| 105 | + mine_report_permit_requirement_id: 5, |
| 106 | + due_date_period_months: 6, |
| 107 | + initial_due_date: null, |
| 108 | + cim_or_cpo: "BOTH", |
| 109 | + ministry_recipient: ["RO"], |
| 110 | + permit_condition_id: 37815, |
| 111 | + stepPath: "test", |
| 112 | + permit_amendment_id: 7551 |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + await store.dispatch(updateMineReportPermitRequirement(payload)); |
| 117 | + |
| 118 | + // Verify loading state management |
| 119 | + expect(showLoadingMock).toHaveBeenCalledTimes(1); |
| 120 | + expect(hideLoadingMock).toHaveBeenCalledTimes(1); |
| 121 | + |
| 122 | + expect(CustomAxios).toHaveBeenCalledWith({ |
| 123 | + errorToastMessage: "default", |
| 124 | + successToastMessage: "Successfully updated report requirement", |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should handle API error when updating a report requirement", async () => { |
| 129 | + const error = new Error("API Error"); |
| 130 | + (CustomAxios as jest.Mock).mockImplementation(() => ({ |
| 131 | + put: jest.fn().mockRejectedValue(error), |
| 132 | + })); |
| 133 | + |
| 134 | + const payload = { |
| 135 | + mineGuid: "5b415b64-58af-434b-8636-960d98e0654f", |
| 136 | + values: { |
| 137 | + report_name: null, |
| 138 | + mine_report_permit_requirement_id: 5, |
| 139 | + due_date_period_months: 6, |
| 140 | + initial_due_date: null, |
| 141 | + cim_or_cpo: "BOTH", |
| 142 | + ministry_recipient: ["RO"], |
| 143 | + permit_condition_id: 37815, |
| 144 | + stepPath: "test", |
| 145 | + permit_amendment_id: 7551 |
| 146 | + } |
| 147 | + }; |
| 148 | + |
| 149 | + await store.dispatch(updateMineReportPermitRequirement(payload)); |
| 150 | + |
| 151 | + expect(notificationSuccessMock).not.toHaveBeenCalled(); |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments