Skip to content

Commit

Permalink
fix: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillermo Machado committed Jan 10, 2025
1 parent 5fc7584 commit c5f0875
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 34 deletions.
37 changes: 4 additions & 33 deletions src/components/login-form.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { cleanup, fireEvent, render, screen, waitFor } from '@/core/test-utils';
import { cleanup, fireEvent, render, screen } from '@/core/test-utils';

import type { LoginFormProps } from './login-form';
import { LoginForm } from './login-form';

afterEach(cleanup);

const onSubmitMock: jest.Mock<LoginFormProps['onSubmit']> = jest.fn();

describe('LoginForm Form ', () => {
const LOGIN_BUTTON = 'login-button';
it('renders correctly', async () => {
Expand All @@ -18,9 +15,9 @@ describe('LoginForm Form ', () => {
render(<LoginForm />);

const button = screen.getByTestId(LOGIN_BUTTON);
expect(screen.queryByText(/Username is required/i)).not.toBeOnTheScreen();
expect(screen.queryByText(/Email is required/i)).not.toBeOnTheScreen();
fireEvent.press(button);
expect(await screen.findByText(/Username is required/i)).toBeOnTheScreen();
expect(await screen.findByText(/Email is required/i)).toBeOnTheScreen();
expect(screen.getByText(/Password is required/i)).toBeOnTheScreen();
});

Expand All @@ -29,39 +26,13 @@ describe('LoginForm Form ', () => {

const button = screen.getByTestId(LOGIN_BUTTON);
const emailInput = screen.getByTestId('email-input');
const usernameInput = screen.getByTestId('username-input');
const passwordInput = screen.getByTestId('password-input');

fireEvent.changeText(emailInput, 'yyyy');
fireEvent.changeText(usernameInput, ' ');
fireEvent.changeText(passwordInput, 'test');
fireEvent.press(button);

expect(screen.queryByText(/Username is required/i)).not.toBeOnTheScreen();
expect(screen.queryByText(/Email is required/i)).not.toBeOnTheScreen();
expect(await screen.findByText(/Invalid Email Format/i)).toBeOnTheScreen();
});

it('Should call LoginForm with correct values when values are valid', async () => {
render(<LoginForm onSubmit={onSubmitMock} />);

const button = screen.getByTestId(LOGIN_BUTTON);
const emailInput = screen.getByTestId('username-input');
const passwordInput = screen.getByTestId('password-input');

fireEvent.changeText(emailInput, 'youssef');
fireEvent.changeText(passwordInput, 'password');
fireEvent.press(button);
await waitFor(() => {
expect(onSubmitMock).toHaveBeenCalledTimes(1);
});
// undefined because we don't use second argument of the SubmitHandler
expect(onSubmitMock).toHaveBeenCalledWith(
{
email: undefined,
username: 'youssef',
password: 'password',
},
undefined,
);
});
});
4 changes: 3 additions & 1 deletion src/components/login-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { Button, ControlledInput, Text, View } from '@/ui';

const MIN_CHARS = 6;
const schema = z.object({
email: z.string().email('Invalid email format'),
email: z
.string({ required_error: 'Email is required' })
.email('Invalid email format'),
password: z
.string({
required_error: 'Password is required',
Expand Down

0 comments on commit c5f0875

Please sign in to comment.