2016-12-14 14:48:09 +08:00
|
|
|
import React from 'react';
|
2016-12-19 10:52:47 +08:00
|
|
|
import { shallow, mount } 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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-14 14:48:09 +08:00
|
|
|
it('responses hover events', () => {
|
|
|
|
const onMouseEnter = jest.fn();
|
|
|
|
const onMouseLeave = jest.fn();
|
|
|
|
|
|
|
|
const wrapper = shallow(
|
|
|
|
<RadioGroup
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
>
|
|
|
|
<Radio />
|
|
|
|
</RadioGroup>
|
|
|
|
);
|
|
|
|
|
|
|
|
wrapper.simulate('mouseenter');
|
|
|
|
expect(onMouseEnter).toHaveBeenCalled();
|
|
|
|
|
|
|
|
wrapper.simulate('mouseleave');
|
|
|
|
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({
|
|
|
|
onChange
|
|
|
|
})
|
|
|
|
);
|
|
|
|
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({
|
|
|
|
onChange
|
|
|
|
})
|
|
|
|
);
|
|
|
|
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);
|
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|