-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #658 from hngprojects/feat/auth-forgot-password-test
- Loading branch information
Showing
3 changed files
with
135 additions
and
9 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,116 @@ | ||
import { fireEvent, render, screen, waitFor } from "@testing-library/react"; | ||
|
||
import ForgotPassword from "./page"; | ||
|
||
vi.mock("next/link", () => ({ | ||
default: ({ children }: { children: React.ReactNode }) => children, | ||
})); | ||
|
||
describe("forgot password page", () => { | ||
it("renders the initial email input form", () => { | ||
expect.hasAssertions(); | ||
render(<ForgotPassword />); | ||
|
||
expect(screen.getByText(/forgot password/i)).toBeInTheDocument(); | ||
|
||
expect(screen.getByText(/enter the email address/i)).toBeInTheDocument(); | ||
|
||
expect( | ||
screen.getByPlaceholderText(/enter your email/i), | ||
).toBeInTheDocument(); | ||
|
||
expect(screen.getByText(/send/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it("shows email error for unregistered email", async () => { | ||
expect.hasAssertions(); | ||
|
||
render(<ForgotPassword />); | ||
|
||
const emailInput = screen.getByPlaceholderText(/enter your email/i); | ||
const sendButton = screen.getByText(/send/i); | ||
|
||
fireEvent.change(emailInput, { | ||
target: { value: "[email protected]" }, | ||
}); | ||
fireEvent.click(sendButton); | ||
|
||
await waitFor(() => { | ||
expect( | ||
screen.getByText(/this email doesn't match our records/i), | ||
).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it("proceeds to verification code stage on valid email", async () => { | ||
expect.hasAssertions(); | ||
|
||
render(<ForgotPassword />); | ||
|
||
const emailInput = screen.getByPlaceholderText(/enter your email/i); | ||
const sendButton = screen.getByText(/send/i); | ||
|
||
fireEvent.change(emailInput, { | ||
target: { value: "[email protected]" }, | ||
}); | ||
fireEvent.click(sendButton); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("Verification Code")).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it("shows error for incorrect OTP", async () => { | ||
expect.hasAssertions(); | ||
|
||
render(<ForgotPassword />); | ||
|
||
const emailInput = screen.getByPlaceholderText(/enter your email/i); | ||
const sendButton = screen.getByText(/send/i); | ||
|
||
fireEvent.change(emailInput, { | ||
target: { value: "[email protected]" }, | ||
}); | ||
fireEvent.click(sendButton); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("Verification Code")).toBeInTheDocument(); | ||
}); | ||
|
||
const otpInput = screen.getByTestId("forgot-password-otp-input"); | ||
fireEvent.change(otpInput, { target: { value: "000000" } }); | ||
|
||
await waitFor(() => { | ||
expect( | ||
screen.getByText(/the otp entered is not correct/i), | ||
).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it("proceeds to reset password stage on correct OTP", async () => { | ||
expect.hasAssertions(); | ||
|
||
render(<ForgotPassword />); | ||
|
||
const emailInput = screen.getByPlaceholderText(/enter your email/i); | ||
const sendButton = screen.getByText(/send/i); | ||
|
||
fireEvent.change(emailInput, { | ||
target: { value: "[email protected]" }, | ||
}); | ||
fireEvent.click(sendButton); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("Verification Code")).toBeInTheDocument(); | ||
}); | ||
|
||
const otpInput = screen.getByTestId("forgot-password-otp-input"); | ||
fireEvent.change(otpInput, { target: { value: "123456" } }); | ||
const verifyButton = screen.getByText(/verify/i); | ||
fireEvent.click(verifyButton); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText(/verification successful/i)).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
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