From 211d8495657958d0d28c027eb5b1468bd05daa3e Mon Sep 17 00:00:00 2001 From: Martino Cesari Tomba <60693085+forrest57@users.noreply.github.com> Date: Mon, 3 Feb 2025 17:44:43 +0100 Subject: [PATCH] test: [IOCOM-1822] fims history export saga tests (#6677) ## Short description addition of said tests ## List of changes proposed in this pull request - required saga tests ## How to test automated tests should pass --- .../handleExportFimsHistorySaga.test.ts | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 ts/features/fims/history/saga/__tests__/handleExportFimsHistorySaga.test.ts diff --git a/ts/features/fims/history/saga/__tests__/handleExportFimsHistorySaga.test.ts b/ts/features/fims/history/saga/__tests__/handleExportFimsHistorySaga.test.ts new file mode 100644 index 00000000000..14ff10fabf2 --- /dev/null +++ b/ts/features/fims/history/saga/__tests__/handleExportFimsHistorySaga.test.ts @@ -0,0 +1,112 @@ +jest.mock("../../../common/analytics"); +import * as E from "fp-ts/Either"; +import { expectSaga, testSaga } from "redux-saga-test-plan"; +import { withRefreshApiCall } from "../../../../fastLogin/saga/utils"; +import { trackExportSucceeded } from "../../../common/analytics"; +import { FimsHistoryClient } from "../../api/client"; +import { fimsHistoryExport } from "../../store/actions"; +import { handleExportFimsHistorySaga } from "../handleExportFimsHistorySaga"; + +describe("handleExportFimsHistorySaga", () => { + beforeEach(() => { + jest.clearAllMocks(); + jest.resetAllMocks(); + }); + type ResponseType = Awaited>; + const mockBearerToken = "mockBearerToken"; + const mockAction = fimsHistoryExport.request(); + + it("should correctly pass the bearerToken to the client, dispatch success with payload 'SUCCESS' and track the export success when status is 202", () => { + const response = E.right({ status: 202 }) as ResponseType; + + const resultPromise = Promise.resolve(response); + const mockClient = jest.fn(() => resultPromise); + testSaga( + handleExportFimsHistorySaga, + mockClient, + mockBearerToken, + mockAction + ) + .next() + .call(withRefreshApiCall, resultPromise, mockAction) + .next(response) + .put(fimsHistoryExport.success("SUCCESS")) + .next() + .isDone(); + + expect(mockClient).toHaveBeenCalledWith({ + Bearer: `Bearer ${mockBearerToken}` + }); + expect(trackExportSucceeded).toHaveBeenCalledTimes(1); + }); + it('should dispatch success with payload "ALREADY_EXPORTING", but not track an export success when status is 409 ', () => { + const response = E.right({ status: 409 }) as ResponseType; + + const resultPromise = Promise.resolve(response); + const mockClient = jest.fn(() => resultPromise); + testSaga( + handleExportFimsHistorySaga, + mockClient, + mockBearerToken, + mockAction + ) + .next() + .call(withRefreshApiCall, resultPromise, mockAction) + .next(response) + .put(fimsHistoryExport.success("ALREADY_EXPORTING")) + .next() + .isDone(); + expect(trackExportSucceeded).toHaveBeenCalledTimes(0); + }); + it("should dispatch failure when status is anything else ", () => { + const response = E.right({ status: 418 }) as ResponseType; + + const resultPromise = Promise.resolve(response); + const mockClient = jest.fn(() => resultPromise); + testSaga( + handleExportFimsHistorySaga, + mockClient, + mockBearerToken, + mockAction + ) + .next() + .call(withRefreshApiCall, resultPromise, mockAction) + .next(response) + .put(fimsHistoryExport.failure()) + .next() + .isDone(); + }); + it("should dispatch failure when decoding fails", () => { + const response = E.left({ status: 202 }) as ResponseType; + + const resultPromise = Promise.resolve(response); + const mockClient = jest.fn(() => resultPromise); + testSaga( + handleExportFimsHistorySaga, + mockClient, + mockBearerToken, + mockAction + ) + .next() + .call(withRefreshApiCall, resultPromise, mockAction) + .next(response) + .put(fimsHistoryExport.failure()) + .next() + .isDone(); + }); + it("should dispatch failure when promise is rejected", async () => { + const response = E.right({ status: 202 }) as ResponseType; + + const resultPromise = Promise.reject(response); + const mockClient = jest.fn(() => resultPromise); + + return expectSaga( + handleExportFimsHistorySaga, + mockClient, + mockBearerToken, + mockAction + ) + .put(fimsHistoryExport.failure()) + .run(); + }); +});