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
- Loading branch information
Showing
7 changed files
with
517 additions
and
243 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
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
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.