-
Notifications
You must be signed in to change notification settings - Fork 4
test #174: created unit test for Avatar component #637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 8 commits
a13cac1
b3f2662
2b12edc
d87c46f
d7931ee
009fece
faafeea
82ab88f
bf87659
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import Avatar from './Avatar'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import { type ComponentProps } from 'react'; | ||
|
|
||
| jest.mock('@radix-ui/react-avatar', () => { | ||
| const actual = jest.requireActual('@radix-ui/react-avatar'); | ||
| return ({ | ||
| ...actual, | ||
| Image: (props: ComponentProps<'img'>) => { | ||
| return <img {...props}/>; | ||
| } | ||
| }) | ||
| }); | ||
|
|
||
| describe('Avatar Component', () => { | ||
|
|
||
| it('Renders the component with user image using valid src attribute', () => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the fallback component show here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not for this scenario. I wanted to test that the user provided image is correctly added to the document.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add an exception to check that fallback doesn't show in this default scenario.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interestingly the fallback image is still showing in the document for this scenario. I tried using 1st attempt: + const avatarElementFallbackImage = screen.getByTestId('avatar-fallback')
+ expect(avatarElementFallbackImage).not.toBeVisible();2nd attempt: + const avatarElementFallbackImage = screen.getByTestId('avatar-fallback')
+ expect(avatarElementFallbackImage).not.toBeInTheDocument();I tried looking at
|
||
| render(<Avatar userAvatar='https://mdbcdn.b-cdn.net/img/new/avatars/2.webp' />); | ||
|
|
||
| const avatarElementWithImage = screen.getByTestId('avatar-image') | ||
| expect(avatarElementWithImage).toBeInTheDocument(); | ||
| expect(avatarElementWithImage).toHaveAttribute( | ||
| 'src', | ||
| 'https://mdbcdn.b-cdn.net/img/new/avatars/2.webp', | ||
| ); | ||
|
|
||
| }); | ||
|
|
||
| it('Renders the fallback image when src is undefined', () => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about when there's a string given bug the image is not found?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense, I'll add this test scenario. |
||
| render(<Avatar userAvatar={undefined} />); | ||
|
|
||
| const avatarElementFallbackImage = screen.getByTestId('avatar-fallback') | ||
| expect(avatarElementFallbackImage).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('Renders the fallback image when an invalid src is provided', () => { | ||
| render(<Avatar userAvatar='https://mdbcdn.b-cdn.net/img/new/avatars/bad-img-url.webp' />); | ||
|
|
||
| const avatarElementFallbackImage = screen.getByTestId('avatar-fallback') | ||
| expect(avatarElementFallbackImage).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| }); | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just FYI for anyone coming across this. My first instinct was to ask why are we mocking this as you always want to mock as little as possible or not at all so we are testing the actual implementation.
In this case, we mock Radix’s Avatar.Image because in JSDOM, what we use in Jest tests, image load events don’t fire as it's not a real browser environment, so only the fallback is ever shown in tests if we don't mock.