-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add jest unit testing to Chip component #3558
Merged
+45
−0
Merged
Changes from all commits
Commits
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
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,44 @@ | ||
import { fireEvent, render, within } from '@testing-library/react'; | ||
import { Chip } from './Chip'; | ||
|
||
import '@testing-library/jest-dom'; | ||
|
||
describe('Chip', () => { | ||
test('when removable is true it returns with remove icon', () => { | ||
const removeOn = jest.fn(); | ||
const { container } = render(<Chip removable onRemove={removeOn} />); | ||
const chipRemoveIcon = container.getElementsByClassName('p-chip-remove-icon')[0]; | ||
|
||
fireEvent.click(chipRemoveIcon); | ||
|
||
expect(container.getElementsByClassName('p-chip-remove-icon pi pi-times-circle').length).toBe(0); | ||
expect(removeOn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('when image is property it returns with image class', () => { | ||
const { container } = render(<Chip image={'test'} />); | ||
|
||
const wrapper = container.getElementsByClassName('p-chip p-component p-chip-image'); | ||
const chipImage = within(wrapper[0]).getByRole('img'); | ||
|
||
expect(chipImage.getAttribute('alt')).toBe('chip'); | ||
}); | ||
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. We should add a test for the |
||
|
||
test('when icon is property it returns with icon class', () => { | ||
const { container } = render(<Chip icon={'pi pi-check'} />); | ||
|
||
const icon = container.getElementsByClassName('p-chip-icon'); | ||
const iconClass = icon[0].getAttribute('class'); | ||
|
||
expect(iconClass).toBe('p-chip-icon pi pi-check'); | ||
}); | ||
|
||
test('when label is property it returns with label class', () => { | ||
const { container } = render(<Chip label={'test'} />); | ||
|
||
const label = container.getElementsByClassName('p-chip-text'); | ||
const spanTextContent = label[0].textContent; | ||
|
||
expect(spanTextContent).toBe('test'); | ||
}); | ||
}); |
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
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.
We can decide as a team or @mertsincan can decide but I am big fan of "arrange act assert" so something like this.
only because it forces developers to properly segment and think through the 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.
Hi, Testing library is a legit library. Buğra makes this understandable by using the right spaces. Reviews provide that too. With all due respect, I don't believe this is necessary. I wonder why you think it's necessary.
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.
@bahadirsofuoglu it is a very common pattern in unit testing in C#/Java etc.
Here is a great explanation: https://docs.telerik.com/devtools/justmock/basic-usage/arrange-act-assert#:~:text=Benefits%20of%20Using%20Arrange%20Act%20Assert&text=Clarifies%20and%20focuses%20attention%20on,many%20different%20things%20at%20once.