Skip to content

Commit 24d2b23

Browse files
committed
chore(tests): update tests
revert email input field
1 parent afc1462 commit 24d2b23

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

src/api/auth/use-login.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createMutation } from 'react-query-kit';
33
import { client } from '../common';
44

55
type Variables = {
6+
email?: string; // optional because API doesn't require email
67
username: string;
78
password: string;
89
expiresInMins?: number;
@@ -20,7 +21,10 @@ const login = async (variables: Variables) => {
2021
const { data } = await client({
2122
url: 'auth/login',
2223
method: 'POST',
23-
data: variables,
24+
data: {
25+
username: variables.username,
26+
password: variables.password,
27+
},
2428
headers: {
2529
'Content-Type': 'application/json',
2630
},

src/components/login-form.test.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ describe('LoginForm Form ', () => {
1818
render(<LoginForm />);
1919

2020
const button = screen.getByTestId(LOGIN_BUTTON);
21-
expect(screen.queryByText(/Email is required/i)).not.toBeOnTheScreen();
21+
expect(screen.queryByText(/Username is required/i)).not.toBeOnTheScreen();
2222
fireEvent.press(button);
23-
expect(await screen.findByText(/Email is required/i)).toBeOnTheScreen();
23+
expect(await screen.findByText(/Username is required/i)).toBeOnTheScreen();
2424
expect(screen.getByText(/Password is required/i)).toBeOnTheScreen();
2525
});
2626

@@ -29,24 +29,26 @@ describe('LoginForm Form ', () => {
2929

3030
const button = screen.getByTestId(LOGIN_BUTTON);
3131
const emailInput = screen.getByTestId('email-input');
32+
const usernameInput = screen.getByTestId('username-input');
3233
const passwordInput = screen.getByTestId('password-input');
3334

34-
fireEvent.changeText(emailInput, 'yyyyy');
35+
fireEvent.changeText(emailInput, 'yyyy');
36+
fireEvent.changeText(usernameInput, ' ');
3537
fireEvent.changeText(passwordInput, 'test');
3638
fireEvent.press(button);
3739

38-
expect(screen.queryByText(/Email is required/i)).not.toBeOnTheScreen();
40+
expect(screen.queryByText(/Username is required/i)).not.toBeOnTheScreen();
3941
expect(await screen.findByText(/Invalid Email Format/i)).toBeOnTheScreen();
4042
});
4143

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

4547
const button = screen.getByTestId(LOGIN_BUTTON);
46-
const emailInput = screen.getByTestId('email-input');
48+
const emailInput = screen.getByTestId('username-input');
4749
const passwordInput = screen.getByTestId('password-input');
4850

49-
fireEvent.changeText(emailInput, 'youssef@gmail.com');
51+
fireEvent.changeText(emailInput, 'youssef');
5052
fireEvent.changeText(passwordInput, 'password');
5153
fireEvent.press(button);
5254
await waitFor(() => {
@@ -55,10 +57,11 @@ describe('LoginForm Form ', () => {
5557
// undefined because we don't use second argument of the SubmitHandler
5658
expect(onSubmitMock).toHaveBeenCalledWith(
5759
{
58-
60+
email: undefined,
61+
username: 'youssef',
5962
password: 'password',
6063
},
61-
undefined
64+
undefined,
6265
);
6366
});
6467
});

src/components/login-form.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import { Button, ControlledInput, Text, View } from '@/ui';
88

99
const MIN_CHARS = 6;
1010
const schema = z.object({
11-
username: z.string(),
11+
username: z.string({
12+
required_error: 'Username is required',
13+
}),
14+
email: z.string().email('Invalid email format').optional(),
1215
password: z
1316
.string({
1417
required_error: 'Password is required',
@@ -38,7 +41,13 @@ export const LoginForm = ({ onSubmit = () => {} }: LoginFormProps) => {
3841
</Text>
3942

4043
<ControlledInput
41-
testID="username"
44+
testID="email-input"
45+
control={control}
46+
name="email"
47+
label="Email (optional)"
48+
/>
49+
<ControlledInput
50+
testID="username-input"
4251
control={control}
4352
name="username"
4453
label="Username"

0 commit comments

Comments
 (0)