2017-08-24 12:56:25 +08:00
|
|
|
import React from 'react';
|
2018-04-22 16:35:34 +08:00
|
|
|
import { mount, render } from 'enzyme';
|
2017-08-24 12:56:25 +08:00
|
|
|
import Checkbox from '../index';
|
|
|
|
|
|
|
|
describe('CheckboxGroup', () => {
|
|
|
|
it('should work basically', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const wrapper = mount(
|
2018-12-07 20:02:01 +08:00
|
|
|
<Checkbox.Group options={['Apple', 'Pear', 'Orange']} onChange={onChange} />,
|
2017-08-24 12:56:25 +08:00
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-checkbox-input')
|
|
|
|
.at(0)
|
|
|
|
.simulate('change');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Apple']);
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-checkbox-input')
|
|
|
|
.at(1)
|
|
|
|
.simulate('change');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Apple', 'Pear']);
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-checkbox-input')
|
|
|
|
.at(2)
|
|
|
|
.simulate('change');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Apple', 'Pear', 'Orange']);
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-checkbox-input')
|
|
|
|
.at(1)
|
|
|
|
.simulate('change');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Apple', 'Orange']);
|
2017-08-24 12:56:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not trigger onChange callback of both Checkbox and CheckboxGroup when CheckboxGroup is disabled', () => {
|
|
|
|
const onChangeGroup = jest.fn();
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
const options = [{ label: 'Apple', value: 'Apple' }, { label: 'Pear', value: 'Pear' }];
|
2017-08-24 12:56:25 +08:00
|
|
|
|
|
|
|
const groupWrapper = mount(
|
2018-12-07 20:02:01 +08:00
|
|
|
<Checkbox.Group options={options} onChange={onChangeGroup} disabled />,
|
2017-08-24 12:56:25 +08:00
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
groupWrapper
|
|
|
|
.find('.ant-checkbox-input')
|
|
|
|
.at(0)
|
|
|
|
.simulate('change');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChangeGroup).not.toHaveBeenCalled();
|
2018-12-07 20:02:01 +08:00
|
|
|
groupWrapper
|
|
|
|
.find('.ant-checkbox-input')
|
|
|
|
.at(1)
|
|
|
|
.simulate('change');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChangeGroup).not.toHaveBeenCalled();
|
2017-08-24 12:56:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not prevent onChange callback from Checkbox when CheckboxGroup is not disabled', () => {
|
|
|
|
const onChangeGroup = jest.fn();
|
|
|
|
|
|
|
|
const options = [
|
|
|
|
{ label: 'Apple', value: 'Apple' },
|
|
|
|
{ label: 'Orange', value: 'Orange', disabled: true },
|
|
|
|
];
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
const groupWrapper = mount(<Checkbox.Group options={options} onChange={onChangeGroup} />);
|
|
|
|
groupWrapper
|
|
|
|
.find('.ant-checkbox-input')
|
|
|
|
.at(0)
|
|
|
|
.simulate('change');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChangeGroup).toHaveBeenCalledWith(['Apple']);
|
2018-12-07 20:02:01 +08:00
|
|
|
groupWrapper
|
|
|
|
.find('.ant-checkbox-input')
|
|
|
|
.at(1)
|
|
|
|
.simulate('change');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChangeGroup).toHaveBeenCalledWith(['Apple']);
|
2017-08-24 12:56:25 +08:00
|
|
|
});
|
2018-04-22 16:35:34 +08:00
|
|
|
|
2019-04-01 10:16:18 +08:00
|
|
|
it('all children should have a name property', () => {
|
|
|
|
const wrapper = mount(<Checkbox.Group name="checkboxgroup" options={['Yes', 'No']} />);
|
2019-04-03 15:54:26 +08:00
|
|
|
wrapper.find('input[type="checkbox"]').forEach(el => {
|
|
|
|
expect(el.props().name).toEqual('checkboxgroup');
|
|
|
|
});
|
2019-04-01 10:16:18 +08:00
|
|
|
});
|
|
|
|
|
2018-04-22 16:35:34 +08:00
|
|
|
it('passes prefixCls down to checkbox', () => {
|
2018-12-07 20:02:01 +08:00
|
|
|
const options = [{ label: 'Apple', value: 'Apple' }, { label: 'Orange', value: 'Orange' }];
|
2018-04-22 16:35:34 +08:00
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
const wrapper = render(<Checkbox.Group prefixCls="my-checkbox" options={options} />);
|
2018-04-22 16:35:34 +08:00
|
|
|
|
|
|
|
expect(wrapper).toMatchSnapshot();
|
|
|
|
});
|
2018-08-10 17:04:40 +08:00
|
|
|
|
|
|
|
it('should be controlled by value', () => {
|
2018-12-07 20:02:01 +08:00
|
|
|
const options = [{ label: 'Apple', value: 'Apple' }, { label: 'Orange', value: 'Orange' }];
|
2018-08-10 17:04:40 +08:00
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
const wrapper = mount(<Checkbox.Group options={options} />);
|
2018-08-10 17:04:40 +08:00
|
|
|
|
|
|
|
expect(wrapper.instance().state.value).toEqual([]);
|
|
|
|
wrapper.setProps({ value: ['Apple'] });
|
|
|
|
expect(wrapper.instance().state.value).toEqual(['Apple']);
|
|
|
|
});
|
2018-10-23 23:40:40 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/12642
|
|
|
|
it('should trigger onChange in sub Checkbox', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Checkbox.Group>
|
|
|
|
<Checkbox value="my" onChange={onChange} />
|
2018-12-07 20:02:01 +08:00
|
|
|
</Checkbox.Group>,
|
2018-10-23 23:40:40 +08:00
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-checkbox-input')
|
|
|
|
.at(0)
|
|
|
|
.simulate('change');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChange).toHaveBeenCalled();
|
2018-10-23 23:40:40 +08:00
|
|
|
expect(onChange.mock.calls[0][0].target.value).toEqual('my');
|
|
|
|
});
|
2019-04-30 23:39:01 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/16376
|
|
|
|
it('onChange should filter removed value', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Checkbox.Group defaultValue={[1]} onChange={onChange}>
|
|
|
|
<Checkbox key={1} value={1} />
|
|
|
|
<Checkbox key={2} value={2} />
|
|
|
|
</Checkbox.Group>,
|
|
|
|
);
|
|
|
|
|
|
|
|
wrapper.setProps({
|
|
|
|
children: [<Checkbox key={2} value={2} />],
|
|
|
|
});
|
|
|
|
|
|
|
|
wrapper
|
|
|
|
.find('.ant-checkbox-input')
|
|
|
|
.at(0)
|
|
|
|
.simulate('change');
|
|
|
|
|
|
|
|
expect(onChange).toHaveBeenCalledWith([2]);
|
|
|
|
});
|
2017-08-24 12:56:25 +08:00
|
|
|
});
|