Skip to content

Commit

Permalink
feat(useNavigateToLoginMethod): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisMattew committed Nov 5, 2024
1 parent 48dd51f commit eaa3575
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 6 deletions.
195 changes: 195 additions & 0 deletions ts/hooks/__tests__/useNavigateToLoginMethod.test.tsx
Original file line number Diff line number Diff line change
@@ -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(<TestComponent />);

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(<TestComponent />);

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(<TestComponent />);

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(<TestComponent />);

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(<TestComponent />);

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(<TestComponent />);

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 (
<View>
<ButtonSolid
testID="navigate-to-cie-pin"
onPress={navigateToCiePinInsertion}
label=" Navigate to Cie + Pin"
/>
<ButtonSolid
testID="navigate-to-cie-id"
onPress={() => navigateToCieIdLoginScreen(SPID_L2)}
label=" Navigate to CieID"
/>
<ButtonSolid
testID="navigate-to-idp-selection"
onPress={navigateToIdpSelection}
label=" Navigate to IDP selection"
/>
</View>
);
});

/**
* A HOC to provide the redux `Context`
* @param Component the component to wrap
* @returns The given `Component` wrapped with the redux `Provider`
*/
function withStore<P extends Record<string, unknown>>(
Component: JSXElementConstructor<P>
) {
const globalState = appReducer(undefined, applicationChangeState("active"));
const store = createStore(appReducer, globalState as any);

return (props: P) => (
<Provider store={store}>
<Component {...props} />
</Provider>
);
}

function navigateToCieIdNotInstalled() {
jest.spyOn(rnCieId, "isCieIdAvailable").mockImplementation(() => false);
const { getByTestId } = render(<TestComponent />);

const navigateToCieIdScreen = getByTestId("navigate-to-cie-id");
fireEvent.press(navigateToCieIdScreen);

expect(mockNavigate).toHaveBeenCalledWith(ROUTES.AUTHENTICATION, {
screen: ROUTES.CIE_NOT_INSTALLED,
params: {
isUat: IS_UAT
}
});
}
11 changes: 7 additions & 4 deletions ts/hooks/useNavigateToLoginMethod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -63,7 +66,7 @@ const useNavigateToLoginMethod = () => {
screen: ROUTES.AUTHENTICATION_IDP_SELECTION
});
},
{ identifier: "SPID" }
{ identifier: Identifier.SPID }
);
}, [withIsFastLoginOptInCheck, navigate]);

Expand All @@ -77,7 +80,7 @@ const useNavigateToLoginMethod = () => {
screen: ROUTES.CIE_PIN_SCREEN
});
},
{ identifier: "CIE" }
{ identifier: Identifier.CIE }
);
}, [withIsFastLoginOptInCheck, navigate, store, dispatch]);

Expand All @@ -99,7 +102,7 @@ const useNavigateToLoginMethod = () => {
params
});
},
{ identifier: "CIE_ID", params }
{ identifier: Identifier.CIE_ID, params }
);
} else {
navigate(ROUTES.AUTHENTICATION, {
Expand Down
9 changes: 7 additions & 2 deletions ts/screens/authentication/OptInScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,22 @@ 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"
};

export type ChosenIdentifier =
| {
identifier: "SPID" | "CIE";
identifier: Identifier.SPID | Identifier.CIE;
}
| {
identifier: "CIE_ID";
identifier: Identifier.CIE_ID;
params: CieIdLoginProps;
};

Expand Down

0 comments on commit eaa3575

Please sign in to comment.