-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(DIA-942): test auth2 login welcome step (#11183)
* Listed the things I want to test. * started implementing tests * chore(DIA-942): test auth2 login welcome step
- Loading branch information
Showing
3 changed files
with
224 additions
and
8 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
5 changes: 0 additions & 5 deletions
5
src/app/Scenes/Onboarding/Auth2/scenes/__tests__/LoginWelcomeStep.test.tsx
This file was deleted.
Oops, something went wrong.
204 changes: 204 additions & 0 deletions
204
src/app/Scenes/Onboarding/Auth2/scenes/__tests__/LoginWelcomeStep.tests.tsx
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,204 @@ | ||
import { useNavigation } from "@react-navigation/native" | ||
import { act, fireEvent, screen } from "@testing-library/react-native" | ||
import { useRecaptcha } from "app/Components/Recaptcha/Recaptcha" | ||
import { AuthContext } from "app/Scenes/Onboarding/Auth2/AuthContext" | ||
import { useAuthNavigation } from "app/Scenes/Onboarding/Auth2/hooks/useAuthNavigation" | ||
import { LoginWelcomeStep } from "app/Scenes/Onboarding/Auth2/scenes/LoginWelcomeStep" | ||
import { GlobalStore } from "app/store/GlobalStore" | ||
import { osMajorVersion } from "app/utils/platformUtil" | ||
import { renderWithWrappers } from "app/utils/tests/renderWithWrappers" | ||
import { Platform } from "react-native" | ||
|
||
jest.mock("@react-navigation/native", () => ({ | ||
useNavigation: jest.fn(), | ||
})) | ||
jest.mock("app/Scenes/Onboarding/Auth2/hooks/useAuthNavigation") | ||
jest.mock("app/Scenes/Onboarding/Auth2/hooks/useInputAutofocus") | ||
jest.mock("app/utils/platformUtil") | ||
jest.mock("app/Components/Recaptcha/Recaptcha", () => ({ | ||
useRecaptcha: jest.fn().mockReturnValue({ Recaptcha: () => {}, token: "recaptcha-token" }), | ||
})) | ||
|
||
describe("LoginWelcomeStep", () => { | ||
const mockUseNavigation = useNavigation as jest.Mock | ||
const mockUseAuthNavigation = useAuthNavigation as jest.Mock | ||
const mockUseRecaptcha = useRecaptcha as jest.Mock | ||
const mockOSMajorVersion = osMajorVersion as jest.Mock | ||
|
||
beforeEach(() => { | ||
/** | ||
* This component involves animations, so we need to mock timers to ensure that the animations | ||
* are completed before making assertions. | ||
*/ | ||
jest.useFakeTimers() | ||
|
||
// Use iOS as the default platform to avoid having to mock the platform in every test. | ||
Platform.OS = "ios" | ||
}) | ||
|
||
afterEach(() => { | ||
jest.runOnlyPendingTimers() | ||
jest.useRealTimers() | ||
}) | ||
|
||
it("expands the modal when the input field is tapped", async () => { | ||
renderWelcomeStep() | ||
|
||
expect(screen.queryByA11yHint("Go back to the previous screen")).not.toBeOnTheScreen() | ||
expect(screen.queryByA11yHint("Continue to the next screen")).not.toBeOnTheScreen() | ||
expect(screen.getByTestId("social-signin-and-disclaimers")).toHaveAnimatedStyle({ opacity: 1 }) | ||
|
||
fireEvent(screen.getByA11yHint("Enter your email address"), "focus") | ||
|
||
jest.advanceTimersByTime(1000) | ||
|
||
expect(screen.getByA11yHint("Go back to the previous screen")).toBeOnTheScreen() | ||
expect(screen.getByA11yHint("Continue to the next screen")).toBeOnTheScreen() | ||
expect(screen.queryByTestId("social-signin-and-disclaimers")).not.toBeOnTheScreen() | ||
}) | ||
|
||
describe("when the modal is expanded", () => { | ||
it("closes the modal when the back icon is tapped", () => { | ||
renderExpandedWelcomeStep() | ||
|
||
fireEvent.press(screen.getByA11yHint("Go back to the previous screen")) | ||
|
||
jest.advanceTimersByTime(1000) | ||
|
||
expect(screen.queryByA11yHint("Go back to the previous screen")).not.toBeOnTheScreen() | ||
expect(screen.queryByA11yHint("Continue to the next screen")).not.toBeOnTheScreen() | ||
expect(screen.getByTestId("social-signin-and-disclaimers")).toHaveAnimatedStyle({ | ||
opacity: 1, | ||
}) | ||
}) | ||
|
||
it("navigates to the sign-up password step if a user account doesn't exist", async () => { | ||
GlobalStore.actions.auth.verifyUser = jest | ||
.fn() | ||
.mockResolvedValue("user_does_not_exist") as any | ||
|
||
const navigateSpy = jest.fn() | ||
mockUseAuthNavigation.mockReturnValueOnce({ | ||
navigate: navigateSpy, | ||
}) | ||
|
||
renderExpandedWelcomeStep() | ||
|
||
fireEvent.changeText(screen.getByA11yHint("Enter your email address"), "[email protected]") | ||
fireEvent.press(screen.getByA11yHint("Continue to the next screen")) | ||
|
||
// eslint-disable-next-line testing-library/no-unnecessary-act | ||
await act(() => fireEvent.press(screen.getByA11yHint("Continue to the next screen"))) | ||
|
||
expect(navigateSpy).toHaveBeenCalledWith({ | ||
name: "SignUpPasswordStep", | ||
params: { email: "[email protected]" }, | ||
}) | ||
}) | ||
|
||
it("navigates to the login password step if a user account exists", async () => { | ||
GlobalStore.actions.auth.verifyUser = jest.fn().mockResolvedValue("user_exists") as any | ||
|
||
const navigateSpy = jest.fn() | ||
mockUseAuthNavigation.mockReturnValueOnce({ | ||
navigate: navigateSpy, | ||
}) | ||
|
||
renderExpandedWelcomeStep() | ||
|
||
fireEvent.changeText(screen.getByA11yHint("Enter your email address"), "[email protected]") | ||
fireEvent.press(screen.getByA11yHint("Continue to the next screen")) | ||
|
||
// eslint-disable-next-line testing-library/no-unnecessary-act | ||
await act(() => fireEvent.press(screen.getByA11yHint("Continue to the next screen"))) | ||
|
||
expect(navigateSpy).toHaveBeenCalledWith({ | ||
name: "LoginPasswordStep", | ||
params: { email: "[email protected]" }, | ||
}) | ||
}) | ||
|
||
it("navigates to the login password step if recaptcha fails", async () => { | ||
mockUseRecaptcha.mockReturnValueOnce({ Recaptcha: () => {}, token: null }) | ||
|
||
const navigateSpy = jest.fn() | ||
mockUseAuthNavigation.mockReturnValueOnce({ | ||
navigate: navigateSpy, | ||
}) | ||
|
||
renderExpandedWelcomeStep() | ||
|
||
fireEvent.changeText(screen.getByA11yHint("Enter your email address"), "[email protected]") | ||
fireEvent.press(screen.getByA11yHint("Continue to the next screen")) | ||
|
||
// eslint-disable-next-line testing-library/no-unnecessary-act | ||
await act(() => fireEvent.press(screen.getByA11yHint("Continue to the next screen"))) | ||
|
||
expect(navigateSpy).toHaveBeenCalledWith({ | ||
name: "LoginPasswordStep", | ||
params: { email: "[email protected]", showSignUpLink: true }, | ||
}) | ||
}) | ||
}) | ||
|
||
it("shows the Sign in with Apple button on iOS versions >= 13 ", () => { | ||
mockOSMajorVersion.mockReturnValue(13) | ||
renderWelcomeStep() | ||
expect(screen.getByA11yHint("Sign in with Apple")).toBeOnTheScreen() | ||
}) | ||
|
||
it("shows the Sign in with Apple button on iOS versions < 13", () => { | ||
mockOSMajorVersion.mockReturnValue(12) | ||
renderWelcomeStep() | ||
expect(screen.queryByA11yHint("Sign in with Apple")).not.toBeOnTheScreen() | ||
}) | ||
|
||
it("hides the Sign in with Apple button on Android", () => { | ||
Platform.OS = "android" | ||
renderWelcomeStep() | ||
expect(screen.queryByA11yHint("Sign in with Apple")).not.toBeOnTheScreen() | ||
}) | ||
|
||
it("provides a link to the terms and conditions", async () => { | ||
const navigateSpy = jest.fn() | ||
mockUseNavigation.mockReturnValueOnce({ | ||
navigate: navigateSpy, | ||
}) | ||
|
||
renderWelcomeStep() | ||
|
||
// eslint-disable-next-line testing-library/no-unnecessary-act | ||
await act(() => fireEvent.press(screen.getByA11yHint("View the Terms and Conditions"))) | ||
|
||
expect(navigateSpy).toHaveBeenCalledWith("OnboardingWebView", { url: "/terms" }) | ||
}) | ||
|
||
it("provides a link to the privacy policy", async () => { | ||
const navigateSpy = jest.fn() | ||
mockUseNavigation.mockReturnValueOnce({ | ||
navigate: navigateSpy, | ||
}) | ||
|
||
renderWelcomeStep() | ||
|
||
// eslint-disable-next-line testing-library/no-unnecessary-act | ||
await act(() => fireEvent.press(screen.getByA11yHint("View the Privacy Policy"))) | ||
|
||
expect(navigateSpy).toHaveBeenCalledWith("OnboardingWebView", { url: "/privacy" }) | ||
}) | ||
}) | ||
|
||
const renderWelcomeStep = () => { | ||
renderWithWrappers( | ||
<AuthContext.Provider> | ||
<LoginWelcomeStep /> | ||
</AuthContext.Provider> | ||
) | ||
jest.advanceTimersByTime(400) | ||
} | ||
|
||
const renderExpandedWelcomeStep = () => { | ||
renderWelcomeStep() | ||
fireEvent(screen.getByA11yHint("Enter your email address"), "focus") | ||
jest.advanceTimersByTime(1000) | ||
} |