2016-12-14 14:48:09 +08:00
|
|
|
import React from 'react';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { mount, render } from 'enzyme';
|
2021-10-13 17:13:07 +08:00
|
|
|
import Radio from '..';
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2019-08-26 22:53:20 +08:00
|
|
|
describe('Radio Group', () => {
|
2016-12-19 10:52:47 +08:00
|
|
|
function createRadioGroup(props) {
|
|
|
|
return (
|
2021-10-13 17:13:07 +08:00
|
|
|
<Radio.Group {...props}>
|
2016-12-19 10:52:47 +08:00
|
|
|
<Radio value="A">A</Radio>
|
|
|
|
<Radio value="B">B</Radio>
|
|
|
|
<Radio value="C">C</Radio>
|
2021-10-13 17:13:07 +08:00
|
|
|
</Radio.Group>
|
2016-12-19 10:52:47 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-03-28 11:56:38 +08:00
|
|
|
function createRadioGroupByOption(props) {
|
|
|
|
const options = [
|
|
|
|
{ label: 'A', value: 'A' },
|
|
|
|
{ label: 'B', value: 'B' },
|
|
|
|
{ label: 'C', value: 'C' },
|
|
|
|
];
|
|
|
|
|
2021-10-13 17:13:07 +08:00
|
|
|
return <Radio.Group {...props} options={options} />;
|
2017-03-28 11:56:38 +08:00
|
|
|
}
|
|
|
|
|
2016-12-14 14:48:09 +08:00
|
|
|
it('responses hover events', () => {
|
|
|
|
const onMouseEnter = jest.fn();
|
|
|
|
const onMouseLeave = jest.fn();
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
const wrapper = mount(
|
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
|
|
|
);
|
|
|
|
|
2020-05-26 22:26:51 +08:00
|
|
|
wrapper.find('div').at(0).simulate('mouseenter');
|
2016-12-14 14:48:09 +08:00
|
|
|
expect(onMouseEnter).toHaveBeenCalled();
|
|
|
|
|
2020-05-26 22:26:51 +08:00
|
|
|
wrapper.find('div').at(0).simulate('mouseleave');
|
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', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
createRadioGroup({
|
2017-01-08 17:04:09 +08:00
|
|
|
onChange,
|
2018-12-07 20:02:01 +08:00
|
|
|
}),
|
2016-12-19 10:52:47 +08:00
|
|
|
);
|
|
|
|
const radios = wrapper.find('input');
|
|
|
|
|
|
|
|
// controlled component
|
|
|
|
wrapper.setProps({ value: 'A' });
|
|
|
|
radios.at(1).simulate('change');
|
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', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const onChangeRadioGroup = jest.fn();
|
|
|
|
|
|
|
|
const wrapper = mount(
|
2021-10-13 17:13:07 +08:00
|
|
|
<Radio.Group onChange={onChangeRadioGroup}>
|
2019-01-18 16:12:47 +08:00
|
|
|
<Radio value="A" onChange={onChange}>
|
|
|
|
A
|
|
|
|
</Radio>
|
|
|
|
<Radio value="B" onChange={onChange}>
|
|
|
|
B
|
|
|
|
</Radio>
|
|
|
|
<Radio value="C" onChange={onChange}>
|
|
|
|
C
|
|
|
|
</Radio>
|
2021-10-13 17:13:07 +08:00
|
|
|
</Radio.Group>,
|
2019-01-18 16:12:47 +08:00
|
|
|
);
|
|
|
|
const radios = wrapper.find('input');
|
|
|
|
|
|
|
|
// controlled component
|
|
|
|
wrapper.setProps({ value: 'A' });
|
|
|
|
radios.at(1).simulate('change');
|
2020-05-27 10:21:17 +08:00
|
|
|
expect(onChange.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', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
const wrapper = mount(
|
2021-10-13 17:13:07 +08:00
|
|
|
<Radio.Group onChange={onChange}>
|
|
|
|
<Radio.Button value="A">A</Radio.Button>
|
|
|
|
<Radio.Button value="B">B</Radio.Button>
|
|
|
|
<Radio.Button value="C">C</Radio.Button>
|
|
|
|
</Radio.Group>,
|
2019-01-25 10:32:30 +08:00
|
|
|
);
|
|
|
|
const radios = wrapper.find('input');
|
|
|
|
|
|
|
|
// controlled component
|
|
|
|
wrapper.setProps({ value: 'A' });
|
|
|
|
radios.at(1).simulate('change');
|
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', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const options = [{ label: 'Bamboo', value: 'Bamboo' }];
|
2021-10-13 17:13:07 +08:00
|
|
|
const wrapper = mount(<Radio.Group options={options} onChange={onChange} />);
|
2019-02-21 20:53:19 +08:00
|
|
|
|
|
|
|
wrapper.find('input').simulate('change');
|
|
|
|
expect(onChange).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
it("won't fire change events when value not changes", () => {
|
2016-12-19 10:52:47 +08:00
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
createRadioGroup({
|
2017-01-08 17:04:09 +08:00
|
|
|
onChange,
|
2018-12-07 20:02:01 +08:00
|
|
|
}),
|
2016-12-19 10:52:47 +08:00
|
|
|
);
|
|
|
|
const radios = wrapper.find('input');
|
|
|
|
|
|
|
|
// controlled component
|
|
|
|
wrapper.setProps({ value: 'A' });
|
|
|
|
radios.at(0).simulate('change');
|
|
|
|
expect(onChange.mock.calls.length).toBe(0);
|
|
|
|
});
|
2017-03-28 11:56:38 +08:00
|
|
|
|
|
|
|
it('optional should correct render', () => {
|
2018-12-07 20:02:01 +08:00
|
|
|
const wrapper = mount(createRadioGroupByOption());
|
2017-03-28 11:56:38 +08:00
|
|
|
const radios = wrapper.find('input');
|
|
|
|
|
|
|
|
expect(radios.length).toBe(3);
|
|
|
|
});
|
2017-07-31 11:33:47 +08:00
|
|
|
|
|
|
|
it('all children should have a name property', () => {
|
|
|
|
const GROUP_NAME = 'radiogroup';
|
2018-12-07 20:02:01 +08:00
|
|
|
const wrapper = mount(createRadioGroup({ name: GROUP_NAME }));
|
2017-07-31 11:33:47 +08:00
|
|
|
|
2019-04-03 15:54:26 +08:00
|
|
|
wrapper.find('input[type="radio"]').forEach(el => {
|
|
|
|
expect(el.props().name).toEqual(GROUP_NAME);
|
|
|
|
});
|
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 } },
|
|
|
|
];
|
2021-10-13 17:13:07 +08:00
|
|
|
const wrapper = render(<Radio.Group prefixCls="my-radio" options={options} />);
|
2018-04-22 16:35:34 +08:00
|
|
|
expect(wrapper).toMatchSnapshot();
|
|
|
|
});
|
2020-03-16 12:47:24 +08:00
|
|
|
|
2020-09-03 15:31:08 +08:00
|
|
|
it('should forward ref', () => {
|
|
|
|
let radioGroupRef;
|
|
|
|
const wrapper = mount(
|
|
|
|
createRadioGroupByOption({
|
|
|
|
ref: ref => {
|
|
|
|
radioGroupRef = ref;
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(radioGroupRef).toBe(wrapper.children().getDOMNode());
|
|
|
|
});
|
|
|
|
|
2021-10-13 17:13:07 +08:00
|
|
|
it('should support data-* or aria-* props', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
createRadioGroup({
|
|
|
|
'data-radio-group-id': 'radio-group-id',
|
|
|
|
'aria-label': 'radio-group',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(wrapper.getDOMNode().getAttribute('data-radio-group-id')).toBe('radio-group-id');
|
|
|
|
expect(wrapper.getDOMNode().getAttribute('aria-label')).toBe('radio-group');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Radio type should not be override', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<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>,
|
|
|
|
);
|
|
|
|
const radios = wrapper.find('input');
|
|
|
|
radios.at(1).simulate('change');
|
|
|
|
expect(onChange).toHaveBeenCalled();
|
|
|
|
expect(radios.at(1).getDOMNode().type).toBe('radio');
|
|
|
|
});
|
|
|
|
|
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' }];
|
|
|
|
const wrapper = mount(
|
2021-10-13 17:13:07 +08:00
|
|
|
<Radio.Group defaultValue="bamboo" value={undefined} options={options} />,
|
2020-03-16 12:47:24 +08:00
|
|
|
);
|
2020-05-26 22:26:51 +08:00
|
|
|
expect(wrapper.find('.ant-radio-wrapper').at(0).hasClass('ant-radio-wrapper-checked')).toBe(
|
|
|
|
true,
|
|
|
|
);
|
2020-03-16 12:47:24 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
[undefined, null].forEach(newValue => {
|
|
|
|
it(`should set value back when value change back to ${newValue}`, () => {
|
|
|
|
const options = [{ label: 'Bamboo', value: 'bamboo' }];
|
2021-10-13 17:13:07 +08:00
|
|
|
const wrapper = mount(<Radio.Group value="bamboo" options={options} />);
|
2020-05-26 22:26:51 +08:00
|
|
|
expect(wrapper.find('.ant-radio-wrapper').at(0).hasClass('ant-radio-wrapper-checked')).toBe(
|
|
|
|
true,
|
|
|
|
);
|
2020-03-16 12:47:24 +08:00
|
|
|
wrapper.setProps({ value: newValue });
|
2020-05-27 10:21:17 +08:00
|
|
|
wrapper.update();
|
2020-05-26 22:26:51 +08:00
|
|
|
expect(wrapper.find('.ant-radio-wrapper').at(0).hasClass('ant-radio-wrapper-checked')).toBe(
|
|
|
|
false,
|
|
|
|
);
|
2020-03-16 12:47:24 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|