import { describe, expect, it, vi } from 'vitest'; import { render, screen } from '@testing-library/svelte'; import userEvent from '@testing-library/user-event'; import Radio from './radio.spec.svelte'; describe('Radio', () => { const common = { name: 'Test radios', options: ['Option 1', 'Option 2', 'Option 3'], selected: 'Option 1', }; it('Renders radio options correctly', () => { render(Radio, common); expect(screen.getByLabelText('Option 1')).toBeVisible(); expect(screen.getByLabelText('Option 2')).toBeVisible(); expect(screen.getByLabelText('Option 3')).toBeVisible(); }); it('Marks the selected option correctly', () => { render(Radio, common); expect(screen.getByLabelText('Option 1')).toBeChecked(); }); it('Marks the selected option as disabled if a disabled attribute of true has been specified', () => { render(Radio, { ...common, disabled: true, }); const option = screen.getByLabelText('Option 1'); expect(option).toHaveAttribute('readonly'); expect(option).toHaveAttribute('aria-disabled'); }); it('Allows option input if not disabled', async () => { const onInput = vi.fn(); render(Radio, { props: common, events: { input: onInput } }); const option: HTMLInputElement = screen.getByLabelText('Option 2'); await userEvent.click(option); expect(onInput).toHaveBeenCalled(); expect(option).toBeChecked(); }); it('Prevents option input if disabled', async () => { const onInput = vi.fn(); render(Radio, { props: { ...common, disabled: true }, events: { input: onInput }, }); const option: HTMLInputElement = screen.getByLabelText('Option 2'); await userEvent.click(option); expect(onInput).not.toHaveBeenCalled(); expect(option).not.toBeChecked(); }); it('Disables only the specified option when disabled is set per-option', async () => { const onInput = vi.fn(); render(Radio, { props: { name: 'Test radios', options: [ { label: 'Option 1', value: 'Option 1' }, { label: 'Option 2', value: 'Option 2', disabled: true }, { label: 'Option 3', value: 'Option 3' }, ], selected: 'Option 1', }, events: { input: onInput }, }); const opt1: HTMLInputElement = screen.getByLabelText('Option 1'); const opt2: HTMLInputElement = screen.getByLabelText('Option 2'); const opt3: HTMLInputElement = screen.getByLabelText('Option 3'); expect(opt2).toHaveAttribute('aria-disabled'); expect(opt2).toHaveAttribute('readonly'); expect(opt1).not.toHaveAttribute('aria-disabled'); expect(opt3).not.toHaveAttribute('aria-disabled'); await userEvent.click(opt2); expect(onInput).not.toHaveBeenCalled(); expect(opt2).not.toBeChecked(); await userEvent.click(opt3); expect(onInput).toHaveBeenCalled(); expect(opt3).toBeChecked(); }); it('Renders the legend correctly', () => { render(Radio, common); expect(screen.getByText('Test radio options')).toBeVisible(); }); it('Renders the legend correctly when required', () => { render(Radio, { ...common, required: true }); const legend = screen.getByText('Test radio options'); expect(legend).toHaveClass('after:text-danger-dark after:content-["*"]'); }); it('Renders in a column if specified', () => { render(Radio, { ...common, direction: 'col' }); const fieldset = screen.getByRole('group', { name: 'Test radio options' }); expect(fieldset).toHaveClass('flex-col'); expect(fieldset).not.toHaveClass('flex-row'); }); it('Renders in a column if no direction specified', () => { render(Radio, common); const fieldset = screen.getByRole('group', { name: 'Test radio options' }); expect(fieldset).toHaveClass('flex-col'); }); it('Renders in a row if direction is "row"', () => { render(Radio, { ...common, direction: 'row' }); const fieldset = screen.getByRole('group', { name: 'Test radio options' }); expect(fieldset).toHaveClass('flex-row'); expect(fieldset).not.toHaveClass('flex-col'); }); });