Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 99054f9

Browse files
committed
Add test to cover redirect case
1 parent a88b109 commit 99054f9

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

site/components/SignIn/SignInForm.test.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,29 @@ describe("SignInForm", () => {
5555
act(() => elem.click())
5656

5757
// Then
58-
// Should redirect because login was successfully
58+
// Should redirect because login was successful
5959
await waitFor(() => expect(singletonRouter).toMatchObject({ asPath: "/" }))
6060
})
61+
62+
it("respects ?redirect query parameter when complete", async () => {
63+
// Given
64+
const loginHandler = (_email: string, _password: string) => Promise.resolve()
65+
// Set a path to redirect to after login is successful
66+
mockRouter.setCurrentUrl("/login?redirect=%2Fsome%2Fother%2Fpath")
67+
68+
// When
69+
// Render the component
70+
const { container } = render(<SignInForm loginHandler={loginHandler} />)
71+
// Set user / password
72+
const inputs = container.querySelectorAll("input")
73+
fireEvent.change(inputs[0], { target: { value: "[email protected]" } })
74+
fireEvent.change(inputs[1], { target: { value: "password" } })
75+
// Click sign-in
76+
const elem = await screen.findByText("Sign In")
77+
act(() => elem.click())
78+
79+
// Then
80+
// Should redirect to /some/other/path because ?redirect was specified and login was successful
81+
await waitFor(() => expect(singletonRouter).toMatchObject({ asPath: "/some/other/path" }))
82+
})
6183
})

0 commit comments

Comments
 (0)