Skip to content

Commit

Permalink
test(component): refactor Chip component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-alexsaiannyi committed Nov 22, 2021
1 parent 3c9ac79 commit e303950
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions packages/big-design/src/components/Chip/spec.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { screen } from '@testing-library/react';
import React from 'react';
import 'jest-styled-components';

Expand All @@ -7,38 +8,40 @@ import { Chip } from './index';

test('renders the label', () => {
const label = 'Test';
const { queryByText } = render(<Chip label={label} />);

expect(queryByText(label)).toBeInTheDocument();
render(<Chip label={label} />);

expect(screen.queryByText(label)).toBeInTheDocument();
});

test('renders without close button', () => {
const { container, queryByRole } = render(<Chip label="Test" />);
render(<Chip label="Test" />);

expect(queryByRole('button')).not.toBeInTheDocument();
expect(container.firstChild).toMatchSnapshot();
expect(screen.queryByRole('button')).not.toBeInTheDocument();
expect(screen.queryByText(/test/i)?.parentElement).toMatchSnapshot();
});

test('renders with close button if onRemove is present', () => {
const { container, queryByRole } = render(<Chip label="Test" onDelete={jest.fn()} />);
render(<Chip label="Test" onDelete={jest.fn()} />);

expect(queryByRole('button')).toBeInTheDocument();
expect(container.firstChild).toMatchSnapshot();
expect(screen.queryByRole('button')).toBeInTheDocument();
expect(screen.queryByText(/test/i)?.parentElement).toMatchSnapshot();
});

test('onDelete is called when close button is clicked', () => {
const onDelete = jest.fn();

const { getByRole } = render(<Chip label="Test" onDelete={onDelete} />);
render(<Chip label="Test" onDelete={onDelete} />);

fireEvent.click(getByRole('button'));
fireEvent.click(screen.getByRole('button'));

expect(onDelete).toHaveBeenCalled();
});

test('accepts custom margin props', () => {
const { container } = render(<Chip label="Test" margin="large" onDelete={jest.fn()} />);
const chip = container.firstChild;
render(<Chip label="Test" margin="large" onDelete={jest.fn()} />);

const chipParent = screen.queryByText(/test/i)?.parentElement;

expect(chip).toHaveStyle('margin: 1.25rem');
expect(chipParent).toHaveStyle('margin: 1.25rem');
});

0 comments on commit e303950

Please sign in to comment.