Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: [IOCOM-1817] Add test for common FIMS hook #6291

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a0606a0
Tests for FIMS common hook
Vangaorth Oct 16, 2024
98fc6e3
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Oct 17, 2024
b985a3a
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Oct 18, 2024
d90cadf
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Oct 18, 2024
0f18458
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Oct 18, 2024
9e43c7c
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Oct 18, 2024
4a9bf4d
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Oct 18, 2024
7d8b1d7
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Oct 21, 2024
78031cb
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Oct 22, 2024
01b199a
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Oct 22, 2024
b07a526
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Oct 23, 2024
b1b8010
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Oct 24, 2024
13a0932
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Oct 24, 2024
d31dee8
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Oct 25, 2024
a7ef437
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Oct 31, 2024
c8d1c58
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Oct 31, 2024
6c172ff
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Nov 4, 2024
5256d6b
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Nov 4, 2024
1bd1d1d
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Nov 4, 2024
4ae5c5a
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Nov 4, 2024
40da7a8
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Nov 5, 2024
1315e83
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Nov 5, 2024
7228468
Merge branch 'master' into IOCOM-1817_fimsTestCommonHook
Vangaorth Nov 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions ts/features/fims/common/hooks/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof import("react-redux")>("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<ServicePublic, Error>) => 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
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Loading