|
| 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 | +}; |
0 commit comments