import { describe, expect, it, vi } from 'vitest'; import { fireEvent, render, screen } from '@testing-library/svelte'; import { cxTestArguments, cxTestResults } from '../../__tests__/cx-test'; import IconButton from '../icon-button.svelte'; describe('IconButton', () => { const common = { icon: 'close' as const, label: 'close' }; it('Renders a button in the style of the primary variant if no variant is specified', () => { render(IconButton, common); expect(screen.getByRole('button')).toHaveClass( 'text-gray-6', 'hover:border-medium' ); }); it('Renders a button in the style of the primary variant if variant is specified as primary', () => { render(IconButton, { ...common, variant: 'primary' }); expect(screen.getByRole('button')).toHaveClass( 'text-gray-6', 'hover:border-medium' ); }); it('Renders a button in the style of the secondary variant if variant is specified as secondary', () => { render(IconButton, { ...common, variant: 'secondary' }); expect(screen.getByRole('button')).toHaveClass('border-light', 'bg-light'); }); it('Renders a button in the style of danger if the variant is specified as danger', () => { render(IconButton, { ...common, variant: 'danger' }); expect(screen.getByRole('button')).toHaveClass( 'hover:text-danger-dark', 'active:text-danger-dark' ); }); it('Renders an icon within the button if an icon attribute is specified', () => { const { container } = render(IconButton, { ...common, icon: 'refresh' }); expect(container.querySelector('svg')).toBeVisible(); }); it('Renders a clickable button', async () => { const onClick = vi.fn(); render(IconButton, { props: common, events: { click: onClick } }); await fireEvent.click(screen.getByRole('button')); expect(onClick).toHaveBeenCalledOnce(); }); it('Renders a disabled button', async () => { const onClick = vi.fn(); render(IconButton, { props: { ...common, disabled: true }, events: { click: onClick }, }); await fireEvent.click(screen.getByRole('button')); expect(onClick).not.toHaveBeenCalled(); }); it('Renders with the passed cx classes', () => { render(IconButton, { ...common, cx: cxTestArguments }); expect(screen.getByRole('button')).toHaveClass(cxTestResults); }); });