2019-01-12 03:33:27 +00:00
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import { asyncExpect } from '@/tests/utils';
|
|
|
|
import Checkbox from '../index';
|
2018-05-20 13:42:23 +00:00
|
|
|
|
|
|
|
describe('CheckboxGroup', () => {
|
|
|
|
it('should work basically', () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const onChange = jest.fn();
|
2018-05-20 13:42:23 +00:00
|
|
|
const wrapper = mount(
|
|
|
|
{
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
return <Checkbox.Group options={['Apple', 'Pear', 'Orange']} onChange={onChange} />;
|
2018-05-20 13:42:23 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
|
|
|
{
|
2018-09-05 13:28:54 +00:00
|
|
|
sync: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
wrapper
|
|
|
|
.findAll('.ant-checkbox-input')
|
|
|
|
.at(0)
|
|
|
|
.trigger('change');
|
|
|
|
expect(onChange).toBeCalledWith(['Apple']);
|
|
|
|
wrapper
|
|
|
|
.findAll('.ant-checkbox-input')
|
|
|
|
.at(1)
|
|
|
|
.trigger('change');
|
|
|
|
expect(onChange).toBeCalledWith(['Apple', 'Pear']);
|
|
|
|
wrapper
|
|
|
|
.findAll('.ant-checkbox-input')
|
|
|
|
.at(2)
|
|
|
|
.trigger('change');
|
|
|
|
expect(onChange).toBeCalledWith(['Apple', 'Pear', 'Orange']);
|
|
|
|
wrapper
|
|
|
|
.findAll('.ant-checkbox-input')
|
|
|
|
.at(1)
|
|
|
|
.trigger('change');
|
|
|
|
expect(onChange).toBeCalledWith(['Apple', 'Orange']);
|
|
|
|
});
|
2018-05-20 13:42:23 +00:00
|
|
|
|
|
|
|
it('does not trigger onChange callback of both Checkbox and CheckboxGroup when CheckboxGroup is disabled', () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const onChangeGroup = jest.fn();
|
2018-05-20 13:42:23 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const options = [{ label: 'Apple', value: 'Apple' }, { label: 'Pear', value: 'Pear' }];
|
2018-05-20 13:42:23 +00:00
|
|
|
|
|
|
|
const groupWrapper = mount(
|
|
|
|
{
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
return <Checkbox.Group options={options} onChange={onChangeGroup} disabled />;
|
2018-05-20 13:42:23 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
|
|
|
{
|
2018-09-05 13:28:54 +00:00
|
|
|
sync: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
groupWrapper
|
|
|
|
.findAll('.ant-checkbox-input')
|
|
|
|
.at(0)
|
|
|
|
.trigger('change');
|
|
|
|
expect(onChangeGroup).not.toBeCalled();
|
|
|
|
groupWrapper
|
|
|
|
.findAll('.ant-checkbox-input')
|
|
|
|
.at(1)
|
|
|
|
.trigger('change');
|
|
|
|
expect(onChangeGroup).not.toBeCalled();
|
|
|
|
});
|
2018-05-20 13:42:23 +00:00
|
|
|
|
|
|
|
it('does not prevent onChange callback from Checkbox when CheckboxGroup is not disabled', () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const onChangeGroup = jest.fn();
|
2018-05-20 13:42:23 +00:00
|
|
|
|
|
|
|
const options = [
|
|
|
|
{ label: 'Apple', value: 'Apple' },
|
|
|
|
{ label: 'Orange', value: 'Orange', disabled: true },
|
2019-01-12 03:33:27 +00:00
|
|
|
];
|
2018-05-20 13:42:23 +00:00
|
|
|
|
|
|
|
const groupWrapper = mount(
|
|
|
|
{
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
return <Checkbox.Group options={options} onChange={onChangeGroup} />;
|
2018-05-20 13:42:23 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
|
|
|
{
|
2018-09-05 13:28:54 +00:00
|
|
|
sync: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
groupWrapper
|
|
|
|
.findAll('.ant-checkbox-input')
|
|
|
|
.at(0)
|
|
|
|
.trigger('change');
|
|
|
|
expect(onChangeGroup).toBeCalledWith(['Apple']);
|
|
|
|
groupWrapper
|
|
|
|
.findAll('.ant-checkbox-input')
|
|
|
|
.at(1)
|
|
|
|
.trigger('change');
|
|
|
|
expect(onChangeGroup).toBeCalledWith(['Apple']);
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
it('passes prefixCls down to checkbox', () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const options = [{ label: 'Apple', value: 'Apple' }, { label: 'Orange', value: 'Orange' }];
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount({
|
|
|
|
render() {
|
|
|
|
return <Checkbox.Group prefixCls="my-checkbox" options={options} />;
|
|
|
|
},
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
it('should be controlled by value', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const options = [{ label: 'Apple', value: 'Apple' }, { label: 'Orange', value: 'Orange' }];
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
const wrapper = mount(Checkbox.Group, {
|
|
|
|
propsData: { options },
|
|
|
|
sync: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper.vm.sValue).toEqual([]);
|
|
|
|
wrapper.setProps({ value: ['Apple'] });
|
2018-09-05 13:28:54 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper.vm.sValue).toEqual(['Apple']);
|
|
|
|
});
|
|
|
|
});
|
2018-11-30 14:58:46 +00:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/12642
|
|
|
|
it('should trigger onChange in sub Checkbox', () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const onChange = jest.fn();
|
2018-11-30 14:58:46 +00:00
|
|
|
const wrapper = mount({
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2018-11-30 14:58:46 +00:00
|
|
|
return (
|
|
|
|
<Checkbox.Group>
|
2019-01-12 03:33:27 +00:00
|
|
|
<Checkbox value="my" onChange={onChange} />
|
2018-11-30 14:58:46 +00:00
|
|
|
</Checkbox.Group>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-11-30 14:58:46 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
wrapper
|
|
|
|
.findAll('.ant-checkbox-input')
|
|
|
|
.at(0)
|
|
|
|
.trigger('change');
|
|
|
|
expect(onChange).toBeCalled();
|
|
|
|
expect(onChange.mock.calls[0][0].target.value).toEqual('my');
|
|
|
|
});
|
|
|
|
});
|