-
Notifications
You must be signed in to change notification settings - Fork 583
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): write tests for auth2 infrastructure (#11211)
* added tests for authcontext * added tests for authscreen and useauthnavigation
- Loading branch information
Showing
10 changed files
with
189 additions
and
34 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
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
src/app/Scenes/Onboarding/Auth2/__tests__/AuthContext.test.tsx
This file was deleted.
Oops, something went wrong.
90 changes: 90 additions & 0 deletions
90
src/app/Scenes/Onboarding/Auth2/__tests__/AuthContext.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,90 @@ | ||
import { act, renderHook } from "@testing-library/react-native" | ||
import { | ||
AuthContext, | ||
AuthContextModel, | ||
defaultState, | ||
} from "app/Scenes/Onboarding/Auth2/AuthContext" | ||
|
||
describe("AuthContext", () => { | ||
const setup = (initialState: Partial<AuthContextModel>) => { | ||
const view = renderHook( | ||
() => ({ | ||
state: AuthContext.useStoreState((state) => state), | ||
actions: AuthContext.useStoreActions((action) => action), | ||
}), | ||
{ | ||
wrapper: ({ children }: any) => ( | ||
<AuthContext.Provider runtimeModel={{ ...defaultState, ...initialState }}> | ||
{children} | ||
</AuthContext.Provider> | ||
), | ||
} | ||
) | ||
|
||
return { | ||
getState: () => view.result.current.state, | ||
actions: view.result.current.actions, | ||
} | ||
} | ||
|
||
it("sets up intial context and action helpers", () => { | ||
const { getState, actions } = setup({}) | ||
|
||
expect(Object.keys(getState())).toEqual([ | ||
"currentScreen", | ||
"isModalExpanded", | ||
"isMounted", | ||
"previousScreens", | ||
]) | ||
|
||
expect(Object.keys(actions)).toEqual([ | ||
"goBack", | ||
"setCurrentScreen", | ||
"setModalExpanded", | ||
"setParams", | ||
]) | ||
}) | ||
|
||
describe("actions", () => { | ||
it("goBack", () => { | ||
const { getState, actions } = setup({ | ||
currentScreen: { name: "SignUpNameStep" }, | ||
previousScreens: [{ name: "LoginWelcomeStep" }, { name: "SignUpPasswordStep" }], | ||
}) | ||
|
||
act(() => actions.goBack()) | ||
|
||
expect(getState().currentScreen).toEqual({ name: "SignUpPasswordStep" }) | ||
}) | ||
|
||
it("setCurrentScreen", () => { | ||
const { getState, actions } = setup({ currentScreen: { name: "LoginWelcomeStep" } }) | ||
|
||
act(() => actions.setCurrentScreen({ name: "LoginPasswordStep" })) | ||
|
||
expect(getState().currentScreen).toEqual({ name: "LoginPasswordStep" }) | ||
}) | ||
|
||
it("setModalExpanded", () => { | ||
const { getState, actions } = setup({}) | ||
|
||
act(() => actions.setModalExpanded(true)) | ||
|
||
expect(getState().isModalExpanded).toBe(true) | ||
expect(getState().isMounted).toBe(true) | ||
|
||
act(() => actions.setModalExpanded(false)) | ||
|
||
expect(getState().isModalExpanded).toBe(false) | ||
expect(getState().isMounted).toBe(true) | ||
}) | ||
|
||
it("setParams", () => { | ||
const { getState, actions } = setup({}) | ||
|
||
act(() => actions.setParams({ email: "[email protected]" })) | ||
|
||
expect(getState().currentScreen?.params).toEqual({ email: "[email protected]" }) | ||
}) | ||
}) | ||
}) |
5 changes: 0 additions & 5 deletions
5
src/app/Scenes/Onboarding/Auth2/__tests__/AuthScenes.test.tsx
This file was deleted.
Oops, something went wrong.
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/components/__tests__/AuthScreen.test.tsx
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
src/app/Scenes/Onboarding/Auth2/components/__tests__/AuthScreen.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,25 @@ | ||
import { screen } from "@testing-library/react-native" | ||
import { AuthContext } from "app/Scenes/Onboarding/Auth2/AuthContext" | ||
import { AuthScreen } from "app/Scenes/Onboarding/Auth2/components/AuthScreen" | ||
import { renderWithWrappers } from "app/utils/tests/renderWithWrappers" | ||
|
||
describe("AuthScreen", () => { | ||
it("is visible when it is the current screen", () => { | ||
jest | ||
.spyOn(AuthContext, "useStoreState") | ||
.mockReturnValue({ currentScreen: { name: "LoginWelcomeStep" } }) | ||
renderWithWrappers(<AuthScreen name="LoginWelcomeStep" />) | ||
|
||
expect(screen.getByTestId("auth-screen")).toBeVisible() | ||
expect(screen.getByTestId("auth-screen").props.zIndex).toEqual(1) | ||
}) | ||
|
||
it("is not visible when it is not the current screen", () => { | ||
jest | ||
.spyOn(AuthContext, "useStoreState") | ||
.mockReturnValue({ currentScreen: { name: "SignUpPasswordStep" } }) | ||
renderWithWrappers(<AuthScreen name="LoginWelcomeStep" />) | ||
|
||
expect(screen.queryByTestId("auth-screen")).toBeNull() | ||
}) | ||
}) |
5 changes: 0 additions & 5 deletions
5
src/app/Scenes/Onboarding/Auth2/hooks/__tests__/useAuthNavigation.test.tsx
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
src/app/Scenes/Onboarding/Auth2/hooks/__tests__/useAuthNavigation.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,55 @@ | ||
import { | ||
useAuthNavigation, | ||
useAuthScreen, | ||
} from "app/Scenes/Onboarding/Auth2/hooks/useAuthNavigation" | ||
|
||
jest.mock("app/Scenes/Onboarding/Auth2/hooks/useAuthNavigation") | ||
|
||
describe("useAuthNavigation", () => { | ||
const mockUseAuthNavigation = useAuthNavigation as jest.Mock | ||
const mockUseAuthScreen = useAuthScreen as jest.Mock | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
mockUseAuthNavigation.mockReturnValue({ | ||
goBack: jest.fn(), | ||
navigate: jest.fn(), | ||
setParams: jest.fn(), | ||
}) | ||
mockUseAuthScreen.mockReturnValue({ | ||
name: "SignUpPasswordStep", | ||
params: { email: "[email protected]" }, | ||
}) | ||
}) | ||
|
||
describe("useAuthNavigation", () => { | ||
it("returns a function to navigate to the previous screen", () => { | ||
const { goBack } = useAuthNavigation() | ||
goBack() | ||
expect(mockUseAuthNavigation().goBack).toHaveBeenCalled() | ||
}) | ||
|
||
it("returns a function to navigate to a given screen", () => { | ||
const { navigate } = useAuthNavigation() | ||
navigate({ name: "SignUpNameStep", params: { email: "[email protected]", password: "qux" } }) //pragma: allowlist secret | ||
expect(mockUseAuthNavigation().navigate).toHaveBeenCalledWith({ | ||
name: "SignUpNameStep", | ||
params: { email: "[email protected]", password: "qux" }, // pragma: allowlist secret | ||
}) | ||
}) | ||
|
||
it("returns a function to set params on the current screen ", () => { | ||
const { setParams } = useAuthNavigation() | ||
setParams({ email: "[email protected]" }) | ||
expect(mockUseAuthNavigation().setParams).toHaveBeenCalledWith({ email: "[email protected]" }) | ||
}) | ||
}) | ||
|
||
describe("useAuthScreen", () => { | ||
it("returns the current screen name and params", () => { | ||
const { name, params } = useAuthScreen() | ||
expect(name).toBe("SignUpPasswordStep") | ||
expect(params).toEqual({ email: "[email protected]" }) | ||
}) | ||
}) | ||
}) |