This repository has been archived by the owner on Sep 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(views/SignIn): add FormControlInput component
- Loading branch information
Showing
12 changed files
with
391 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { useMemo } from 'react' | ||
import { Control, Controller, FieldValues, Path } from 'react-hook-form' | ||
import FormControl, { FormControlProps } from '@mui/material/FormControl' | ||
import FormHelperText, { | ||
FormHelperTextProps, | ||
} from '@mui/material/FormHelperText' | ||
import InputLabel, { InputLabelProps } from '@mui/material/InputLabel' | ||
import OutlinedInput, { OutlinedInputProps } from '@mui/material/OutlinedInput' | ||
import toTitleCase from '@/utils/toTitleCase' | ||
|
||
type InputFormControlProps<TFieldValues extends FieldValues = FieldValues> = { | ||
control: Control<TFieldValues, any> | ||
name: Path<TFieldValues> | ||
label?: string | ||
FormControlProps?: FormControlProps | ||
FormHelperTextProps?: FormHelperTextProps | ||
InputLabelProps?: InputLabelProps | ||
InputProps?: OutlinedInputProps | ||
} | ||
|
||
const InputFormControl = <TFieldValues extends FieldValues>({ | ||
control, | ||
name, | ||
label = toTitleCase(name), | ||
FormControlProps, | ||
FormHelperTextProps, | ||
InputLabelProps, | ||
InputProps, | ||
}: InputFormControlProps<TFieldValues>) => { | ||
const { | ||
slotProps: { input: inputSlotProps = {}, root: rootSlotProps = {} } = {}, | ||
} = InputProps || {} | ||
|
||
return ( | ||
<FormControl fullWidth {...FormControlProps}> | ||
<Controller | ||
control={control} | ||
name={name} | ||
render={({ | ||
field: { value, onBlur, onChange }, | ||
fieldState: { error, invalid }, | ||
}) => ( | ||
<> | ||
<InputLabel | ||
htmlFor={`${name}-input`} | ||
id={`${name}-label`} | ||
{...InputLabelProps} | ||
error={!!error} | ||
> | ||
{label} | ||
</InputLabel> | ||
<OutlinedInput | ||
aria-describedby={error && `${name}-error`} | ||
id={`${name}-input`} | ||
{...InputProps} | ||
error={!!error} | ||
label={label} | ||
value={value} | ||
onBlur={onBlur} | ||
onChange={onChange} | ||
slotProps={{ | ||
root: rootSlotProps, | ||
input: { | ||
'aria-invalid': !!error, | ||
'aria-labelledby': `${name}-label`, | ||
pattern: name || 'text', | ||
...inputSlotProps, | ||
}, | ||
}} | ||
/> | ||
<FormHelperText | ||
id={`${name}-error`} | ||
{...FormHelperTextProps} | ||
error={!!error} | ||
> | ||
{error?.message || ' '} | ||
</FormHelperText> | ||
</> | ||
)} | ||
/> | ||
</FormControl> | ||
) | ||
} | ||
|
||
export default InputFormControl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// src/views/SignIn/useSignIn.test.ts | ||
import { JSXElementConstructor, ReactElement } from 'react' | ||
import { render, screen, waitFor } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
import SignIn from '@/views/SignIn/SignIn' | ||
|
||
function setup( | ||
jsx: ReactElement< | ||
typeof SignIn, | ||
string | JSXElementConstructor<typeof SignIn> | ||
> | ||
) { | ||
return { | ||
user: userEvent.setup(), | ||
...render(jsx), | ||
} | ||
} | ||
|
||
jest.mock('react-router-dom', () => ({ | ||
useNavigate: jest.fn(), | ||
})) | ||
|
||
describe('SignIn', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('should submit correct form data in the SignIn page', async () => { | ||
const mockSubmit = jest.fn() | ||
|
||
jest.mock('react-hook-form', () => ({ | ||
useForm: () => ({ | ||
control: { | ||
email: '', | ||
password: '', | ||
}, | ||
handleSubmit: mockSubmit, | ||
}), | ||
})) | ||
|
||
// Setup to render the SignIn page | ||
const { user } = setup(<SignIn />) | ||
|
||
// Type the email into the email field | ||
await user.type( | ||
screen.getByRole('textbox', { name: /email/i }), | ||
'[email protected]' | ||
) | ||
|
||
// Type the password into the password field | ||
await user.type( | ||
screen.getByRole('textbox', { name: /password/i }), | ||
'password' | ||
) | ||
|
||
// Save the form | ||
await user.click(screen.getByRole('button', { name: /^login$/i })) | ||
|
||
// Wait for form submission to complete | ||
waitFor(() => { | ||
expect(mockSubmit).toHaveBeenCalledWith({ | ||
email: '[email protected]', | ||
password: 'password', | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.