-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useNavigateToLoginMethod): add tests
- Loading branch information
1 parent
48dd51f
commit eaa3575
Showing
3 changed files
with
209 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters