|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import Radio from '../Radio'; |
| 4 | + |
| 5 | +describe('Radio', () => { |
| 6 | + const baseProps = { |
| 7 | + name: 'test-radio', |
| 8 | + value: 'option1', |
| 9 | + id: 'radio1' |
| 10 | + }; |
| 11 | + |
| 12 | + it('renders with required props', () => { |
| 13 | + render(<Radio {...baseProps} />); |
| 14 | + const radio = screen.getByRole('radio'); |
| 15 | + expect(radio).toBeInTheDocument(); |
| 16 | + expect(radio).toHaveAttribute('name', 'test-radio'); |
| 17 | + expect(radio).toHaveAttribute('value', 'option1'); |
| 18 | + expect(radio).toHaveAttribute('id', 'radio1'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('applies checked, required, and disabled props', () => { |
| 22 | + render( |
| 23 | + <Radio {...baseProps} checked required disabled /> |
| 24 | + ); |
| 25 | + const radio = screen.getByRole('radio'); |
| 26 | + expect(radio).toBeChecked(); |
| 27 | + expect(radio).toBeRequired(); |
| 28 | + expect(radio).toBeDisabled(); |
| 29 | + expect(radio).toHaveAttribute('aria-disabled', 'true'); |
| 30 | + expect(radio).toHaveAttribute('aria-required', 'true'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('toggles checked state on click', () => { |
| 34 | + render(<Radio {...baseProps} />); |
| 35 | + const radio = screen.getByRole('radio'); |
| 36 | + expect(radio).not.toBeChecked(); |
| 37 | + fireEvent.click(radio); |
| 38 | + expect(radio).toBeChecked(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('calls onChange when clicked', () => { |
| 42 | + const handleChange = jest.fn(); |
| 43 | + render( |
| 44 | + <Radio {...baseProps} onChange={handleChange} /> |
| 45 | + ); |
| 46 | + const radio = screen.getByRole('radio'); |
| 47 | + fireEvent.click(radio); |
| 48 | + expect(handleChange).toHaveBeenCalled(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('applies custom class names', () => { |
| 52 | + render( |
| 53 | + <Radio {...baseProps} className="custom-class" customRootClass="root-class" /> |
| 54 | + ); |
| 55 | + const radio = screen.getByRole('radio'); |
| 56 | + expect(radio.className).toMatch(/custom-class/); |
| 57 | + expect(radio.className).toMatch(/root-class/); |
| 58 | + }); |
| 59 | + |
| 60 | + it('applies data attributes for variant, size, and color', () => { |
| 61 | + render( |
| 62 | + <Radio {...baseProps} variant="filled" size="lg" color="red" /> |
| 63 | + ); |
| 64 | + const radio = screen.getByRole('radio'); |
| 65 | + expect(radio).toHaveAttribute('data-button-variant', 'filled'); |
| 66 | + expect(radio).toHaveAttribute('data-button-size', 'lg'); |
| 67 | + expect(radio).toHaveAttribute('data-rad-ui-accent-color', 'red'); |
| 68 | + }); |
| 69 | +}); |
0 commit comments