From eaa3575d801f0fb24d3f204cfc091639ca74d33d Mon Sep 17 00:00:00 2001 From: ChrisMattew Date: Tue, 5 Nov 2024 17:41:34 +0100 Subject: [PATCH] feat(useNavigateToLoginMethod): add tests --- .../useNavigateToLoginMethod.test.tsx | 195 ++++++++++++++++++ ts/hooks/useNavigateToLoginMethod.tsx | 11 +- ts/screens/authentication/OptInScreen.tsx | 9 +- 3 files changed, 209 insertions(+), 6 deletions(-) create mode 100644 ts/hooks/__tests__/useNavigateToLoginMethod.test.tsx diff --git a/ts/hooks/__tests__/useNavigateToLoginMethod.test.tsx b/ts/hooks/__tests__/useNavigateToLoginMethod.test.tsx new file mode 100644 index 00000000000..0bdb690a655 --- /dev/null +++ b/ts/hooks/__tests__/useNavigateToLoginMethod.test.tsx @@ -0,0 +1,195 @@ +import { fireEvent, render } from "@testing-library/react-native"; +import React, { JSXElementConstructor } from "react"; +import { createStore } from "redux"; +import { Provider } from "react-redux"; +import { View } from "react-native"; +import { ButtonSolid } from "@pagopa/io-app-design-system"; +import * as rnCieId from "@pagopa/io-react-native-cieid"; +import useNavigateToLoginMethod from "../useNavigateToLoginMethod"; +import { appReducer } from "../../store/reducers"; +import { applicationChangeState } from "../../store/actions/application"; +import ROUTES from "../../navigation/routes"; +import * as fastLoginSelector from "../../features/fastLogin/store/selectors"; +import { Identifier } from "../../screens/authentication/OptInScreen"; + +const IS_UAT = false; +const SPID_L2 = "SpidL2"; + +const mockNavigate = jest.fn(); + +jest.mock("@react-navigation/native", () => ({ + ...jest.requireActual("@react-navigation/native"), + useNavigation: () => ({ + navigate: mockNavigate + }) +})); + +jest.mock("../../features/cieLogin/store/selectors", () => ({ + isCieLoginUatEnabledSelector: () => IS_UAT +})); +jest.mock("../../store/reducers/cie", () => ({ + isCieSupportedSelector: () => true +})); + +describe(useNavigateToLoginMethod, () => { + afterEach(jest.clearAllMocks); + + describe("Login flow WITHOUT OptIn", () => { + it("Should navigate to the Cie + Pin screen", () => { + const { getByTestId } = render(); + + const navigateToCiePin = getByTestId("navigate-to-cie-pin"); + fireEvent.press(navigateToCiePin); + + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.AUTHENTICATION, { + screen: ROUTES.CIE_PIN_SCREEN + }); + }); + it("Should navigate to idp selection", () => { + const { getByTestId } = render(); + + const navigateToIdpSelection = getByTestId("navigate-to-idp-selection"); + fireEvent.press(navigateToIdpSelection); + + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.AUTHENTICATION, { + screen: ROUTES.AUTHENTICATION_IDP_SELECTION + }); + }); + it("Should navigate to the CieID screen", () => { + jest.spyOn(rnCieId, "isCieIdAvailable").mockImplementation(() => true); + const { getByTestId } = render(); + + const navigateToCieIdScreen = getByTestId("navigate-to-cie-id"); + fireEvent.press(navigateToCieIdScreen); + + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.AUTHENTICATION, { + screen: ROUTES.AUTHENTICATION_CIE_ID_LOGIN, + params: { + spidLevel: SPID_L2, + isUat: IS_UAT + } + }); + }); + it( + "Should navigate to the CieID not installed screen", + navigateToCieIdNotInstalled + ); + }); + describe("Login flow WITH OptIn", () => { + beforeAll(() => { + jest + .spyOn(fastLoginSelector, "fastLoginOptInFFEnabled") + .mockImplementation(() => true); + }); + afterAll(jest.clearAllMocks); + + it(`Should navigate to the OptIn screen with ${Identifier.CIE} as identifier`, () => { + const { getByTestId } = render(); + + const navigateToCiePin = getByTestId("navigate-to-cie-pin"); + fireEvent.press(navigateToCiePin); + + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.AUTHENTICATION, { + screen: ROUTES.AUTHENTICATION_OPT_IN, + params: { + identifier: Identifier.CIE + } + }); + }); + it(`Should navigate to the OptIn screen with ${Identifier.SPID} as identifier`, () => { + const { getByTestId } = render(); + + const navigateToIdpSelection = getByTestId("navigate-to-idp-selection"); + fireEvent.press(navigateToIdpSelection); + + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.AUTHENTICATION, { + screen: ROUTES.AUTHENTICATION_OPT_IN, + params: { + identifier: Identifier.SPID + } + }); + }); + it(`Should navigate to the OptIn screen with ${Identifier.CIE_ID} as identifier`, () => { + jest.spyOn(rnCieId, "isCieIdAvailable").mockImplementation(() => true); + const { getByTestId } = render(); + + const navigateToCieIdScreen = getByTestId("navigate-to-cie-id"); + fireEvent.press(navigateToCieIdScreen); + + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.AUTHENTICATION, { + screen: ROUTES.AUTHENTICATION_OPT_IN, + params: { + identifier: Identifier.CIE_ID, + params: { + spidLevel: SPID_L2, + isUat: IS_UAT + } + } + }); + }); + it( + "Should navigate to the CieID not installed screen", + navigateToCieIdNotInstalled + ); + }); +}); + +const TestComponent = withStore(() => { + const { + navigateToCieIdLoginScreen, + navigateToIdpSelection, + navigateToCiePinInsertion + } = useNavigateToLoginMethod(); + return ( + + + navigateToCieIdLoginScreen(SPID_L2)} + label=" Navigate to CieID" + /> + + + ); +}); + +/** + * A HOC to provide the redux `Context` + * @param Component the component to wrap + * @returns The given `Component` wrapped with the redux `Provider` + */ +function withStore

>( + Component: JSXElementConstructor

+) { + const globalState = appReducer(undefined, applicationChangeState("active")); + const store = createStore(appReducer, globalState as any); + + return (props: P) => ( + + + + ); +} + +function navigateToCieIdNotInstalled() { + jest.spyOn(rnCieId, "isCieIdAvailable").mockImplementation(() => false); + const { getByTestId } = render(); + + const navigateToCieIdScreen = getByTestId("navigate-to-cie-id"); + fireEvent.press(navigateToCieIdScreen); + + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.AUTHENTICATION, { + screen: ROUTES.CIE_NOT_INSTALLED, + params: { + isUat: IS_UAT + } + }); +} diff --git a/ts/hooks/useNavigateToLoginMethod.tsx b/ts/hooks/useNavigateToLoginMethod.tsx index 46ef4e918d4..22c8abef1f2 100644 --- a/ts/hooks/useNavigateToLoginMethod.tsx +++ b/ts/hooks/useNavigateToLoginMethod.tsx @@ -17,7 +17,10 @@ import { import { idpSelected } from "../store/actions/authentication"; import { SpidIdp } from "../../definitions/content/SpidIdp"; import { isCieLoginUatEnabledSelector } from "../features/cieLogin/store/selectors"; -import { ChosenIdentifier } from "../screens/authentication/OptInScreen"; +import { + ChosenIdentifier, + Identifier +} from "../screens/authentication/OptInScreen"; export const IdpCIE: SpidIdp = { id: "cie", @@ -63,7 +66,7 @@ const useNavigateToLoginMethod = () => { screen: ROUTES.AUTHENTICATION_IDP_SELECTION }); }, - { identifier: "SPID" } + { identifier: Identifier.SPID } ); }, [withIsFastLoginOptInCheck, navigate]); @@ -77,7 +80,7 @@ const useNavigateToLoginMethod = () => { screen: ROUTES.CIE_PIN_SCREEN }); }, - { identifier: "CIE" } + { identifier: Identifier.CIE } ); }, [withIsFastLoginOptInCheck, navigate, store, dispatch]); @@ -99,7 +102,7 @@ const useNavigateToLoginMethod = () => { params }); }, - { identifier: "CIE_ID", params } + { identifier: Identifier.CIE_ID, params } ); } else { navigate(ROUTES.AUTHENTICATION, { diff --git a/ts/screens/authentication/OptInScreen.tsx b/ts/screens/authentication/OptInScreen.tsx index f63f8fa3dab..d62ec10c87a 100644 --- a/ts/screens/authentication/OptInScreen.tsx +++ b/ts/screens/authentication/OptInScreen.tsx @@ -35,6 +35,11 @@ import { useHeaderSecondLevel } from "../../hooks/useHeaderSecondLevel"; import { CieIdLoginProps } from "../../features/cieLogin/components/CieIdLoginWebView"; import { AuthenticationParamsList } from "../../navigation/params/AuthenticationParamsList"; +export enum Identifier { + SPID = "SPID", + CIE = "CIE", + CIE_ID = "CIE_ID" +} const contextualHelpMarkdown: ContextualHelpPropsMarkdown = { title: "authentication.opt_in.contextualHelpTitle", body: "authentication.opt_in.contextualHelpContent" @@ -42,10 +47,10 @@ const contextualHelpMarkdown: ContextualHelpPropsMarkdown = { export type ChosenIdentifier = | { - identifier: "SPID" | "CIE"; + identifier: Identifier.SPID | Identifier.CIE; } | { - identifier: "CIE_ID"; + identifier: Identifier.CIE_ID; params: CieIdLoginProps; };