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): split component and hooks (#114)
- Loading branch information
Showing
16 changed files
with
787 additions
and
384 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
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,83 @@ | ||
// PasswordVisibilityToggle.test.tsx | ||
import { act, render, fireEvent, waitFor } from '@testing-library/react' | ||
import PasswordVisibilityToggle from './PasswordVisibilityToggle' | ||
|
||
describe('PasswordVisibilityToggle', () => { | ||
let setShowPassword: jest.Mock | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
setShowPassword = jest.fn() | ||
}) | ||
|
||
it('renders without crashing', () => { | ||
const { getByRole } = render( | ||
<PasswordVisibilityToggle | ||
showPassword={false} | ||
setShowPassword={setShowPassword} | ||
/> | ||
) | ||
expect(getByRole('button')).toBeInTheDocument() | ||
}) | ||
|
||
it('changes icon when clicked', async () => { | ||
const { getByRole, rerender } = render( | ||
<PasswordVisibilityToggle | ||
showPassword={false} | ||
setShowPassword={setShowPassword} | ||
/> | ||
) | ||
const button = getByRole('button') | ||
await act(async () => { | ||
fireEvent.click(button) | ||
}) | ||
waitFor(() => { | ||
expect(setShowPassword).toHaveBeenCalledWith(true) | ||
}) | ||
rerender( | ||
<PasswordVisibilityToggle | ||
showPassword={true} | ||
setShowPassword={setShowPassword} | ||
/> | ||
) | ||
await act(async () => { | ||
fireEvent.click(button) | ||
}) | ||
waitFor(() => { | ||
expect(setShowPassword).toHaveBeenCalledWith(false) | ||
}) | ||
}) | ||
|
||
it('calls setShowPassword with correct argument when clicked', async () => { | ||
const { getByRole } = render( | ||
<PasswordVisibilityToggle | ||
showPassword={false} | ||
setShowPassword={setShowPassword} | ||
/> | ||
) | ||
const button = getByRole('button') | ||
await act(async () => { | ||
fireEvent.click(button) | ||
}) | ||
waitFor(() => { | ||
expect(setShowPassword).toHaveBeenCalledWith(true) | ||
}) | ||
}) | ||
|
||
it('prevents default on mouse down', async () => { | ||
const { getByRole } = render( | ||
<PasswordVisibilityToggle | ||
showPassword={false} | ||
setShowPassword={setShowPassword} | ||
/> | ||
) | ||
const button = getByRole('button') | ||
const preventDefault = jest.fn() | ||
await act(async () => { | ||
fireEvent.mouseDown(button, { preventDefault }) | ||
}) | ||
waitFor(() => { | ||
expect(preventDefault).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
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,38 @@ | ||
/** | ||
* @module sbom-harbor-ui/components/PasswordVisibilityToggle.tsx | ||
*/ | ||
import React from 'react' | ||
import IconButton from '@mui/material/IconButton' | ||
import InputAdornment from '@mui/material/InputAdornment' | ||
import VisibilityOutline from '@mui/icons-material/Visibility' | ||
import VisibilityOffIcon from '@mui/icons-material/VisibilityOff' | ||
|
||
interface PasswordVisibilityToggleProps { | ||
showPassword: boolean | ||
setShowPassword: (value: boolean) => void | ||
} | ||
|
||
const PasswordVisibilityToggle: React.FC<PasswordVisibilityToggleProps> = ({ | ||
showPassword, | ||
setShowPassword, | ||
}) => { | ||
const handleClick = React.useCallback( | ||
() => setShowPassword(!showPassword), | ||
[showPassword, setShowPassword] | ||
) | ||
|
||
return ( | ||
<InputAdornment position="end"> | ||
<IconButton | ||
edge="end" | ||
onMouseDown={(e) => e.preventDefault()} | ||
onClick={handleClick} | ||
aria-label={showPassword ? 'Hide password' : 'Show password'} | ||
> | ||
{showPassword ? <VisibilityOutline /> : <VisibilityOffIcon />} | ||
</IconButton> | ||
</InputAdornment> | ||
) | ||
} | ||
|
||
export default PasswordVisibilityToggle |
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,83 @@ | ||
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, unknown> | ||
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 }, | ||
}) => ( | ||
<> | ||
<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`, | ||
...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
Oops, something went wrong.