import { render, screen } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { describe, expect, it, vi } from 'vitest' import { expectNoA11yViolations } from '../test/a11y' import { Accordion, type AccordionItem } from './Accordion' const items: AccordionItem[] = [ { value: 'a', title: 'Billing', content: 'Billing answer' }, { value: 'b', title: 'Cancelling', content: 'Cancel answer' }, { value: 'c', title: 'SSO', content: 'SSO answer', disabled: true }, ] describe('Accordion', () => { it('toggles a section and keeps aria-controls pointing at an existing panel', async () => { const onValueChange = vi.fn() render() const trigger = screen.getByRole('button', { name: 'Billing' }) expect(trigger).toHaveAttribute('aria-expanded', 'false') const controlsExisting = () => { const id = trigger.getAttribute('aria-controls') // Base UI only sets aria-controls while the panel is open; when set, it must resolve. return id == null || document.getElementById(id) != null } expect(controlsExisting()).toBe(true) await userEvent.click(trigger) expect(trigger).toHaveAttribute('aria-expanded', 'true') expect(onValueChange).toHaveBeenLastCalledWith(['a']) expect(document.getElementById(trigger.getAttribute('aria-controls')!)).toHaveTextContent('Billing answer') await userEvent.click(trigger) expect(trigger).toHaveAttribute('aria-expanded', 'false') expect(controlsExisting()).toBe(true) }) it('closes the other section unless multiple', async () => { const { unmount } = render() await userEvent.click(screen.getByRole('button', { name: 'Cancelling' })) expect(screen.getByRole('button', { name: 'Billing' })).toHaveAttribute('aria-expanded', 'false') unmount() render() await userEvent.click(screen.getByRole('button', { name: 'Cancelling' })) expect(screen.getByRole('button', { name: 'Billing' })).toHaveAttribute('aria-expanded', 'true') expect(screen.getByRole('button', { name: 'Cancelling' })).toHaveAttribute('aria-expanded', 'true') }) it('works with the keyboard', async () => { render() await userEvent.tab() expect(screen.getByRole('button', { name: 'Billing' })).toHaveFocus() await userEvent.keyboard('{Enter}') expect(screen.getByRole('button', { name: 'Billing' })).toHaveAttribute('aria-expanded', 'true') await userEvent.tab() await userEvent.keyboard(' ') expect(screen.getByRole('button', { name: 'Cancelling' })).toHaveAttribute('aria-expanded', 'true') }) it('renders headings at the requested level', () => { const { rerender } = render() expect(screen.getAllByRole('heading', { level: 3 })).toHaveLength(3) rerender() expect(screen.getAllByRole('heading', { level: 5 })).toHaveLength(3) expect(screen.getByRole('heading', { level: 5, name: 'Billing' })).toContainElement( screen.getByRole('button', { name: 'Billing' }), ) }) it('disables items', () => { render() const sso = screen.getByRole('button', { name: 'SSO' }) expect(sso).toHaveAttribute('aria-disabled', 'true') }) it('works controlled', async () => { const onValueChange = vi.fn() render() await userEvent.click(screen.getByRole('button', { name: 'Billing' })) expect(onValueChange).toHaveBeenCalledWith(['a']) expect(screen.getByRole('button', { name: 'Cancelling' })).toHaveAttribute('aria-expanded', 'true') }) it('has no axe violations', async () => { const { container } = render() await expectNoA11yViolations(container) }) })