|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom'; |
| 4 | +import RadioGroup from '../RadioGroup'; |
| 5 | + |
| 6 | +// Helper for the story pattern |
| 7 | +const options = [ |
| 8 | + { id: 'html', value: 'html', label: 'HTML' }, |
| 9 | + { id: 'css', value: 'css', label: 'CSS' }, |
| 10 | + { id: 'javascript', value: 'javascript', label: 'JavaScript' } |
| 11 | +]; |
| 12 | + |
| 13 | +// Explicitly type props to allow all RadioGroupRoot props, but make children optional |
| 14 | +function StoryRadioGroup(props: Omit<React.ComponentProps<typeof RadioGroup.Root>, 'children'> & { children?: React.ReactNode }) { |
| 15 | + return ( |
| 16 | + <RadioGroup.Root {...props}> |
| 17 | + {props.children ?? |
| 18 | + options.map((option) => ( |
| 19 | + <RadioGroup.Label key={option.id} data-testid={`label-${option.value}`}> |
| 20 | + <RadioGroup.Item value={option.value} data-testid={`item-${option.value}`}> |
| 21 | + <RadioGroup.Indicator data-testid={`indicator-${option.value}`} /> |
| 22 | + </RadioGroup.Item> |
| 23 | + {option.label} |
| 24 | + </RadioGroup.Label> |
| 25 | + ))} |
| 26 | + </RadioGroup.Root> |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +describe('RadioGroup (fragments)', () => { |
| 31 | + it('renders without crashing (story pattern)', () => { |
| 32 | + render(<StoryRadioGroup />); |
| 33 | + options.forEach(opt => { |
| 34 | + expect(screen.getByText(opt.label)).toBeInTheDocument(); |
| 35 | + expect(screen.getByTestId(`item-${opt.value}`)).toBeInTheDocument(); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('selects the correct radio on click (uncontrolled)', () => { |
| 40 | + render(<StoryRadioGroup defaultValue="css" name="test-group" />); |
| 41 | + const itemHtml = screen.getByTestId('item-html'); |
| 42 | + const itemCss = screen.getByTestId('item-css'); |
| 43 | + const itemJs = screen.getByTestId('item-javascript'); |
| 44 | + // Only CSS is selected initially |
| 45 | + expect(itemHtml).toHaveAttribute('aria-checked', 'false'); |
| 46 | + expect(itemCss).toHaveAttribute('aria-checked', 'true'); |
| 47 | + expect(itemJs).toHaveAttribute('aria-checked', 'false'); |
| 48 | + // Click HTML |
| 49 | + fireEvent.click(itemHtml); |
| 50 | + expect(itemHtml).toHaveAttribute('aria-checked', 'true'); |
| 51 | + expect(itemCss).toHaveAttribute('aria-checked', 'false'); |
| 52 | + expect(itemJs).toHaveAttribute('aria-checked', 'false'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('shows indicator only for selected item', () => { |
| 56 | + render(<StoryRadioGroup defaultValue="javascript" />); |
| 57 | + options.forEach(opt => { |
| 58 | + const indicator = screen.queryByTestId(`indicator-${opt.value}`); |
| 59 | + if (opt.value === 'javascript') { |
| 60 | + expect(indicator).toBeInTheDocument(); |
| 61 | + // Optionally: expect(indicator).not.toBeEmptyDOMElement(); |
| 62 | + } else { |
| 63 | + expect(indicator).not.toBeInTheDocument(); |
| 64 | + } |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('renders label text and associates with item', () => { |
| 69 | + render(<StoryRadioGroup />); |
| 70 | + options.forEach(opt => { |
| 71 | + const label = screen.getByTestId(`label-${opt.value}`); |
| 72 | + expect(label).toHaveTextContent(opt.label); |
| 73 | + // The item is a button inside the label |
| 74 | + const item = screen.getByTestId(`item-${opt.value}`); |
| 75 | + expect(label).toContainElement(item); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('passes custom className, variant, size, color to root', () => { |
| 80 | + render( |
| 81 | + <RadioGroup.Root className="custom-class" data-testid="radio-group" variant="filled" size="lg" color="primary"> |
| 82 | + <RadioGroup.Item value="a">A</RadioGroup.Item> |
| 83 | + </RadioGroup.Root> |
| 84 | + ); |
| 85 | + const group = screen.getByTestId('radio-group'); |
| 86 | + console.log('group:', group); |
| 87 | + // Data attributes for variant/size/color |
| 88 | + expect(group.getAttribute('data-radio-group-variant')).toBe('filled'); |
| 89 | + expect(group.getAttribute('data-radio-group-size')).toBe('lg'); |
| 90 | + expect(group.getAttribute('data-rad-ui-accent-color')).toBe('primary'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('warns on direct usage of RadioGroup', () => { |
| 94 | + const spy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 95 | + render(<RadioGroup />); |
| 96 | + expect(spy).toHaveBeenCalledWith( |
| 97 | + 'Direct usage of RadioGroup is not supported. Please use RadioGroup.Root and RadioGroup.Item instead.' |
| 98 | + ); |
| 99 | + spy.mockRestore(); |
| 100 | + }); |
| 101 | +}); |
0 commit comments