-
Notifications
You must be signed in to change notification settings - Fork 391
chore(Avatar) update tests to new standarts #7829
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 69 additions & 3 deletions
72
packages/react-core/src/components/Avatar/__tests__/Avatar.test.tsx
This file contains hidden or 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,8 +1,74 @@ | ||
| import * as React from 'react'; | ||
| import { render } from '@testing-library/react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import { Avatar } from '../Avatar'; | ||
|
|
||
| test('simple avatar', () => { | ||
| const { asFragment } = render(<Avatar alt="avatar" src="test.png" border="light" />); | ||
| test('Renders simple avatar', () => { | ||
| render( | ||
| <div data-testid="avatar"> | ||
| <Avatar alt="avatar" /> | ||
| </div> | ||
| ); | ||
| expect(screen.getByTestId('avatar').firstChild).toBeVisible(); | ||
| }); | ||
|
|
||
| test('Renders without any modifier class when border and size props are not passed', () => { | ||
| render(<Avatar alt="avatar" />); | ||
| expect(screen.getByRole('img')).toHaveClass('pf-c-avatar', { exact: true }); | ||
| }); | ||
|
|
||
| test('Renders with class name pf-m-light when "light" is passed as border prop', () => { | ||
| render(<Avatar alt="avatar" border="light" />); | ||
| expect(screen.getByRole('img')).toHaveClass('pf-m-light'); | ||
| }); | ||
|
|
||
| test('Renders with class name pf-m-dark when "dark" is passed as border prop', () => { | ||
| render(<Avatar alt="avatar" border="dark" />); | ||
| expect(screen.getByRole('img')).toHaveClass('pf-m-dark'); | ||
| }); | ||
|
|
||
| test('Renders with class name pf-m-xl when "xl" is passed as size prop', () => { | ||
| render(<Avatar alt="avatar" size="xl" />); | ||
| expect(screen.getByRole('img')).toHaveClass('pf-m-xl'); | ||
| }); | ||
|
|
||
| test('Renders with class name pf-m-lg when "lg" is passed as size prop', () => { | ||
| render(<Avatar alt="avatar" size="lg" />); | ||
| expect(screen.getByRole('img')).toHaveClass('pf-m-lg'); | ||
| }); | ||
|
|
||
| test('Renders with class name pf-m-md when "md" is passed as size prop', () => { | ||
| render(<Avatar alt="avatar" size="md" />); | ||
| expect(screen.getByRole('img')).toHaveClass('pf-m-md'); | ||
| }); | ||
|
|
||
| test('Renders with class name pf-m-sm when "sm" is passed as size prop', () => { | ||
| render(<Avatar alt="avatar" size="sm" />); | ||
| expect(screen.getByRole('img')).toHaveClass('pf-m-sm'); | ||
| }); | ||
|
|
||
| test('Renders with custom class name when className prop is passed', () => { | ||
| render(<Avatar alt="avatar" className="test-class" />); | ||
| expect(screen.getByRole('img')).toHaveClass('test-class'); | ||
| }); | ||
|
|
||
| test('Renders with passed src prop', () => { | ||
| render(<Avatar alt="avatar" src="test.png" />); | ||
| const image = screen.getByRole('img') as HTMLImageElement; | ||
| expect(image.src).toMatch('test.png'); | ||
|
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. I think this was a great way of testing that |
||
| }); | ||
|
|
||
| test('Renders with passed alt prop', () => { | ||
| render(<Avatar alt="avatar" />); | ||
| expect(screen.getByRole('img')).toHaveProperty('alt', 'avatar'); | ||
| }); | ||
|
|
||
| test('Renders with passed aria-label prop', () => { | ||
| render(<Avatar alt="avatar" aria-label="Avatar test" />); | ||
| expect(screen.getByRole('img')).toHaveAccessibleName('Avatar test'); | ||
| }); | ||
|
|
||
| test('Matches the snapshot', () => { | ||
| const { asFragment } = render(<Avatar alt="avatar" aria-label="Avatar test" src="test.png" className="test-class" />); | ||
|
|
||
| expect(asFragment()).toMatchSnapshot(); | ||
| }); | ||
5 changes: 3 additions & 2 deletions
5
packages/react-core/src/components/Avatar/__tests__/__snapshots__/Avatar.test.tsx.snap
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Additional things I would like to see tested:
pf-c-avatarclassNameprop functionssrcprop functionsaltprop functionsaria-labelin this case) can be applied as expectedThere 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 one note: typically we want each test to ideally only include props that are either required or under test, so a lot of the props in use in the tests you have here can be removed.
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.
Hopefully all the required changes have been resolved. Thanks for the feedback. I am not quite sure about the way the src test is carried out though, let me know what you think.