-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(component): refactor Chip component tests (#653)
- Loading branch information
1 parent
6031a5e
commit 6479028
Showing
1 changed file
with
18 additions
and
14 deletions.
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 |
---|---|---|
@@ -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'); | ||
}); |