2023-06-07 11:54:50 +08:00
|
|
|
import React from 'react';
|
2023-10-12 09:43:35 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
import type { RadioGroupProps } from '..';
|
|
|
|
import Radio from '..';
|
2023-06-07 11:54:50 +08:00
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
2022-07-24 12:33:05 +08:00
|
|
|
|
2019-08-26 22:53:20 +08:00
|
|
|
describe('Radio Group', () => {
|
2024-03-16 21:41:25 +08:00
|
|
|
const RadioGroupComponent: React.FC<RadioGroupProps> = (props) => (
|
|
|
|
<Radio.Group {...props}>
|
|
|
|
<Radio value="A">A</Radio>
|
|
|
|
<Radio value="B">B</Radio>
|
|
|
|
<Radio value="C">C</Radio>
|
|
|
|
</Radio.Group>
|
|
|
|
);
|
|
|
|
|
|
|
|
const RadioGroupByOptions = React.forwardRef<HTMLDivElement, RadioGroupProps>((props, ref) => {
|
2017-03-28 11:56:38 +08:00
|
|
|
const options = [
|
|
|
|
{ label: 'A', value: 'A' },
|
|
|
|
{ label: 'B', value: 'B' },
|
|
|
|
{ label: 'C', value: 'C' },
|
|
|
|
];
|
2024-03-16 21:41:25 +08:00
|
|
|
return <Radio.Group {...props} options={options} ref={ref} />;
|
|
|
|
});
|
2017-03-28 11:56:38 +08:00
|
|
|
|
2016-12-14 14:48:09 +08:00
|
|
|
it('responses hover events', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onMouseEnter = jest.fn();
|
|
|
|
const onMouseLeave = jest.fn();
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container } = render(
|
2021-10-13 17:13:07 +08:00
|
|
|
<Radio.Group onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}>
|
2016-12-14 14:48:09 +08:00
|
|
|
<Radio />
|
2021-10-13 17:13:07 +08:00
|
|
|
</Radio.Group>,
|
2016-12-14 14:48:09 +08:00
|
|
|
);
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelector('div')!);
|
2016-12-14 14:48:09 +08:00
|
|
|
expect(onMouseEnter).toHaveBeenCalled();
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.mouseLeave(container.querySelector('div')!);
|
2016-12-14 14:48:09 +08:00
|
|
|
expect(onMouseLeave).toHaveBeenCalled();
|
|
|
|
});
|
2016-12-19 10:52:47 +08:00
|
|
|
|
|
|
|
it('fire change events when value changes', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onChange = jest.fn();
|
2016-12-19 10:52:47 +08:00
|
|
|
|
2024-03-16 21:41:25 +08:00
|
|
|
const { container, rerender } = render(<RadioGroupComponent onChange={onChange} />);
|
2022-07-24 12:33:05 +08:00
|
|
|
const radios = container.querySelectorAll('input');
|
2016-12-19 10:52:47 +08:00
|
|
|
|
|
|
|
// controlled component
|
2024-03-16 21:41:25 +08:00
|
|
|
rerender(<RadioGroupComponent value="A" onChange={onChange} />);
|
2022-07-24 12:33:05 +08:00
|
|
|
fireEvent.click(radios[1]);
|
2020-05-27 10:21:17 +08:00
|
|
|
expect(onChange.mock.calls.length).toBe(1);
|
2016-12-19 10:52:47 +08:00
|
|
|
});
|
|
|
|
|
2019-01-18 16:12:47 +08:00
|
|
|
it('both of radio and radioGroup will trigger onchange event when they exists', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onChange = jest.fn();
|
|
|
|
const onChangeRadioGroup = jest.fn();
|
2019-01-18 16:12:47 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
const RadioGroup: React.FC<
|
|
|
|
RadioGroupProps & { onChangeRadioGroup: RadioGroupProps['onChange'] }
|
2022-11-19 13:47:33 +08:00
|
|
|
> = (props) => (
|
2022-07-24 12:33:05 +08:00
|
|
|
<Radio.Group onChange={props.onChangeRadioGroup}>
|
|
|
|
<Radio value="A" onChange={props.onChange}>
|
2019-01-18 16:12:47 +08:00
|
|
|
A
|
|
|
|
</Radio>
|
2022-07-24 12:33:05 +08:00
|
|
|
<Radio value="B" onChange={props.onChange}>
|
2019-01-18 16:12:47 +08:00
|
|
|
B
|
|
|
|
</Radio>
|
2022-07-24 12:33:05 +08:00
|
|
|
<Radio value="C" onChange={props.onChange}>
|
2019-01-18 16:12:47 +08:00
|
|
|
C
|
|
|
|
</Radio>
|
2022-07-24 12:33:05 +08:00
|
|
|
</Radio.Group>
|
2019-01-18 16:12:47 +08:00
|
|
|
);
|
2022-07-24 12:33:05 +08:00
|
|
|
|
|
|
|
const { container, rerender } = render(
|
|
|
|
<RadioGroup onChangeRadioGroup={onChangeRadioGroup} onChange={onChange} />,
|
2019-01-18 16:12:47 +08:00
|
|
|
);
|
2022-07-24 12:33:05 +08:00
|
|
|
const radios = container.querySelectorAll('input');
|
2019-01-18 16:12:47 +08:00
|
|
|
|
|
|
|
// controlled component
|
2022-07-24 12:33:05 +08:00
|
|
|
rerender(<RadioGroup value="A" onChangeRadioGroup={onChangeRadioGroup} onChange={onChange} />);
|
|
|
|
fireEvent.click(radios[1]);
|
2020-05-27 10:21:17 +08:00
|
|
|
expect(onChange.mock.calls.length).toBe(1);
|
2022-07-24 12:33:05 +08:00
|
|
|
expect(onChangeRadioGroup.mock.calls.length).toBe(1);
|
2019-01-18 16:12:47 +08:00
|
|
|
});
|
|
|
|
|
2019-01-25 10:32:30 +08:00
|
|
|
it('Trigger onChange when both of radioButton and radioGroup exists', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onChange = jest.fn();
|
2019-01-25 10:32:30 +08:00
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const RadioGroup: React.FC<RadioGroupProps> = (props) => (
|
2022-07-24 12:33:05 +08:00
|
|
|
<Radio.Group {...props}>
|
2021-10-13 17:13:07 +08:00
|
|
|
<Radio.Button value="A">A</Radio.Button>
|
|
|
|
<Radio.Button value="B">B</Radio.Button>
|
|
|
|
<Radio.Button value="C">C</Radio.Button>
|
2022-07-24 12:33:05 +08:00
|
|
|
</Radio.Group>
|
2019-01-25 10:32:30 +08:00
|
|
|
);
|
2022-07-24 12:33:05 +08:00
|
|
|
|
|
|
|
const { container, rerender } = render(<RadioGroup onChange={onChange} />);
|
|
|
|
const radios = container.querySelectorAll('input');
|
2019-01-25 10:32:30 +08:00
|
|
|
|
|
|
|
// controlled component
|
2022-07-24 12:33:05 +08:00
|
|
|
rerender(<RadioGroup value="A" onChange={onChange} />);
|
|
|
|
fireEvent.click(radios[1]);
|
2020-05-27 10:21:17 +08:00
|
|
|
expect(onChange.mock.calls.length).toBe(1);
|
2019-01-25 10:32:30 +08:00
|
|
|
});
|
|
|
|
|
2019-02-21 20:53:19 +08:00
|
|
|
it('should only trigger once when in group with options', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onChange = jest.fn();
|
2019-02-21 20:53:19 +08:00
|
|
|
const options = [{ label: 'Bamboo', value: 'Bamboo' }];
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container } = render(<Radio.Group options={options} onChange={onChange} />);
|
2019-02-21 20:53:19 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.click(container.querySelector('input')!);
|
2019-02-21 20:53:19 +08:00
|
|
|
expect(onChange).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
it("won't fire change events when value not changes", () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onChange = jest.fn();
|
2016-12-19 10:52:47 +08:00
|
|
|
|
2024-03-16 21:41:25 +08:00
|
|
|
const { container, rerender } = render(<RadioGroupComponent onChange={onChange} />);
|
2022-07-24 12:33:05 +08:00
|
|
|
const radios = container.querySelectorAll('input');
|
2016-12-19 10:52:47 +08:00
|
|
|
|
|
|
|
// controlled component
|
2024-03-16 21:41:25 +08:00
|
|
|
rerender(<RadioGroupComponent value="A" onChange={onChange} />);
|
2022-07-24 12:33:05 +08:00
|
|
|
fireEvent.click(radios[0]);
|
2016-12-19 10:52:47 +08:00
|
|
|
expect(onChange.mock.calls.length).toBe(0);
|
|
|
|
});
|
2017-03-28 11:56:38 +08:00
|
|
|
|
|
|
|
it('optional should correct render', () => {
|
2024-03-16 21:41:25 +08:00
|
|
|
const { container } = render(<RadioGroupByOptions />);
|
2022-07-24 12:33:05 +08:00
|
|
|
const radios = container.querySelectorAll('input');
|
2017-03-28 11:56:38 +08:00
|
|
|
|
|
|
|
expect(radios.length).toBe(3);
|
|
|
|
});
|
2017-07-31 11:33:47 +08:00
|
|
|
|
|
|
|
it('all children should have a name property', () => {
|
2022-07-24 12:33:05 +08:00
|
|
|
const GROUP_NAME = 'GROUP_NAME';
|
2024-03-16 21:41:25 +08:00
|
|
|
const { container } = render(<RadioGroupComponent name={GROUP_NAME} />);
|
2017-07-31 11:33:47 +08:00
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
container.querySelectorAll<HTMLInputElement>('input[type="radio"]').forEach((el) => {
|
2022-07-24 12:33:05 +08:00
|
|
|
expect(el.name).toEqual(GROUP_NAME);
|
2019-04-03 15:54:26 +08:00
|
|
|
});
|
2017-07-31 11:33:47 +08:00
|
|
|
});
|
2018-04-22 16:35:34 +08:00
|
|
|
|
|
|
|
it('passes prefixCls down to radio', () => {
|
2020-03-16 12:47:24 +08:00
|
|
|
const options = [
|
|
|
|
{ label: 'Apple', value: 'Apple' },
|
|
|
|
{ label: 'Orange', value: 'Orange', style: { fontSize: 12 } },
|
|
|
|
];
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container } = render(<Radio.Group prefixCls="my-radio" options={options} />);
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2018-04-22 16:35:34 +08:00
|
|
|
});
|
2020-03-16 12:47:24 +08:00
|
|
|
|
2020-09-03 15:31:08 +08:00
|
|
|
it('should forward ref', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
let radioGroupRef: HTMLDivElement;
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container } = render(
|
2024-03-16 21:41:25 +08:00
|
|
|
<RadioGroupByOptions
|
|
|
|
ref={(ref: HTMLDivElement) => {
|
2020-09-03 15:31:08 +08:00
|
|
|
radioGroupRef = ref;
|
2024-03-16 21:41:25 +08:00
|
|
|
}}
|
|
|
|
/>,
|
2020-09-03 15:31:08 +08:00
|
|
|
);
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
expect(radioGroupRef!).toBe(container.querySelector<HTMLDivElement>('.ant-radio-group'));
|
2020-09-03 15:31:08 +08:00
|
|
|
});
|
|
|
|
|
2021-10-13 17:13:07 +08:00
|
|
|
it('should support data-* or aria-* props', () => {
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container } = render(
|
2024-03-16 21:41:25 +08:00
|
|
|
<RadioGroupComponent data-radio-group-id="radio-group-id" aria-label="radio-group" />,
|
2022-09-05 19:41:32 +08:00
|
|
|
);
|
|
|
|
expect((container.firstChild as HTMLDivElement)?.getAttribute('data-radio-group-id')).toBe(
|
|
|
|
'radio-group-id',
|
|
|
|
);
|
|
|
|
expect((container.firstChild as HTMLDivElement)?.getAttribute('aria-label')).toBe(
|
|
|
|
'radio-group',
|
2021-10-13 17:13:07 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Radio type should not be override', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onChange = jest.fn();
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container } = render(
|
2021-10-13 17:13:07 +08:00
|
|
|
<Radio.Group onChange={onChange}>
|
|
|
|
<Radio value={1} type="1">
|
|
|
|
A
|
|
|
|
</Radio>
|
|
|
|
<Radio value={2} type="2">
|
|
|
|
B
|
|
|
|
</Radio>
|
|
|
|
<Radio value={3} type="3">
|
|
|
|
C
|
|
|
|
</Radio>
|
|
|
|
<Radio value={4} type="4">
|
|
|
|
D
|
|
|
|
</Radio>
|
|
|
|
</Radio.Group>,
|
|
|
|
);
|
2022-07-24 12:33:05 +08:00
|
|
|
const radios = container.querySelectorAll('input');
|
|
|
|
fireEvent.click(radios[0]);
|
2021-10-13 17:13:07 +08:00
|
|
|
expect(onChange).toHaveBeenCalled();
|
2022-07-24 12:33:05 +08:00
|
|
|
expect(radios[1].type).toBe('radio');
|
2021-10-13 17:13:07 +08:00
|
|
|
});
|
|
|
|
|
2020-03-16 12:47:24 +08:00
|
|
|
describe('value is null or undefined', () => {
|
|
|
|
it('use `defaultValue` when `value` is undefined', () => {
|
|
|
|
const options = [{ label: 'Bamboo', value: 'bamboo' }];
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container } = render(
|
2021-10-13 17:13:07 +08:00
|
|
|
<Radio.Group defaultValue="bamboo" value={undefined} options={options} />,
|
2020-03-16 12:47:24 +08:00
|
|
|
);
|
2022-07-24 12:33:05 +08:00
|
|
|
expect(container.querySelectorAll('.ant-radio-wrapper-checked').length).toBe(1);
|
2020-03-16 12:47:24 +08:00
|
|
|
});
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
[undefined, null].forEach((newValue) => {
|
2020-03-16 12:47:24 +08:00
|
|
|
it(`should set value back when value change back to ${newValue}`, () => {
|
|
|
|
const options = [{ label: 'Bamboo', value: 'bamboo' }];
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container, rerender } = render(<Radio.Group value="bamboo" options={options} />);
|
|
|
|
expect(container.querySelectorAll('.ant-radio-wrapper-checked').length).toBe(1);
|
|
|
|
rerender(<Radio.Group value={newValue} options={options} />);
|
|
|
|
expect(container.querySelectorAll('.ant-radio-wrapper-checked').length).toBe(0);
|
2020-03-16 12:47:24 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-06-14 14:00:33 +08:00
|
|
|
|
|
|
|
it('onBlur & onFocus should work', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const handleBlur = jest.fn();
|
|
|
|
const handleFocus = jest.fn();
|
2022-07-25 17:39:38 +08:00
|
|
|
const { container } = render(
|
2022-06-14 14:00:33 +08:00
|
|
|
<Radio.Group options={['1', '2', '3']} onBlur={handleBlur} onFocus={handleFocus} />,
|
|
|
|
);
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.focus(container.firstChild!);
|
2022-06-14 14:00:33 +08:00
|
|
|
expect(handleFocus).toHaveBeenCalledTimes(1);
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.blur(container.firstChild!);
|
2022-06-14 14:00:33 +08:00
|
|
|
expect(handleBlur).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2023-10-12 09:43:35 +08:00
|
|
|
|
|
|
|
it('options support id', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Radio.Group options={[{ label: 'bamboo', id: 'bamboo', value: 'bamboo' }]} />,
|
|
|
|
);
|
|
|
|
expect(container.querySelector('#bamboo')).toBeTruthy();
|
|
|
|
});
|
2024-01-09 14:14:26 +08:00
|
|
|
|
|
|
|
it('options support title', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Radio.Group options={[{ label: 'bamboo', title: 'bamboo', value: 'bamboo' }]} />,
|
|
|
|
);
|
|
|
|
|
|
|
|
const select = container.querySelector('.ant-radio-group label > span');
|
|
|
|
expect(select).toBeTruthy();
|
|
|
|
// https://github.com/ant-design/ant-design/issues/46739
|
|
|
|
expect(select!.getAttribute('title')).toBeFalsy();
|
|
|
|
// fix 46739 solution
|
|
|
|
expect(container.querySelector('.ant-radio-group label')).toHaveAttribute('title', 'bamboo');
|
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|