test #174: created unit test for Avatar component#637
test #174: created unit test for Avatar component#637
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| const actual = jest.requireActual('@radix-ui/react-avatar'); | ||
| return ({ | ||
| ...actual, | ||
| Image: ({src, alt, ...props}: {src?: string, alt?: string}) => { |
There was a problem hiding this comment.
Remove ...props from the Image mock. When testing, you should only mock the necessary behavior. For the Avatar component, you're currently only testing for src and alt. The other properties of Image aren't needed.
| return ({ | ||
| ...actual, | ||
| Image: ({src, alt, ...props}: {src?: string, alt?: string}) => { | ||
| return <img data-testid="avatar-image" src={src} alt={alt} {...props}/>; |
There was a problem hiding this comment.
Don't add data-testids inside components used in testing. While you fetched the element by role, which is good, the id still isn't necessary for the mock.
If anything, this id should have been added to the AvatarImage component.
| const actual = jest.requireActual('@radix-ui/react-avatar'); | ||
| return ({ | ||
| ...actual, | ||
| Image: ({src, alt, ...props}: {src?: string, alt?: string}) => { |
There was a problem hiding this comment.
Remove the optional ? on the alt?. This property will always be included in both the AvatarImage and AvatarFallback.
| return ( | ||
| <AvatarBody> | ||
| <AvatarImage src={userAvatar} alt="" /> | ||
| <AvatarImage src={userAvatar} alt="user avatar" /> |
There was a problem hiding this comment.
refactor (a11y): "user avatar" is too generic. If the avatar conveys meaning, add more context, like the username, otherwise leave the alt test to "".
| <AvatarImage src={userAvatar} alt="user avatar" /> | |
| <AvatarImage src={userAvatar} alt=`profile image for user ${username}` /> |
There was a problem hiding this comment.
I think adding more context with the username makes sense. After adding username as a prop:
const Avatar = ({ userAvatar, username }: { userAvatar: string | undefined, username: string })
I believe I would need to update each instance where the Avatar component is used:
I want to make sure this correct and okay to do inside the same PR as I'm starting to change multiple files. For the UserDropdownMenu component it gets more complicated to pass a username as well.
There was a problem hiding this comment.
Also, this is only a unit test task. This should be removed into an A11Y ticket. I want to keep things simple for your first test.
There was a problem hiding this comment.
Thanks, I'll just leave the AvatarImage alt text as it was prior to my edits.
| import Avatar from './Avatar'; | ||
| import { render, screen } from '@testing-library/react'; | ||
|
|
||
| jest.mock('@radix-ui/react-avatar', () => { |
There was a problem hiding this comment.
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.
| return ( | ||
| <AvatarBody> | ||
| <AvatarImage src={userAvatar} alt="" /> | ||
| <AvatarImage src={userAvatar} alt="user avatar" /> |
There was a problem hiding this comment.
Also, this is only a unit test task. This should be removed into an A11Y ticket. I want to keep things simple for your first test.
|
|
||
| }); | ||
|
|
||
| it('Renders the fallback image when src is undefined', () => { |
There was a problem hiding this comment.
What about when there's a string given bug the image is not found?
There was a problem hiding this comment.
That makes sense, I'll add this test scenario.
|
|
||
| describe('Avatar Component', () => { | ||
|
|
||
| it('Renders the component with user image using valid src attribute', () => { |
There was a problem hiding this comment.
Should the fallback component show here?
There was a problem hiding this comment.
Not for this scenario. I wanted to test that the user provided image is correctly added to the document.
There was a problem hiding this comment.
Add an exception to check that fallback doesn't show in this default scenario.
There was a problem hiding this comment.
Interestingly the fallback image is still showing in the document for this scenario. I tried using .not.toBeVisible() method as well as .not.toBeInTheDocument() but both tests fail:
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 screen.debug() and it's showing both elements in the JSDOM. Are both of these supposed to show up like this?
| it('Renders the component with user image using valid src attribute', () => { | ||
| render(<Avatar userAvatar='https://mdbcdn.b-cdn.net/img/new/avatars/2.webp' />); | ||
|
|
||
| const avatarElementWithImage = screen.getByRole('img', { name: 'user avatar'}); |
There was a problem hiding this comment.
Follow how we're doing the other components by adding a data-testid attribute.
|
The unit test for the |
Description
Before:
The
Avatarcomponent lacked unit tests.After:
Unit tests have been added for
Avatarcomponent:AvatarImageto access element by semantic queryCloses #174
Testing instructions
If applicable, provide steps for reviewers to test changes locally -- including necessary setup, commands, and expected results
Additional information
Share any additional info that may provide context for the PR evaluation (performance considerations, design choices, etc)
[optional] Screenshots
Pre-submission checklist
test #001: created unit test for __ component)Peer Code ReviewersandSenior+ Code Reviewersgroupsgis-code-questions