Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion __tests__/components/login-form.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cleanup, fireEvent, render, screen } from '@/lib/test-utils';
import { cleanup, fireEvent, render, screen, waitFor } from '@/lib/test-utils';

import { LoginForm } from '../../src/components/login-form';

Expand Down Expand Up @@ -35,4 +35,34 @@ describe('LoginForm Form ', () => {
expect(screen.queryByText(/Email is required/i)).not.toBeOnTheScreen();
expect(await screen.findByText(/Invalid Email Format/i)).toBeOnTheScreen();
});

it('should display required error when password is cleared', async () => {
render(<LoginForm />);

const passwordInput = screen.getByTestId('password-input');
fireEvent.changeText(screen.getByTestId('email-input'), '[email protected]');
fireEvent.changeText(passwordInput, '123');
fireEvent.changeText(passwordInput, '');
fireEvent.press(screen.getByTestId(LOGIN_BUTTON));

expect(await screen.findByText(/Password is required/i)).toBeOnTheScreen();
});

it('should submit with a short password without a length error', async () => {
const onSubmit = jest.fn();
render(<LoginForm onSubmit={onSubmit} />);

fireEvent.changeText(screen.getByTestId('email-input'), '[email protected]');
fireEvent.changeText(screen.getByTestId('password-input'), '123');
fireEvent.press(screen.getByTestId(LOGIN_BUTTON));

await waitFor(() => {
expect(onSubmit).toHaveBeenCalledWith(
{ email: '[email protected]', password: '123' },
undefined,
);
});
expect(screen.queryByText(/at least/i)).not.toBeOnTheScreen();
expect(screen.queryByText(/Password is required/i)).not.toBeOnTheScreen();
});
});
4 changes: 2 additions & 2 deletions docs/src/content/docs/ui-and-theme/Forms.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import z from 'zod';

const schema = z.object({
email: z.string().email(),
password: z.string().min(6),
password: z.string().min(1),
});

type FormType = z.infer<typeof schema>;
Expand All @@ -61,7 +61,7 @@ import { Button, ControlledInput, View } from '@/components/ui';

const schema = z.object({
email: z.string().email(),
password: z.string().min(6),
password: z.string().min(1),
});

type FormType = z.infer<typeof schema>;
Expand Down
3 changes: 1 addition & 2 deletions src/components/login-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import { object, string } from 'zod';
import { Button, ControlledInput, Text, View } from '@/components/ui';
import { translate } from '@/lib';

const MIN_CHARS = 6;
const schema = object({
email: string({
required_error: translate('auth.signIn.validation.emailRequired'),
}).email(translate('auth.signIn.validation.invalidEmail')),
password: string({
required_error: translate('auth.signIn.validation.passwordRequired'),
}).min(MIN_CHARS, translate('auth.signIn.validation.passwordMinChars')),
}).min(1, translate('auth.signIn.validation.passwordRequired')),
Comment thread
fernandatoledo marked this conversation as resolved.
Resolved
});

export type FormType = z.infer<typeof schema>;
Expand Down
1 change: 0 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"validation": {
"emailRequired": "Email is required",
"invalidEmail": "Invalid email format",
"passwordMinChars": "Password must be at least 6 characters",
"passwordRequired": "Password is required"
}
},
Expand Down
Loading