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 Dec 3, 2021
1 parent 6031a5e commit 73a2e22
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions packages/big-design/src/components/Chip/spec.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import 'jest-styled-components';

import { fireEvent, render } from '@test/utils';
import { render } from '@test/utils';

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.getByText(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.getByText(/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.getByRole('button')).toBeInTheDocument();
expect(screen.getByText(/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'));
userEvent.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.getByText(/test/i).parentElement;

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

0 comments on commit 73a2e22

Please sign in to comment.