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';
|
2016-12-14 14:48:09 +08:00
|
|
|
import Radio from '../radio';
|
|
|
|
import RadioGroup from '../group';
|
|
|
|
|
|
|
|
describe('Radio', () => {
|
2016-12-19 10:52:47 +08:00
|
|
|
function createRadioGroup(props) {
|
|
|
|
return (
|
|
|
|
<RadioGroup
|
|
|
|
{...props}
|
|
|
|
>
|
|
|
|
<Radio value="A">A</Radio>
|
|
|
|
<Radio value="B">B</Radio>
|
|
|
|
<Radio value="C">C</Radio>
|
|
|
|
</RadioGroup>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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' },
|
|
|
|
];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RadioGroup
|
|
|
|
{...props}
|
|
|
|
options={options}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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(
|
2016-12-14 14:48:09 +08:00
|
|
|
<RadioGroup
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
>
|
|
|
|
<Radio />
|
|
|
|
</RadioGroup>
|
|
|
|
);
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
wrapper.find('div').at(0).simulate('mouseenter');
|
2016-12-14 14:48:09 +08:00
|
|
|
expect(onMouseEnter).toHaveBeenCalled();
|
|
|
|
|
2018-12-05 19:12:18 +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,
|
2016-12-19 10:52:47 +08:00
|
|
|
})
|
|
|
|
);
|
|
|
|
const radios = wrapper.find('input');
|
|
|
|
|
|
|
|
// uncontrolled component
|
|
|
|
wrapper.setState({ value: 'B' });
|
|
|
|
radios.at(0).simulate('change');
|
|
|
|
expect(onChange.mock.calls.length).toBe(1);
|
|
|
|
|
|
|
|
// controlled component
|
|
|
|
wrapper.setProps({ value: 'A' });
|
|
|
|
radios.at(1).simulate('change');
|
|
|
|
expect(onChange.mock.calls.length).toBe(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('won\'t fire change events when value not changes', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
createRadioGroup({
|
2017-01-08 17:04:09 +08:00
|
|
|
onChange,
|
2016-12-19 10:52:47 +08:00
|
|
|
})
|
|
|
|
);
|
|
|
|
const radios = wrapper.find('input');
|
|
|
|
|
|
|
|
// uncontrolled component
|
|
|
|
wrapper.setState({ value: 'B' });
|
|
|
|
radios.at(1).simulate('change');
|
|
|
|
expect(onChange.mock.calls.length).toBe(0);
|
|
|
|
|
|
|
|
// 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', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
createRadioGroupByOption()
|
|
|
|
);
|
|
|
|
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';
|
|
|
|
const wrapper = mount(
|
|
|
|
createRadioGroup({ name: GROUP_NAME })
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(wrapper.find('input[type="radio"]').forEach((el) => {
|
|
|
|
expect(el.props().name).toEqual(GROUP_NAME);
|
|
|
|
}));
|
|
|
|
});
|
2018-04-22 16:35:34 +08:00
|
|
|
|
|
|
|
it('passes prefixCls down to radio', () => {
|
|
|
|
const options = [
|
|
|
|
{ label: 'Apple', value: 'Apple' },
|
|
|
|
{ label: 'Orange', value: 'Orange' },
|
|
|
|
];
|
|
|
|
|
|
|
|
const wrapper = render(
|
|
|
|
<RadioGroup prefixCls="my-radio" options={options} />
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(wrapper).toMatchSnapshot();
|
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|