import { describe, expect, it } from 'vitest'; import type { ComponentProps } from 'svelte'; import { render, screen, waitFor } from '@testing-library/svelte'; import userEvent from '@testing-library/user-event'; import Subject from './expandable.spec.svelte'; describe('Expandable', () => { const renderSubject = (props?: Partial>) => { const result = render(Subject, props); return result; }; it('renders with default values', () => { renderSubject(); // Should render a button const button = screen.getByRole('button'); expect(button).toBeInTheDocument(); // Check for title within the button expect(button).toHaveTextContent('Show'); const region = screen.getByRole('region', { hidden: true }); expect(region).not.toBeVisible(); expect(region).toHaveTextContent('This is the content for the expandable.'); expect(region).toHaveTextContent('It can include various HTML elements.'); }); it('renders open', () => { renderSubject({ open: true }); // Should render a button const button = screen.getByRole('button'); expect(button).toBeInTheDocument(); // Check for title within the button expect(button).toHaveTextContent('Hide'); const region = screen.getByRole('region'); expect(region).toBeVisible(); expect(region).toHaveTextContent('This is the content for the expandable.'); expect(region).toHaveTextContent('It can include various HTML elements.'); }); it('renders with custom classes', () => { const { container } = renderSubject({ class: 'bg-red-500', triggerClass: 'bg-blue-500', contentClass: 'bg-green-500', open: true, }); // eslint-disable-next-line testing-library/no-node-access const root = container.querySelector('[data-part="root"]'); const trigger = screen.getByRole('button'); const content = screen.getByRole('region'); expect(root).toHaveClass('bg-red-500'); expect(trigger).toHaveClass('bg-blue-500'); expect(content).toHaveClass('bg-green-500'); }); it('toggles content visibility when clicked', async () => { const user = userEvent.setup(); renderSubject(); const button = screen.getByRole('button'); const region = screen.getByRole('region', { hidden: true }); expect(button).toHaveTextContent('Show'); expect(region).not.toBeVisible(); await user.click(button); expect(button).toHaveTextContent('Hide'); expect(region).toBeVisible(); await user.click(button); expect(button).toHaveTextContent('Show'); await waitFor(() => expect(region).not.toBeVisible()); }); });