|
| 1 | +import '@testing-library/jest-dom'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { Radio } from './Radio'; |
| 5 | + |
| 6 | +const buildProperties = ( |
| 7 | + additive: number | string |
| 8 | +): { id: string; label: string } => ({ |
| 9 | + id: `radio-${additive}`, |
| 10 | + label: `label-${additive}` |
| 11 | +}); |
| 12 | + |
| 13 | +describe('<Radio />', () => { |
| 14 | + const helperText = 'helperText goes here'; |
| 15 | + const role = 'radio'; |
| 16 | + |
| 17 | + it('renders labels correctly', () => { |
| 18 | + const properties = buildProperties('first'); |
| 19 | + render(<Radio {...properties} />); |
| 20 | + expect(screen.getByText(properties.label)).toBeInTheDocument(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('renders helperText', () => { |
| 24 | + const properties = buildProperties('helper'); |
| 25 | + render(<Radio {...properties} helperText={helperText} />); |
| 26 | + expect(screen.getByText(helperText)).toBeInTheDocument(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('isLarge', () => { |
| 30 | + const properties = buildProperties('large'); |
| 31 | + render(<Radio {...properties} helperText={helperText} isLarge />); |
| 32 | + |
| 33 | + expect(screen.getByText(helperText)).toBeInTheDocument(); |
| 34 | + expect(screen.getByTestId('radio-container').getAttribute('class')).toMatch( |
| 35 | + 'm-form-field__lg-target' |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it('isDisabled', () => { |
| 40 | + const properties = buildProperties('disabled'); |
| 41 | + render(<Radio {...properties} isDisabled />); |
| 42 | + |
| 43 | + const element = screen.getByRole(role); |
| 44 | + |
| 45 | + expect(element).toBeInTheDocument(); |
| 46 | + expect(element.getAttribute('disabled')).not.toBe('null'); |
| 47 | + expect(element.getAttribute('disabled')).toBe(''); |
| 48 | + }); |
| 49 | + |
| 50 | + it('Select via click', () => { |
| 51 | + const properties = buildProperties('click'); |
| 52 | + render(<Radio {...properties} />); |
| 53 | + |
| 54 | + const element = screen.getByRole(role); |
| 55 | + expect(element).not.toBeChecked(); |
| 56 | + |
| 57 | + screen.getByText(properties.label).click(); |
| 58 | + |
| 59 | + expect(element).toBeChecked(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('Select via keyboard', async () => { |
| 63 | + const user = userEvent.setup(); |
| 64 | + const properties = buildProperties('keyboard'); |
| 65 | + render(<Radio {...properties} />); |
| 66 | + |
| 67 | + const element = screen.getByRole(role); |
| 68 | + expect(element).not.toBeChecked(); |
| 69 | + |
| 70 | + element.focus(); |
| 71 | + await user.keyboard(' '); |
| 72 | + |
| 73 | + expect(element).toBeChecked(); |
| 74 | + }); |
| 75 | +}); |
0 commit comments