import { beforeEach, describe, expect, it, vi } from 'vitest'; import { fireEvent, render, screen } from '@testing-library/svelte'; import userEvent from '@testing-library/user-event'; import Pill from '../pill.svelte'; import { cxTestArguments, cxTestResults } from './cx-test'; describe('Pill', () => { let user: ReturnType; beforeEach(() => { user = userEvent.setup(); }); it('Renders text within the pill if a value attribute is specified', () => { render(Pill, { value: 'test' }); expect(screen.getByText('test')).toBeVisible(); }); it('Renders a removable pill', () => { render(Pill, { value: 'test' }); expect(screen.getByLabelText('Remove test')).toBeVisible(); }); it('Renders a pill that is readonly if a readonly attribute of true has been specified', () => { render(Pill, { value: 'test', readonly: true }); expect(screen.queryByRole('button')).not.toBeInTheDocument(); }); it('Renders a pill that is not readonly if a readonly attribute of false has been specified', () => { render(Pill, { value: 'test', readonly: false }); expect(screen.getByLabelText('Remove test')).toBeVisible(); }); it('Renders a normal pill if no disabled attribute has been specified', () => { render(Pill, { value: 'test' }); expect(screen.getByTestId('pill-test')).not.toHaveAttribute( 'aria-disabled' ); }); it('Renders a disabled pill if a disabled attribute of true has been specified', () => { render(Pill, { value: 'test', disabled: true }); expect(screen.getByTestId('pill-test')).toHaveAttribute( 'aria-disabled', 'true' ); }); it('Renders a normal pill if a disabled attribute of false has been specified', () => { render(Pill, { value: 'test', disabled: false }); expect(screen.getByTestId('pill-test')).not.toHaveAttribute( 'aria-disabled' ); }); it('Confirms default pill is clickable', async () => { const onClick = vi.fn(); render(Pill, { value: '', onRemove: onClick }); await fireEvent.click(screen.getByRole('button')); expect(onClick).toHaveBeenCalledOnce(); expect(onClick).toHaveBeenCalledWith(''); }); it('Confirms disabled pill is not clickable', async () => { const onClick = vi.fn(); render(Pill, { value: 'test', disabled: true, onRemove: onClick, }); await fireEvent.click(screen.getByRole('button')); expect(onClick).not.toHaveBeenCalled(); }); it('Renders with the passed cx classes', () => { render(Pill, { value: 'test', cx: cxTestArguments }); expect(screen.getByTestId('pill-test')).toHaveClass(cxTestResults); }); it('Renders with the passed iconCx classes', () => { render(Pill, { value: 'test', icon: 'cog', iconCx: cxTestArguments }); expect(screen.getByTestId('icon-cog')).toHaveClass(cxTestResults); }); it('Renders with the passed href', () => { render(Pill, { value: 'link', href: 'https://www.viam.com' }); const anchor = screen.getByRole('link', { name: 'link' }); expect(anchor).toHaveAttribute('href', 'https://www.viam.com'); expect(anchor).not.toHaveAttribute('target'); }); it('May set anchor target', () => { render(Pill, { props: { value: 'link', href: 'https://www.viam.com', target: '_blank' }, }); const anchor = screen.getByRole('link', { name: 'link' }); expect(anchor).toHaveAttribute('href', 'https://www.viam.com'); expect(anchor).toHaveAttribute('target', '_blank'); }); it('Renders icon tooltip when hovered', async () => { render(Pill, { value: 'test', icon: 'cog', iconTooltip: 'Live' }); const icon = screen.getByTestId('icon-cog'); await user.hover(icon); const tooltip = screen.getByRole('tooltip'); expect(tooltip).not.toHaveClass('hidden'); expect(tooltip).toHaveTextContent('Live'); }); });