2022-09-05 19:41:32 +08:00
|
|
|
import type { RefAttributes } from 'react';
|
2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
2022-09-05 19:41:32 +08:00
|
|
|
import type { RadioGroupProps } from '..';
|
2022-04-08 15:23:52 +08:00
|
|
|
import Radio, { Button } from '..';
|
|
|
|
import focusTest from '../../../tests/shared/focusTest';
|
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
|
|
|
|
2022-07-24 12:33:05 +08:00
|
|
|
import { render, fireEvent } from '../../../tests/utils';
|
|
|
|
|
2022-04-08 15:23:52 +08:00
|
|
|
describe('Radio Button', () => {
|
|
|
|
focusTest(Button, { refFocus: true });
|
|
|
|
mountTest(Button);
|
|
|
|
|
|
|
|
rtlTest(Button);
|
|
|
|
|
|
|
|
it('should render correctly', () => {
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container } = render(<Button className="customized">Test</Button>);
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2022-04-08 15:23:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('responses hover events', () => {
|
|
|
|
const onMouseEnter = jest.fn();
|
|
|
|
const onMouseLeave = jest.fn();
|
|
|
|
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Button onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} />,
|
|
|
|
);
|
2022-04-08 15:23:52 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelector('label')!);
|
2022-04-08 15:23:52 +08:00
|
|
|
expect(onMouseEnter).toHaveBeenCalled();
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.mouseLeave(container.querySelector('label')!);
|
2022-04-08 15:23:52 +08:00
|
|
|
expect(onMouseLeave).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Radio Group', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
function createRadioGroup(props?: RadioGroupProps & RefAttributes<HTMLDivElement>) {
|
2022-04-08 15:23:52 +08:00
|
|
|
return (
|
|
|
|
<Radio.Group {...props}>
|
|
|
|
<Button value="A">A</Button>
|
|
|
|
<Button value="B">B</Button>
|
|
|
|
<Button value="C">C</Button>
|
|
|
|
</Radio.Group>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
it('responses hover events', () => {
|
|
|
|
const onMouseEnter = jest.fn();
|
|
|
|
const onMouseLeave = jest.fn();
|
|
|
|
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container } = render(
|
2022-04-08 15:23:52 +08:00
|
|
|
<Radio.Group onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}>
|
|
|
|
<Radio />
|
|
|
|
</Radio.Group>,
|
|
|
|
);
|
|
|
|
|
2022-07-24 12:33:05 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelectorAll('div')[0]);
|
2022-04-08 15:23:52 +08:00
|
|
|
expect(onMouseEnter).toHaveBeenCalled();
|
|
|
|
|
2022-07-24 12:33:05 +08:00
|
|
|
fireEvent.mouseLeave(container.querySelectorAll('div')[0]);
|
2022-04-08 15:23:52 +08:00
|
|
|
expect(onMouseLeave).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fire change events when value changes', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container, rerender } = render(createRadioGroup({ onChange }));
|
|
|
|
|
|
|
|
const radios = container.querySelectorAll('input');
|
2022-04-08 15:23:52 +08:00
|
|
|
|
|
|
|
// controlled component
|
2022-07-24 12:33:05 +08:00
|
|
|
rerender(createRadioGroup({ onChange, value: 'A' }));
|
|
|
|
fireEvent.click(radios[1]);
|
2022-04-08 15:23:52 +08:00
|
|
|
expect(onChange.mock.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('both of radio and radioGroup will trigger onchange event when they exists', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const onChangeRadioGroup = jest.fn();
|
|
|
|
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container } = render(
|
2022-04-08 15:23:52 +08:00
|
|
|
<Radio.Group onChange={onChangeRadioGroup}>
|
|
|
|
<Radio value="A" onChange={onChange}>
|
|
|
|
A
|
|
|
|
</Radio>
|
|
|
|
<Radio value="B" onChange={onChange}>
|
|
|
|
B
|
|
|
|
</Radio>
|
|
|
|
<Radio value="C" onChange={onChange}>
|
|
|
|
C
|
|
|
|
</Radio>
|
|
|
|
</Radio.Group>,
|
|
|
|
);
|
2022-07-24 12:33:05 +08:00
|
|
|
const radios = container.querySelectorAll('input');
|
2022-04-08 15:23:52 +08:00
|
|
|
|
|
|
|
// controlled component
|
2022-07-24 12:33:05 +08:00
|
|
|
fireEvent.click(radios[1]);
|
2022-04-08 15:23:52 +08:00
|
|
|
expect(onChange.mock.calls.length).toBe(1);
|
2022-07-24 12:33:05 +08:00
|
|
|
expect(onChangeRadioGroup.mock.calls.length).toBe(1);
|
2022-04-08 15:23:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Trigger onChange when both of Button and radioGroup exists', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container, rerender } = render(
|
2022-04-08 15:23:52 +08:00
|
|
|
<Radio.Group onChange={onChange}>
|
|
|
|
<Button value="A">A</Button>
|
|
|
|
<Button value="B">B</Button>
|
|
|
|
<Button value="C">C</Button>
|
|
|
|
</Radio.Group>,
|
|
|
|
);
|
2022-07-24 12:33:05 +08:00
|
|
|
const radios = container.querySelectorAll('input');
|
2022-04-08 15:23:52 +08:00
|
|
|
|
|
|
|
// controlled component
|
2022-07-24 12:33:05 +08:00
|
|
|
rerender(
|
|
|
|
<Radio.Group value="A" onChange={onChange}>
|
|
|
|
<Button value="A">A</Button>
|
|
|
|
<Button value="B">B</Button>
|
|
|
|
<Button value="C">C</Button>
|
|
|
|
</Radio.Group>,
|
|
|
|
);
|
|
|
|
fireEvent.click(radios[1]);
|
2022-04-08 15:23:52 +08:00
|
|
|
expect(onChange.mock.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should only trigger once when in group with options', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const options = [{ label: 'Bamboo', value: 'Bamboo' }];
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container } = render(<Radio.Group options={options} onChange={onChange} />);
|
2022-04-08 15:23:52 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.click(container.querySelector('input')!);
|
2022-04-08 15:23:52 +08:00
|
|
|
expect(onChange).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("won't fire change events when value not changes", () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container, rerender } = render(
|
2022-04-08 15:23:52 +08:00
|
|
|
createRadioGroup({
|
|
|
|
onChange,
|
|
|
|
}),
|
|
|
|
);
|
2022-07-24 12:33:05 +08:00
|
|
|
const radios = container.querySelectorAll('input');
|
2022-04-08 15:23:52 +08:00
|
|
|
|
|
|
|
// controlled component
|
2022-07-24 12:33:05 +08:00
|
|
|
rerender(createRadioGroup({ onChange, value: 'A' }));
|
|
|
|
fireEvent.click(radios[0]);
|
2022-04-08 15:23:52 +08:00
|
|
|
expect(onChange.mock.calls.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('all children should have a name property', () => {
|
2022-07-24 12:33:05 +08:00
|
|
|
const GROUP_NAME = 'GROUP_NAME';
|
|
|
|
const { container } = render(createRadioGroup({ name: GROUP_NAME }));
|
2022-04-08 15:23:52 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
container.querySelectorAll<HTMLInputElement>('input[type="radio"]').forEach(el => {
|
2022-07-24 12:33:05 +08:00
|
|
|
expect(el.name).toEqual(GROUP_NAME);
|
2022-04-08 15:23:52 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes prefixCls down to radio', () => {
|
|
|
|
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();
|
2022-04-08 15:23:52 +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(
|
2022-04-08 15:23:52 +08:00
|
|
|
createRadioGroup({
|
2022-09-05 19:41:32 +08:00
|
|
|
ref(ref: HTMLDivElement) {
|
2022-04-08 15:23:52 +08:00
|
|
|
radioGroupRef = ref;
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
expect(radioGroupRef!).toBe(container.querySelector('.ant-radio-group'));
|
2022-04-08 15:23:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support data-* or aria-* props', () => {
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container } = render(
|
2022-04-08 15:23:52 +08:00
|
|
|
createRadioGroup({
|
|
|
|
'data-radio-group-id': 'radio-group-id',
|
|
|
|
'aria-label': 'radio-group',
|
2022-09-05 19:41:32 +08:00
|
|
|
} as RadioGroupProps),
|
|
|
|
);
|
|
|
|
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',
|
2022-04-08 15:23:52 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Radio type should not be override', () => {
|
|
|
|
const onChange = jest.fn();
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container } = render(
|
2022-04-08 15:23:52 +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]);
|
2022-04-08 15:23:52 +08:00
|
|
|
expect(onChange).toHaveBeenCalled();
|
2022-07-24 12:33:05 +08:00
|
|
|
expect(radios[1].type).toBe('radio');
|
2022-04-08 15:23:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('value is null or undefined', () => {
|
|
|
|
it('use `defaultValue` when `value` is undefined', () => {
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container } = render(
|
2022-04-08 15:23:52 +08:00
|
|
|
<Radio.Group defaultValue="bamboo" value={undefined}>
|
|
|
|
<Button value="bamboo">Bamboo</Button>
|
|
|
|
</Radio.Group>,
|
|
|
|
);
|
2022-07-24 12:33:05 +08:00
|
|
|
expect(container.querySelectorAll('.ant-radio-button-wrapper-checked').length).toBe(1);
|
2022-04-08 15:23:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
[undefined, null].forEach(newValue => {
|
|
|
|
it(`should set value back when value change back to ${newValue}`, () => {
|
2022-07-24 12:33:05 +08:00
|
|
|
const { container, rerender } = render(
|
2022-04-08 15:23:52 +08:00
|
|
|
<Radio.Group value="bamboo">
|
|
|
|
<Button value="bamboo">Bamboo</Button>
|
|
|
|
</Radio.Group>,
|
|
|
|
);
|
2022-07-24 12:33:05 +08:00
|
|
|
expect(container.querySelectorAll('.ant-radio-button-wrapper-checked').length).toBe(1);
|
|
|
|
rerender(
|
|
|
|
<Radio.Group value={newValue}>
|
|
|
|
<Button value="bamboo">Bamboo</Button>
|
|
|
|
</Radio.Group>,
|
|
|
|
);
|
|
|
|
expect(container.querySelectorAll('.ant-radio-button-wrapper-checked').length).toBe(0);
|
2022-04-08 15:23:52 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|