Skip to content

Commit 3368a8a

Browse files
author
Guillermo Machado
committed
feat: add tests
1 parent 67cd4fc commit 3368a8a

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { cleanup, fireEvent, render, screen, waitFor } from '@/core/test-utils';
2+
3+
import {
4+
ForgotPasswordForm,
5+
type ForgotPasswordFormProps,
6+
} from './forgot-password-form';
7+
8+
afterEach(cleanup);
9+
10+
const onSubmitMock: jest.Mock<ForgotPasswordFormProps['onSubmit']> = jest.fn();
11+
12+
describe('ForgotPasswordForm', () => {
13+
const SEND_EMAIL_BUTTON = 'send-email-button';
14+
const EMAIL_INPUT = 'email-input';
15+
16+
it('renders correctly', async () => {
17+
render(<ForgotPasswordForm onSubmit={onSubmitMock} />);
18+
expect(await screen.findByText(/Forgot your password?/i)).toBeOnTheScreen();
19+
});
20+
21+
it('should display error when email is empty', async () => {
22+
render(<ForgotPasswordForm onSubmit={onSubmitMock} />);
23+
const button = screen.getByTestId(SEND_EMAIL_BUTTON);
24+
fireEvent.press(button);
25+
expect(await screen.findByText(/Required/i)).toBeOnTheScreen();
26+
});
27+
28+
it('should display error when email is invalid', async () => {
29+
render(<ForgotPasswordForm onSubmit={onSubmitMock} />);
30+
const emailInput = screen.getByTestId(EMAIL_INPUT);
31+
const button = screen.getByTestId(SEND_EMAIL_BUTTON);
32+
33+
fireEvent.changeText(emailInput, 'invalid-email');
34+
fireEvent.press(button);
35+
36+
expect(await screen.findByText(/Invalid email format/i)).toBeOnTheScreen();
37+
});
38+
39+
it('should call onSubmit with correct values when email is valid', async () => {
40+
render(<ForgotPasswordForm onSubmit={onSubmitMock} />);
41+
const emailInput = screen.getByTestId(EMAIL_INPUT);
42+
const button = screen.getByTestId(SEND_EMAIL_BUTTON);
43+
44+
fireEvent.changeText(emailInput, '[email protected]');
45+
fireEvent.press(button);
46+
47+
await waitFor(() => {
48+
expect(onSubmitMock).toHaveBeenCalledTimes(1);
49+
});
50+
51+
expect(onSubmitMock).toHaveBeenCalledWith({
52+
53+
});
54+
});
55+
});

src/components/forgot-password-form.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ const schema = z.object({
1616

1717
export type FormType = z.infer<typeof schema>;
1818

19-
export const ForgotPasswordForm = (props: {
19+
export type ForgotPasswordFormProps = {
2020
onSubmit: (data: FormType) => void;
21-
}) => {
21+
};
22+
23+
export const ForgotPasswordForm = (props: ForgotPasswordFormProps) => {
2224
const { t } = useTranslation();
2325
const { handleSubmit, control } = useForm<{
2426
email: string;

0 commit comments

Comments
 (0)