import { fireEvent, render, screen } from '@testing-library/react' import Radio from '.' import { RadioGroup } from './RadioGroup' import { vi } from 'vitest' describe('Radio', () => { describe('development mode', () => { beforeEach(() => { global.process.env.NODE_ENV = 'development' }) describe(' is not surrounded by ', () => { beforeEach(() => { // eslint-disable-next-line no-console console.error = vi.fn() render() }) it('console.error()', () => { // eslint-disable-next-line no-console expect(console.error).toHaveBeenCalledWith( expect.stringMatching( /Perhaps you forgot to wrap with /u, ), ) }) }) }) describe('value is the first option', () => { let option1: HTMLInputElement let option2: HTMLInputElement const handleChange = vi.fn() beforeEach(() => { render() option1 = screen.getByDisplayValue('option1') option2 = screen.getByDisplayValue('option2') }) it('the first is checked', () => { expect(option1.checked).toBeTruthy() expect(option2.checked).toBeFalsy() }) describe('clicking the second', () => { it('event handler is called', () => { fireEvent.click(option2) expect(handleChange).toHaveBeenCalledWith('option2') }) }) }) describe(' is disabled', () => { it('all s are disabled', () => { render() screen.getAllByRole('radio').forEach((element) => { expect(element.disabled).toBeTruthy() }) }) }) describe('the first is disabled', () => { let option1: HTMLInputElement let option2: HTMLInputElement beforeEach(() => { render() option1 = screen.getByDisplayValue('option1') option2 = screen.getByDisplayValue('option2') }) it(' in the first is disabled', () => { expect(option1.checked).toBeTruthy() }) it('No other is disabled', () => { expect(option2.checked).toBeFalsy() }) }) describe('has readonly in ', () => { let option1: HTMLInputElement let option2: HTMLInputElement beforeEach(() => { render() option1 = screen.getByDisplayValue('option1') option2 = screen.getByDisplayValue('option2') }) it('the first is checked', () => { expect(option1.checked).toBeTruthy() expect(option1.disabled).toBeFalsy() }) it('Non-first s are disabled', () => { expect(option2.disabled).toBeTruthy() }) }) }) function TestComponent({ value, onChange = vi.fn(), radioGroupDisabled = false, readonly = false, invalid = false, option1Disabled = false, option2Disabled = false, }: { value?: string onChange?: () => void radioGroupDisabled?: boolean readonly?: boolean invalid?: boolean option1Disabled?: boolean option2Disabled?: boolean }) { return ( option1を選ぶ option2を選ぶ ) }