-
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.
feat: avatar component improvements and tests
- Loading branch information
Showing
7 changed files
with
422 additions
and
45 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,39 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import '@testing-library/jest-dom'; | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import Avatar from '../src/components/Avatar/Avatar'; | ||
|
||
describe('Avatar Component', () => { | ||
const props = { | ||
src: '', | ||
alt: 'Test Alt Text', | ||
size: 50, | ||
onClick: jest.fn(), | ||
}; | ||
|
||
it('should render correctly', () => { | ||
const { getByAltText } = render(<Avatar {...props} />); | ||
const avatarImage = getByAltText(props.alt); | ||
|
||
expect(avatarImage).toBeInTheDocument(); | ||
expect(avatarImage).toHaveAttribute('src', props.src); | ||
}); | ||
|
||
it('calls the onClick function when clicked', () => { | ||
const { getByAltText } = render(<Avatar {...props} />); | ||
const avatarImage = getByAltText(props.alt); | ||
fireEvent.click(avatarImage); | ||
expect(props.onClick).toHaveBeenCalled(); | ||
}); | ||
|
||
it('renders the avatar image with the correct size', () => { | ||
const { getByAltText } = render(<Avatar {...props} />); | ||
const avatarImage = getByAltText(props.alt); | ||
expect(avatarImage).toHaveStyle(`width: ${props.size}px`); | ||
expect(avatarImage).toHaveStyle(`height: ${props.size}px`); | ||
}); | ||
}); |
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
37 changes: 31 additions & 6 deletions
37
packages/react-material-ui/src/components/Avatar/Avatar.tsx
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 |
---|---|---|
@@ -1,17 +1,42 @@ | ||
import React, { FC } from 'react'; | ||
import React, { FC, useState } from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import { Image } from './Styles'; | ||
import Text from '../Text'; | ||
|
||
type Props = { | ||
src: string; | ||
alt: string; | ||
size: number; | ||
initials?: string; | ||
onClick?: () => void; | ||
}; | ||
|
||
const Avatar: FC<Props> = (props) => { | ||
const { src, alt, size, onClick } = props; | ||
export const Avatar: FC<Props> = (props) => { | ||
const { src, alt, size, initials, onClick } = props; | ||
const [failed, setFailed] = useState(false); | ||
|
||
return <Image src={src} alt={alt} size={size} onClick={onClick} />; | ||
}; | ||
const handleImageError = () => { | ||
setFailed(true); | ||
}; | ||
|
||
if (failed && initials) { | ||
return ( | ||
<Box sx={{ backgroundColor: 'grey.500' }}> | ||
<Text fontSize={30} fontWeight={800} mt={1} gutterBottom> | ||
{initials} | ||
</Text> | ||
</Box> | ||
); | ||
} | ||
|
||
export default Avatar; | ||
return ( | ||
<Image | ||
src={src} | ||
alt={alt} | ||
size={size} | ||
onClick={onClick} | ||
onError={handleImageError} | ||
style={{ display: failed ? 'none' : 'block' }} | ||
/> | ||
); | ||
}; |
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.