From a0606a01b7e1c28d543e19b4385a7a1f600e8c1c Mon Sep 17 00:00:00 2001 From: Andrea Piai Date: Wed, 16 Oct 2024 18:01:48 +0200 Subject: [PATCH] Tests for FIMS common hook --- .../common/hooks/__tests__/index.test.tsx | 90 +++++++++++++++++++ .../{utils/hooks.tsx => hooks/index.tsx} | 0 .../components/FimsHistoryListItem.tsx | 2 +- .../components/FimsSuccessBody.tsx | 2 +- 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 ts/features/fims/common/hooks/__tests__/index.test.tsx rename ts/features/fims/common/{utils/hooks.tsx => hooks/index.tsx} (100%) diff --git a/ts/features/fims/common/hooks/__tests__/index.test.tsx b/ts/features/fims/common/hooks/__tests__/index.test.tsx new file mode 100644 index 00000000000..1022ad750a4 --- /dev/null +++ b/ts/features/fims/common/hooks/__tests__/index.test.tsx @@ -0,0 +1,90 @@ +import * as pot from "@pagopa/ts-commons/lib/pot"; +import { createStore } from "redux"; +import { applicationChangeState } from "../../../../../store/actions/application"; +import { appReducer } from "../../../../../store/reducers"; +import { renderScreenWithNavigationStoreContext } from "../../../../../utils/testWrapper"; +import { useAutoFetchingServiceByIdPot } from ".."; +import { ServiceId } from "../../../../../../definitions/backend/ServiceId"; +import * as serviceSelectors from "../../../../services/details/store/reducers"; +import { ServicePublic } from "../../../../../../definitions/backend/ServicePublic"; +import { loadServiceDetail } from "../../../../services/details/store/actions/details"; + +const mockDispatch = jest.fn(); +jest.mock("react-redux", () => ({ + ...jest.requireActual("react-redux"), + useDispatch: () => mockDispatch +})); + +describe("useAutoFetchingServiceByIdPot", () => { + beforeEach(() => { + jest.resetAllMocks(); + jest.clearAllMocks(); + }); + const serviceId = "01JA7JWXH7488H8APPYG8JXZXE" as ServiceId; + const serviceData = { + service_id: serviceId + } as ServicePublic; + [ + pot.none, + pot.noneLoading, + pot.noneUpdating(serviceData), + pot.noneError(Error()), + pot.some(serviceData), + pot.someLoading(serviceData), + pot.someUpdating(serviceData, serviceData), + pot.someError(serviceData, Error()) + ].forEach(serviceDataPot => { + const shouldDispatchLoadServiceDetailAction = + serviceDataPot.kind === "PotNone" || + serviceDataPot.kind === "PotNoneError"; + it(`should ${ + shouldDispatchLoadServiceDetailAction ? " " : "not" + } dispatch 'loadServiceDetail.request(${serviceId})' and return an instance of ServiceData wrapped in a pot with state '${ + serviceDataPot.kind + }'`, () => { + jest + .spyOn(serviceSelectors, "serviceByIdPotSelector") + .mockImplementation((_, selectorServiceId) => + selectorServiceId === serviceData.service_id + ? serviceDataPot + : pot.none + ); + + const hookResult = jest.fn(); + renderComponentWithNavigationContext(serviceId, hookResult); + + if (shouldDispatchLoadServiceDetailAction) { + expect(mockDispatch.mock.calls.length).toBe(1); + expect(mockDispatch.mock.calls[0].length).toBe(1); + expect(mockDispatch.mock.calls[0][0]).toEqual( + loadServiceDetail.request(serviceId) + ); + } else { + expect(mockDispatch.mock.calls.length).toBe(0); + } + + expect(hookResult.mock.calls.length).toBe(1); + expect(hookResult.mock.calls[0].length).toBe(1); + expect(hookResult.mock.calls[0][0]).toEqual(serviceDataPot); + }); + }); +}); + +const renderComponentWithNavigationContext = ( + serviceId: ServiceId, + hookResult: (_: pot.Pot) => void +) => { + const initialState = appReducer(undefined, applicationChangeState("active")); + const store = createStore(appReducer, initialState as any); + + return renderScreenWithNavigationStoreContext( + () => { + const hookOutput = useAutoFetchingServiceByIdPot(serviceId); + hookResult(hookOutput); + return undefined; + }, + "MOCK_ROUTE", + {}, + store + ); +}; diff --git a/ts/features/fims/common/utils/hooks.tsx b/ts/features/fims/common/hooks/index.tsx similarity index 100% rename from ts/features/fims/common/utils/hooks.tsx rename to ts/features/fims/common/hooks/index.tsx diff --git a/ts/features/fims/history/components/FimsHistoryListItem.tsx b/ts/features/fims/history/components/FimsHistoryListItem.tsx index f0357a22c83..d9f94d27189 100644 --- a/ts/features/fims/history/components/FimsHistoryListItem.tsx +++ b/ts/features/fims/history/components/FimsHistoryListItem.tsx @@ -17,7 +17,7 @@ import { Consent } from "../../../../../definitions/fims/Consent"; import I18n from "../../../../i18n"; import { dateToAccessibilityReadableFormat } from "../../../../utils/accessibility"; import { potFoldWithDefault } from "../../../../utils/pot"; -import { useAutoFetchingServiceByIdPot } from "../../common/utils/hooks"; +import { useAutoFetchingServiceByIdPot } from "../../common/hooks"; import { FimsHistorySharedStyles } from "../utils/styles"; import { LoadingFimsHistoryListItem } from "./FimsHistoryLoaders"; diff --git a/ts/features/fims/singleSignOn/components/FimsSuccessBody.tsx b/ts/features/fims/singleSignOn/components/FimsSuccessBody.tsx index 1efeea6b3a7..45bb56ba5ea 100644 --- a/ts/features/fims/singleSignOn/components/FimsSuccessBody.tsx +++ b/ts/features/fims/singleSignOn/components/FimsSuccessBody.tsx @@ -28,7 +28,7 @@ import { useIODispatch, useIOStore } from "../../../../store/hooks"; import { useIOBottomSheetModal } from "../../../../utils/hooks/bottomSheet"; import { openWebUrl } from "../../../../utils/url"; import { logoForService } from "../../../services/home/utils"; -import { useAutoFetchingServiceByIdPot } from "../../common/utils/hooks"; +import { useAutoFetchingServiceByIdPot } from "../../common/hooks"; import { fimsGetRedirectUrlAndOpenIABAction } from "../store/actions"; import { fimsErrorTagSelector } from "../store/selectors"; import { ConsentData } from "../types";