Skip to content
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
merged 3 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions components/lib/chip/Chip.spec.js
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);
Copy link
Member

@melloware melloware Nov 2, 2022

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.

// Arrange
const removeOn = jest.fn();
const { container } = render(<Chip removable onRemove={removeOn} />);
const chipRemoveIcon = container.getElementsByClassName('p-chip-remove-icon')[0];

// Act
fireEvent.click(chipRemoveIcon);

// Assert
expect(container.getElementsByClassName('p-chip-remove-icon pi pi-times-circle').length).toBe(0);
expect(removeOn).toHaveBeenCalledTimes(1);

only because it forces developers to properly segment and think through the test

Copy link
Contributor

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});

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');
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a test for the imageAlt property of the Chip as well.


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');
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@types/react-dom": "^18.0.6",
"@types/react-transition-group": "^4.4.5",
"@typescript-eslint/eslint-plugin": "^5.42.0",
"@testing-library/user-event": "^14.4.3",
"eslint": "8.26.0",
"eslint-config-next": "13.0.0",
"eslint-config-prettier": "^8.5.0",
Expand Down