-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OSDEV-1083 Enable Password Visibility on Chrome Browser (#274)
[OSDEV-1083](https://opensupplyhub.atlassian.net/browse/OSDEV-1083) Enable Password Visibility on Chrome Browser. [OSDEV-1083]: https://opensupplyhub.atlassian.net/browse/OSDEV-1083?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ Implemented 'toggle password visibility' feature in the login, register, reset password and user profile forms. This allows users to view their password while typing, helping to prevent login issues due to misspelled passwords.
- Loading branch information
1 parent
f768013
commit 0e32d22
Showing
8 changed files
with
276 additions
and
47 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
62 changes: 62 additions & 0 deletions
62
src/react/src/__tests__/components/TogglePasswordField.test.js
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,62 @@ | ||
import React from 'react'; | ||
import { screen, fireEvent } from '@testing-library/react'; | ||
import TogglePasswordField from '../../components/TogglePasswordField'; | ||
import renderWithProviders from '../../util/testUtils/renderWithProviders'; | ||
|
||
// Mock the styles and icons | ||
jest.mock('@material-ui/icons', () => ({ | ||
Visibility: () => <div>Visibility</div>, | ||
VisibilityOff: () => <div>VisibilityOff</div>, | ||
})); | ||
|
||
describe('TogglePasswordField', () => { | ||
const defaultProps = { | ||
id: 'password', | ||
value: '', | ||
label: 'Password', | ||
updatePassword: jest.fn(), | ||
submitFormOnEnterKeyPress: jest.fn(), | ||
classes: {}, | ||
}; | ||
|
||
test('renders password field', () => { | ||
renderWithProviders(<TogglePasswordField {...defaultProps} />); | ||
|
||
const passwordInput = screen.getByLabelText('Password'); | ||
|
||
expect(passwordInput).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders input with type "password"', () => { | ||
renderWithProviders(<TogglePasswordField {...defaultProps} />); | ||
|
||
const passwordInput = screen.getByLabelText('Password'); | ||
|
||
expect(passwordInput).toHaveAttribute('type', 'password'); | ||
}); | ||
|
||
test('toggles password visibility', () => { | ||
renderWithProviders(<TogglePasswordField {...defaultProps} />); | ||
|
||
const toggleButton = screen.getByLabelText( | ||
'toggle password visibility', | ||
); | ||
const passwordInput = screen.getByLabelText('Password'); | ||
|
||
expect(passwordInput).toHaveAttribute('type', 'password'); | ||
|
||
fireEvent.click(toggleButton); | ||
expect(passwordInput).toHaveAttribute('type', 'text'); | ||
|
||
fireEvent.click(toggleButton); | ||
expect(passwordInput).toHaveAttribute('type', 'password'); | ||
}); | ||
|
||
test('updates password value', async () => { | ||
renderWithProviders(<TogglePasswordField {...defaultProps} value="newPassword" />); | ||
|
||
const passwordInput = screen.getByLabelText('Password'); | ||
|
||
expect(passwordInput).toHaveValue('newPassword') | ||
}); | ||
}); |
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,89 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { func, objectOf, string } from 'prop-types'; | ||
import { connect } from 'react-redux'; | ||
import InputLabel from '@material-ui/core/InputLabel'; | ||
import InputBase from '@material-ui/core/InputBase'; | ||
import InputAdornment from '@material-ui/core/InputAdornment'; | ||
import IconButton from '@material-ui/core/IconButton'; | ||
import Visibility from '@material-ui/icons/VisibilityOutlined'; | ||
import VisibilityOff from '@material-ui/icons/VisibilityOffOutlined'; | ||
import { withStyles } from '@material-ui/core/styles'; | ||
|
||
import { togglePasswordFieldStyles } from '../util/styles'; | ||
|
||
function TogglePasswordField({ | ||
id, | ||
value, | ||
label, | ||
updatePassword, | ||
submitFormOnEnterKeyPress, | ||
classes, | ||
children, | ||
}) { | ||
const [showPassword, setShowPassword] = useState(false); | ||
|
||
const handleClickShowPassword = () => { | ||
setShowPassword(prevShowPassword => !prevShowPassword); | ||
}; | ||
|
||
const handleMouseDownPassword = event => { | ||
event.preventDefault(); | ||
}; | ||
|
||
return ( | ||
<div className="form__field"> | ||
<InputLabel className={classes.label} htmlFor={id}> | ||
{label} | ||
{children || null} | ||
</InputLabel> | ||
<InputBase | ||
className={classes.wrapper} | ||
id={id} | ||
type={showPassword ? 'text' : 'password'} | ||
value={value} | ||
onChange={updatePassword} | ||
onKeyPress={submitFormOnEnterKeyPress} | ||
fullWidth | ||
classes={{ | ||
inputType: classes.inputType, | ||
input: classes.input, | ||
focused: classes.inputFocused, | ||
}} | ||
endAdornment={ | ||
<InputAdornment | ||
position="end" | ||
classes={{ | ||
root: classes.adornment, | ||
positionEnd: classes.adornmentPositionEnd, | ||
}} | ||
> | ||
<IconButton | ||
aria-label="toggle password visibility" | ||
onClick={handleClickShowPassword} | ||
onMouseDown={handleMouseDownPassword} | ||
classes={{ | ||
root: classes.button, | ||
}} | ||
> | ||
{showPassword ? <Visibility /> : <VisibilityOff />} | ||
</IconButton> | ||
</InputAdornment> | ||
} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
TogglePasswordField.propTypes = { | ||
id: string.isRequired, | ||
value: string.isRequired, | ||
label: string.isRequired, | ||
updatePassword: func.isRequired, | ||
submitFormOnEnterKeyPress: func.isRequired, | ||
classes: objectOf(string).isRequired, | ||
}; | ||
|
||
export default connect()( | ||
withStyles(togglePasswordFieldStyles)(TogglePasswordField), | ||
); |
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