text: import MockAdapter from "axios-mock-adapter";
  import axios from "axios";
  import {
    createMineIncident,
    updateMineIncident,
    fetchMineIncidents,
    fetchIncidents,
    deleteMineIncident,
  } from "@common/actionCreators/incidentActionCreator";
  import * as genericActions from "@common/actions/genericActions";
  import { ENVIRONMENT } from "@common/constants/environment";
  import * as API from "@common/constants/API";
  import * as MOCK from "@/tests/mocks/dataMocks";
  
  const dispatch = jest.fn();
  const requestSpy = jest.spyOn(genericActions, "request");
  const successSpy = jest.spyOn(genericActions, "success");
  const errorSpy = jest.spyOn(genericActions, "error");
  const mockAxios = new MockAdapter(axios);
  
  beforeEach(() => {
    mockAxios.reset();
    dispatch.mockClear();
    requestSpy.mockClear();
    successSpy.mockClear();
    errorSpy.mockClear();
  });
  
  describe("`createMineIncident` action creator", () => {
    const mineGuid = "12345-6789";
    const url = `${ENVIRONMENT.apiUrl}${API.MINE_INCIDENTS(mineGuid)}`;
    const mockPayload = {
      incident_timestamp: "2001-01-01 12:12",
      incident_description: "bad things happened",
    };
    it("Request successful, dispatches `success` with correct response", () => {
      const mockResponse = { data: { success: true } };
      mockAxios.onPost(url, mockPayload).reply(200, mockResponse);
      return createMineIncident(
        mineGuid,
        mockPayload
      )(dispatch).then(() => {
        expect(requestSpy).toHaveBeenCalledTimes(1);
        expect(successSpy).toHaveBeenCalledTimes(1);
        expect(dispatch).toHaveBeenCalledTimes(4);
      });
    });
  
    it("Request failure, dispatches `error` with correct response", () => {
      mockAxios.onPost(url).reply(418, MOCK.ERROR);
      return createMineIncident(mineGuid)(dispatch).catch(() => {
        expect(requestSpy).toHaveBeenCalledTimes(1);
        expect(errorSpy).toHaveBeenCalledTimes(1);
        expect(dispatch).toHaveBeenCalledTimes(4);
      });
    });
  });
  
  describe("`fetchMineIncidents` action creator", () => {
    const mineGuid = "12345-6789";
    const url = `${ENVIRONMENT.apiUrl}${API.MINE_INCIDENTS(mineGuid)}`;
  
    it("Request successful, dispatches `success` with correct response", () => {
      const mockResponse = { data: { success: true } };
      mockAxios.onGet(url).reply(200, mockResponse);
      return fetchMineIncidents(mineGuid)(dispatch).then(() => {
        expect(requestSpy).toHaveBeenCalledTimes(1);
        expect(successSpy).toHaveBeenCalledTimes(1);
        expect(dispatch).toHaveBeenCalledTimes(5);
      });
    });
  
    it("Request failure, dispatches `error` with correct response", () => {
      mockAxios.onGet(url, MOCK.createMockHeader()).reply(418, MOCK.ERROR);
      return fetchMineIncidents(mineGuid)(dispatch).then(() => {
        expect(requestSpy).toHaveBeenCalledTimes(1);
        expect(errorSpy).toHaveBeenCalledTimes(1);
        expect(dispatch).toHaveBeenCalledTimes(4);
      });
    });
  });
  
  describe("`updateMineIncident` action creator", () => {
    const mineGuid = "12345-6789";
    const mineIncidentGUID = "9876-54321";
    const url = `${ENVIRONMENT.apiUrl}${API.MINE_INCIDENT(mineGuid, mineIncidentGUID)}`;
    const mockPayload = {
      incident_timestamp: "2001-01-01 12:12",
      incident_description: "bad things happened",
    };
    it("Request successful, dispatches `success` with correct response", () => {
      const mockResponse = { data: { success: true } };
      mockAxios.onPut(url, mockPayload).reply(200, mockResponse);
      return updateMineIncident(
        mineGuid,
        mineIncidentGUID,
        mockPayload
      )(dispatch).then(() => {
        expect(requestSpy).toHaveBeenCalledTimes(1);
        expect(successSpy).toHaveBeenCalledTimes(1);
        expect(dispatch).toHaveBeenCalledTimes(4);
      });
    });
  
    it("Request failure, dispatches `error` with correct response", () => {
      mockAxios.onPut(url).reply(418, MOCK.ERROR);
      return updateMineIncident(
        mineGuid,
        mineIncidentGUID,
        mockPayload
      )(dispatch).catch(() => {
        expect(requestSpy).toHaveBeenCalledTimes(1);
        expect(errorSpy).toHaveBeenCalledTimes(1);
        expect(dispatch).toHaveBeenCalledTimes(4);
      });
    });
  });
  
  describe("`fetchIncidents` action creator", () => {
    const payload = "";
    const url = `${ENVIRONMENT.apiUrl}${API.INCIDENTS(payload)}`;
  
    it("Request successful, dispatches `success` with correct response", () => {
      const mockResponse = { data: { success: true } };
      mockAxios.onGet(url).reply(200, mockResponse);
      return fetchIncidents(payload)(dispatch).then(() => {
        expect(requestSpy).toHaveBeenCalledTimes(1);
        expect(successSpy).toHaveBeenCalledTimes(1);
        expect(dispatch).toHaveBeenCalledTimes(5);
      });
    });
  
    it("Request failure, dispatches `error` with correct response", () => {
      mockAxios.onGet(url, MOCK.createMockHeader()).reply(418, MOCK.ERROR);
      return fetchIncidents(payload)(dispatch).then(() => {
        expect(requestSpy).toHaveBeenCalledTimes(1);
        expect(errorSpy).toHaveBeenCalledTimes(1);
        expect(dispatch).toHaveBeenCalledTimes(4);
      });
    });
  });
  
  describe("`deleteMineIncident` action creator", () => {
    const mineGuid = "12345-6789";
    const mineIncidentGUID = "9876-54321";
    const url = `${ENVIRONMENT.apiUrl}${API.MINE_INCIDENT(mineGuid, mineIncidentGUID)}`;
  
    it("Request successful, dispatches `success` with correct response", () => {
      const mockResponse = { data: { success: true } };
      mockAxios.onDelete(url).reply(204, mockResponse);
      return deleteMineIncident(
        mineGuid,
        mineIncidentGUID
      )(dispatch).then(() => {
        expect(requestSpy).toHaveBeenCalledTimes(1);
        expect(successSpy).toHaveBeenCalledTimes(1);
        expect(dispatch).toHaveBeenCalledTimes(4);
      });
    });
  
    it("Request failure, dispatches `error` with correct response", () => {
      mockAxios.onDelete(url).reply(418, MOCK.ERROR);
      return deleteMineIncident(
        mineGuid,
        mineIncidentGUID
      )(dispatch).catch(() => {
        expect(requestSpy).toHaveBeenCalledTimes(1);
        expect(errorSpy).toHaveBeenCalledTimes(1);
        expect(dispatch).toHaveBeenCalledTimes(4);
      });
    });
  });
  