-
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?
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
components/Avatar/Avatar.test.tsx
Outdated
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
components/Avatar/Avatar.test.tsx
Outdated
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
components/Avatar/Avatar.test.tsx
Outdated
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the optional ? on the alt?. This property will always be included in both the AvatarImage and AvatarFallback.
components/Avatar/Avatar.tsx
Outdated
| return ( | ||
| <AvatarBody> | ||
| <AvatarImage src={userAvatar} alt="" /> | ||
| <AvatarImage src={userAvatar} alt="user avatar" /> |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
| <Avatar userAvatar={avatar} /> |
| <Avatar data-testid="avatar" userAvatar={profile?.avatar} /> |
| <Avatar userAvatar={member.member.avatar} /> |
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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.
components/Avatar/Avatar.tsx
Outdated
| return ( | ||
| <AvatarBody> | ||
| <AvatarImage src={userAvatar} alt="" /> | ||
| <AvatarImage src={userAvatar} alt="user avatar" /> |
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.
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.
Choose a reason for hiding this comment
The 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?
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the fallback component show here?
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.
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.
Choose a reason for hiding this comment
The 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.
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.
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?
components/Avatar/Avatar.test.tsx
Outdated
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow how we're doing the other components by adding a data-testid attribute.
nickytonline
left a comment
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.
🚀
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.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
|
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