ant-design/components/radio/__tests__/group.test.js

182 lines
4.8 KiB
JavaScript
Raw Normal View History

2016-12-14 14:48:09 +08:00
import React from 'react';
import { mount, render } from 'enzyme';
2016-12-14 14:48:09 +08:00
import Radio from '../radio';
import RadioGroup from '../group';
import RadioButton from '../radioButton';
2016-12-14 14:48:09 +08:00
describe('Radio', () => {
function createRadioGroup(props) {
return (
2018-12-07 20:02:01 +08:00
<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' },
];
2018-12-07 20:02:01 +08:00
return <RadioGroup {...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();
const wrapper = mount(
2018-12-07 20:02:01 +08:00
<RadioGroup onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}>
2016-12-14 14:48:09 +08:00
<Radio />
2018-12-07 20:02:01 +08:00
</RadioGroup>,
2016-12-14 14:48:09 +08:00
);
2018-12-07 20:02:01 +08:00
wrapper
.find('div')
.at(0)
.simulate('mouseenter');
2016-12-14 14:48:09 +08:00
expect(onMouseEnter).toHaveBeenCalled();
2018-12-07 20:02:01 +08:00
wrapper
.find('div')
.at(0)
.simulate('mouseleave');
2016-12-14 14:48:09 +08:00
expect(onMouseLeave).toHaveBeenCalled();
});
it('fire change events when value changes', () => {
const onChange = jest.fn();
const wrapper = mount(
createRadioGroup({
onChange,
2018-12-07 20:02:01 +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('both of radio and radioGroup will trigger onchange event when they exists', () => {
const onChange = jest.fn();
const onChangeRadioGroup = jest.fn();
const wrapper = mount(
<RadioGroup onChange={onChangeRadioGroup}>
<Radio value="A" onChange={onChange}>
A
</Radio>
<Radio value="B" onChange={onChange}>
B
</Radio>
<Radio value="C" onChange={onChange}>
C
</Radio>
</RadioGroup>,
);
const radios = wrapper.find('input');
// uncontrolled component
wrapper.setState({ value: 'B' });
radios.at(0).simulate('change');
expect(onChange.mock.calls.length).toBe(1);
expect(onChangeRadioGroup.mock.calls.length).toBe(1);
// controlled component
wrapper.setProps({ value: 'A' });
radios.at(1).simulate('change');
expect(onChange.mock.calls.length).toBe(2);
});
it('Trigger onChange when both of radioButton and radioGroup exists', () => {
const onChange = jest.fn();
const wrapper = mount(
<RadioGroup onChange={onChange}>
<RadioButton value="A">A</RadioButton>
<RadioButton value="B">B</RadioButton>
<RadioButton value="C">C</RadioButton>
</RadioGroup>,
);
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('should only trigger once when in group with options', () => {
const onChange = jest.fn();
const options = [{ label: 'Bamboo', value: 'Bamboo' }];
const wrapper = mount(<RadioGroup options={options} onChange={onChange} />);
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", () => {
const onChange = jest.fn();
const wrapper = mount(
createRadioGroup({
onChange,
2018-12-07 20:02:01 +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', () => {
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);
});
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 }));
2018-12-07 20:02:01 +08:00
expect(
wrapper.find('input[type="radio"]').forEach(el => {
expect(el.props().name).toEqual(GROUP_NAME);
}),
);
});
it('passes prefixCls down to radio', () => {
2018-12-07 20:02:01 +08:00
const options = [{ label: 'Apple', value: 'Apple' }, { label: 'Orange', value: 'Orange' }];
2018-12-07 20:02:01 +08:00
const wrapper = render(<RadioGroup prefixCls="my-radio" options={options} />);
expect(wrapper).toMatchSnapshot();
});
2016-12-14 14:48:09 +08:00
});