Skip to content

Commit fd0ff87

Browse files
Vangaorthforrest57
andauthored
chore: [IOCOM-1817] Add test for common FIMS hook (#6291)
## Short description This PR adds a test for the FIMS common hook. ## List of changes proposed in this pull request - Test for the FIMS common hook - `utils/hooks.ts` renamed to `hooks/index.ts` ## How to test CI tests should succeed. Co-authored-by: Martino Cesari Tomba <[email protected]>
1 parent ec8f5cc commit fd0ff87

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import * as pot from "@pagopa/ts-commons/lib/pot";
2+
import { createStore } from "redux";
3+
import { applicationChangeState } from "../../../../../store/actions/application";
4+
import { appReducer } from "../../../../../store/reducers";
5+
import { renderScreenWithNavigationStoreContext } from "../../../../../utils/testWrapper";
6+
import { useAutoFetchingServiceByIdPot } from "..";
7+
import { ServiceId } from "../../../../../../definitions/backend/ServiceId";
8+
import * as serviceSelectors from "../../../../services/details/store/reducers";
9+
import { ServicePublic } from "../../../../../../definitions/backend/ServicePublic";
10+
import { loadServiceDetail } from "../../../../services/details/store/actions/details";
11+
12+
const mockDispatch = jest.fn();
13+
jest.mock("react-redux", () => ({
14+
...jest.requireActual<typeof import("react-redux")>("react-redux"),
15+
useDispatch: () => mockDispatch
16+
}));
17+
18+
describe("useAutoFetchingServiceByIdPot", () => {
19+
beforeEach(() => {
20+
jest.resetAllMocks();
21+
jest.clearAllMocks();
22+
});
23+
const serviceId = "01JA7JWXH7488H8APPYG8JXZXE" as ServiceId;
24+
const serviceData = {
25+
service_id: serviceId
26+
} as ServicePublic;
27+
[
28+
pot.none,
29+
pot.noneLoading,
30+
pot.noneUpdating(serviceData),
31+
pot.noneError(Error()),
32+
pot.some(serviceData),
33+
pot.someLoading(serviceData),
34+
pot.someUpdating(serviceData, serviceData),
35+
pot.someError(serviceData, Error())
36+
].forEach(serviceDataPot => {
37+
const shouldDispatchLoadServiceDetailAction =
38+
serviceDataPot.kind === "PotNone" ||
39+
serviceDataPot.kind === "PotNoneError";
40+
it(`should ${
41+
shouldDispatchLoadServiceDetailAction ? " " : "not"
42+
} dispatch 'loadServiceDetail.request(${serviceId})' and return an instance of ServiceData wrapped in a pot with state '${
43+
serviceDataPot.kind
44+
}'`, () => {
45+
jest
46+
.spyOn(serviceSelectors, "serviceByIdPotSelector")
47+
.mockImplementation((_, selectorServiceId) =>
48+
selectorServiceId === serviceData.service_id
49+
? serviceDataPot
50+
: pot.none
51+
);
52+
53+
const hookResult = jest.fn();
54+
renderComponentWithNavigationContext(serviceId, hookResult);
55+
56+
if (shouldDispatchLoadServiceDetailAction) {
57+
expect(mockDispatch.mock.calls.length).toBe(1);
58+
expect(mockDispatch.mock.calls[0].length).toBe(1);
59+
expect(mockDispatch.mock.calls[0][0]).toEqual(
60+
loadServiceDetail.request(serviceId)
61+
);
62+
} else {
63+
expect(mockDispatch.mock.calls.length).toBe(0);
64+
}
65+
66+
expect(hookResult.mock.calls.length).toBe(1);
67+
expect(hookResult.mock.calls[0].length).toBe(1);
68+
expect(hookResult.mock.calls[0][0]).toEqual(serviceDataPot);
69+
});
70+
});
71+
});
72+
73+
const renderComponentWithNavigationContext = (
74+
serviceId: ServiceId,
75+
hookResult: (_: pot.Pot<ServicePublic, Error>) => void
76+
) => {
77+
const initialState = appReducer(undefined, applicationChangeState("active"));
78+
const store = createStore(appReducer, initialState as any);
79+
80+
return renderScreenWithNavigationStoreContext(
81+
() => {
82+
const hookOutput = useAutoFetchingServiceByIdPot(serviceId);
83+
hookResult(hookOutput);
84+
return undefined;
85+
},
86+
"MOCK_ROUTE",
87+
{},
88+
store
89+
);
90+
};
File renamed without changes.

ts/features/fims/history/components/FimsHistoryListItem.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { Consent } from "../../../../../definitions/fims/Consent";
1717
import I18n from "../../../../i18n";
1818
import { dateToAccessibilityReadableFormat } from "../../../../utils/accessibility";
1919
import { potFoldWithDefault } from "../../../../utils/pot";
20-
import { useAutoFetchingServiceByIdPot } from "../../common/utils/hooks";
20+
import { useAutoFetchingServiceByIdPot } from "../../common/hooks";
2121
import { FimsHistorySharedStyles } from "../utils/styles";
2222
import { LoadingFimsHistoryListItem } from "./FimsHistoryLoaders";
2323

ts/features/fims/singleSignOn/components/FimsSuccessBody.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { useIODispatch, useIOStore } from "../../../../store/hooks";
2828
import { useIOBottomSheetModal } from "../../../../utils/hooks/bottomSheet";
2929
import { openWebUrl } from "../../../../utils/url";
3030
import { logoForService } from "../../../services/home/utils";
31-
import { useAutoFetchingServiceByIdPot } from "../../common/utils/hooks";
31+
import { useAutoFetchingServiceByIdPot } from "../../common/hooks";
3232
import { fimsGetRedirectUrlAndOpenIABAction } from "../store/actions";
3333
import { fimsErrorTagSelector } from "../store/selectors";
3434
import { ConsentData } from "../types";

0 commit comments

Comments
 (0)