Skip to content

Commit

Permalink
test: increase test coverage to 100% for Tag (carbon-design-system#17614
Browse files Browse the repository at this point in the history
)

* test: increase test coverage to 100% for Tag

* refactor: spyOn updated

* refactor: removed unused import

---------

Co-authored-by: Preeti Bansal <[email protected]>
  • Loading branch information
2nikhiltom and preetibansalui authored Oct 9, 2024
1 parent a4e4668 commit b44a218
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions packages/react/src/components/Tag/Tag-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import React from 'react';
import Tag, { TagSkeleton } from './';
import DismissibleTag from './DismissibleTag';
import { AILabel } from '../AILabel';
import { Asleep } from '@carbon/icons-react';

const prefix = 'cds';

Expand Down Expand Up @@ -83,4 +84,76 @@ describe('Tag', () => {
expect(skeletonTag).toHaveClass(`${prefix}--layout--size-sm`);
});
});

it('should render with different types', () => {
const types = [
'red',
'magenta',
'purple',
'blue',
'cyan',
'teal',
'green',
'gray',
'cool-gray',
'warm-gray',
'high-contrast',
'outline',
];

types.forEach((type) => {
const { container } = render(<Tag type={type}>Tag content</Tag>);
expect(container.firstChild).toHaveClass(`${prefix}--tag--${type}`);
});
});

it('should render with custom className', () => {
const { container } = render(<Tag className="some-class">Tag content</Tag>);
expect(container.firstChild).toHaveClass('some-class');
});

it('should render with icon', () => {
render(<Tag renderIcon={Asleep}>Tag content</Tag>);
expect(screen.getByText('Tag content')).toBeInTheDocument();
expect(document.querySelector('svg')).toBeInTheDocument();
});

it('should render as a filter tag', () => {
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {});
const { container } = render(<Tag filter>Tag content</Tag>);
expect(container.firstChild).toHaveClass(`${prefix}--tag--filter`);
expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

it('should render with different sizes', () => {
const sizes = ['sm', 'md', 'lg'];

sizes.forEach((size) => {
const { container } = render(<Tag size={size}>Tag content</Tag>);
expect(container.firstChild).toHaveClass(
`${prefix}--tag ${prefix}--tag--${size} ${prefix}--layout--size-${size}`
);
});
});

it('should render as disabled', () => {
const { container } = render(<Tag disabled>Disabled Tag</Tag>);
expect(container.firstChild).toHaveClass(`${prefix}--tag--disabled`);
});

it('should handle close button click', () => {
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {});
const mockOnClose = jest.fn();
render(
<Tag type="red" filter onClose={mockOnClose} title="Close tag">
onClose
</Tag>
);
const closeButton = screen.getByTitle('Close tag');
closeButton.click();
expect(mockOnClose).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalled();
spy.mockRestore();
});
});

0 comments on commit b44a218

Please sign in to comment.