Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat : Add unit tests on FormFields if isDisabled #515

Merged
merged 2 commits into from
Aug 2, 2024
Merged
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
38 changes: 38 additions & 0 deletions src/components/Form/FieldCheckboxes/FieldCheckboxes.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,41 @@ test('default value', async () => {
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ colors: ['green', 'blue'] });
});

test('disabled', async () => {
const user = setupUser();
const mockedSubmit = vi.fn();
render(
<FormMocked
schema={z.object({ colors: z.enum(['red', 'green', 'blue']).array() })}
useFormOptions={{
defaultValues: {
colors: ['green', 'blue'],
},
}}
onSubmit={mockedSubmit}
>
{({ form }) => (
<FormField>
<FormFieldLabel>Colors</FormFieldLabel>
<FormFieldController
type="checkboxes"
control={form.control}
name="colors"
isDisabled
options={options}
/>
</FormField>
)}
</FormMocked>
);

await user.click(screen.getByLabelText('Green'));

expect(screen.getByLabelText<HTMLInputElement>('Green').checked).toBe(true);
expect(screen.getByLabelText<HTMLInputElement>('Blue').checked).toBe(true);
expect(screen.getByLabelText<HTMLInputElement>('Red').checked).toBe(false);

await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ colors: ['green', 'blue'] });
});
29 changes: 29 additions & 0 deletions src/components/Form/FieldCurrency/FieldCurrency.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,32 @@ test('default value', async () => {
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ balance: 12 });
});

test('disabled', async () => {
const user = setupUser();
const mockedSubmit = vi.fn();

render(
<FormMocked
schema={z.object({ balance: z.number() })}
useFormOptions={{ defaultValues: { balance: 12 } }}
onSubmit={mockedSubmit}
>
{({ form }) => (
<FormField>
<FormFieldLabel>Balance</FormFieldLabel>
<FormFieldController
type="currency"
control={form.control}
name="balance"
isDisabled
/>
</FormField>
)}
</FormMocked>
);
const input = screen.getByLabelText<HTMLInputElement>('Balance');
await user.type(input, '10.00');
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ balance: 12 });
});
36 changes: 36 additions & 0 deletions src/components/Form/FieldDate/FieldDate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,39 @@ test('default value', async () => {
date: dayjs('01/01/2000').toDate(),
});
});

test('disabled', async () => {
const user = setupUser();
const mockedSubmit = vi.fn();
render(
<FormMocked
schema={z.object({ date: z.date() })}
useFormOptions={{
defaultValues: {
date: dayjs('01/01/2000').toDate(),
},
}}
onSubmit={mockedSubmit}
>
{({ form }) => (
<FormField>
<FormFieldLabel>Date</FormFieldLabel>
<FormFieldController
type="date"
control={form.control}
name="date"
isDisabled
/>
</FormField>
)}
</FormMocked>
);

const input = screen.getByLabelText('Date');
await user.type(input, '');
await user.tab();
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({
date: dayjs('01/01/2000').toDate(),
});
});
36 changes: 36 additions & 0 deletions src/components/Form/FieldMultiSelect/FieldMultiSelect.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,39 @@ test('default value', async () => {
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ colors: ['green', 'blue'] });
});

test('disabled', async () => {
const user = setupUser();
const mockedSubmit = vi.fn();
render(
<FormMocked
schema={z.object({ colors: z.string().array() })}
useFormOptions={{
defaultValues: {
colors: ['green', 'blue'],
},
}}
onSubmit={mockedSubmit}
>
{({ form }) => (
<FormField>
<FormFieldLabel>Colors</FormFieldLabel>
<FormFieldController
type="multi-select"
control={form.control}
name="colors"
isDisabled
options={options}
/>
</FormField>
)}
</FormMocked>
);

const input = screen.getByLabelText<HTMLInputElement>('Colors');
await user.type(input, 'red');
await user.tab();

await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ colors: ['green', 'blue'] });
});
30 changes: 30 additions & 0 deletions src/components/Form/FieldOtp/FieldOtp.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,33 @@ test('auto submit', async () => {
await user.paste('000000');
expect(mockedSubmit).toHaveBeenCalledWith({ code: '000000' });
});

test('disabled', async () => {
const user = setupUser();
const mockedSubmit = vi.fn();

render(
<FormMocked
schema={z.object({ code: z.string().min(6).max(6) })}
useFormOptions={{ defaultValues: { code: '000000' } }}
onSubmit={mockedSubmit}
>
{({ form }) => (
<FormField>
<FormFieldLabel>Code</FormFieldLabel>
<FormFieldController
type="otp"
control={form.control}
name="code"
isDisabled
/>
</FormField>
)}
</FormMocked>
);
const input = screen.getByLabelText<HTMLInputElement>('Code');
await user.click(input);
await user.paste('123456');
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ code: '000000' });
});
29 changes: 29 additions & 0 deletions src/components/Form/FieldPassword/FieldPassword.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,32 @@ test('toggle visibility', async () => {
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ password: 'new value' });
});

test('disabled', async () => {
const user = setupUser();
const mockedSubmit = vi.fn();

render(
<FormMocked
schema={z.object({ password: z.string() })}
useFormOptions={{ defaultValues: { password: 'new value' } }}
onSubmit={mockedSubmit}
>
{({ form }) => (
<FormField>
<FormFieldLabel>Password</FormFieldLabel>
<FormFieldController
type="password"
control={form.control}
name="password"
isDisabled
/>
</FormField>
)}
</FormMocked>
);
const input = screen.getByLabelText<HTMLInputElement>('Password');
await user.type(input, 'another value');
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ password: 'new value' });
});
1 change: 1 addition & 0 deletions src/components/Form/FieldPassword/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const FieldPassword = <
<Flex flexDirection="column" gap={1} flex={1} {...props.containerProps}>
<InputGroup size={props.size}>
<Input
isDisabled={props.isDisabled}
type={showPassword ? 'text' : 'password'}
placeholder={props.placeholder}
autoFocus={props.autoFocus}
Expand Down
38 changes: 38 additions & 0 deletions src/components/Form/FieldRadios/FieldRadios.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,41 @@ test('default value', async () => {
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ color: 'green' });
});

test('disabled', async () => {
const user = setupUser();
const mockedSubmit = vi.fn();
render(
<FormMocked
schema={z.object({ color: z.enum(['red', 'green', 'blue']) })}
useFormOptions={{
defaultValues: {
color: 'green',
},
}}
onSubmit={mockedSubmit}
>
{({ form }) => (
<FormField>
<FormFieldLabel>Color</FormFieldLabel>
<FormFieldController
type="radios"
control={form.control}
name="color"
isDisabled
options={options}
/>
</FormField>
)}
</FormMocked>
);

await user.click(screen.getByLabelText('Blue'));

expect(screen.getByLabelText<HTMLInputElement>('Green').checked).toBe(true);
expect(screen.getByLabelText<HTMLInputElement>('Blue').checked).toBe(false);
expect(screen.getByLabelText<HTMLInputElement>('Red').checked).toBe(false);

await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ color: 'green' });
});
36 changes: 36 additions & 0 deletions src/components/Form/FieldSelect/FieldSelect.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,39 @@ test('default value', async () => {
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ color: 'blue' });
});

test('disabled', async () => {
const user = setupUser();
const mockedSubmit = vi.fn();
render(
<FormMocked
schema={z.object({ color: z.string() })}
useFormOptions={{
defaultValues: {
color: 'blue',
},
}}
onSubmit={mockedSubmit}
>
{({ form }) => (
<FormField>
<FormFieldLabel>Color</FormFieldLabel>
<FormFieldController
type="select"
control={form.control}
name="color"
isDisabled
options={options}
/>
</FormField>
)}
</FormMocked>
);

const input = screen.getByLabelText<HTMLInputElement>('Color');
await user.type(input, 'green');
await user.tab();

await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ color: 'blue' });
});
29 changes: 29 additions & 0 deletions src/components/Form/FieldText/FieldText.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,32 @@ test('default value', async () => {
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ name: 'default value' });
});

test('disabled', async () => {
const user = setupUser();
const mockedSubmit = vi.fn();

render(
<FormMocked
schema={z.object({ name: z.string() })}
useFormOptions={{ defaultValues: { name: 'new value' } }}
onSubmit={mockedSubmit}
>
{({ form }) => (
<FormField>
<FormFieldLabel>Name</FormFieldLabel>
<FormFieldController
type="text"
control={form.control}
name="name"
isDisabled
/>
</FormField>
)}
</FormMocked>
);
const input = screen.getByLabelText<HTMLInputElement>('Name');
await user.type(input, 'another value');
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ name: 'new value' });
});
29 changes: 29 additions & 0 deletions src/components/Form/FieldTextarea/FieldTextarea.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,32 @@ test('default value', async () => {
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ description: 'default value' });
});

test('disabled', async () => {
const user = setupUser();
const mockedSubmit = vi.fn();

render(
<FormMocked
schema={z.object({ description: z.string() })}
useFormOptions={{ defaultValues: { description: 'new value' } }}
onSubmit={mockedSubmit}
>
{({ form }) => (
<FormField>
<FormFieldLabel>Description</FormFieldLabel>
<FormFieldController
type="textarea"
control={form.control}
name="description"
isDisabled
/>
</FormField>
)}
</FormMocked>
);
const input = screen.getByLabelText<HTMLInputElement>('Description');
await user.type(input, 'another value');
await user.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockedSubmit).toHaveBeenCalledWith({ description: 'new value' });
});
Loading