2019-01-12 11:33:27 +08:00
|
|
|
import { mount } from '@vue/test-utils';
|
2021-09-25 16:51:32 +08:00
|
|
|
import { asyncExpect, sleep } from '../../../tests/utils';
|
2019-01-12 11:33:27 +08:00
|
|
|
import Checkbox from '../index';
|
2020-03-07 19:45:13 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2018-05-20 21:42:23 +08:00
|
|
|
|
|
|
|
describe('CheckboxGroup', () => {
|
2020-03-07 19:45:13 +08:00
|
|
|
mountTest(Checkbox.Group);
|
2021-03-16 15:39:44 +08:00
|
|
|
it('should work basically', async () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const onChange = jest.fn();
|
2018-05-20 21:42:23 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
{
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
|
|
|
return <Checkbox.Group options={['Apple', 'Pear', 'Orange']} onChange={onChange} />;
|
2018-05-20 21:42:23 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
},
|
|
|
|
{
|
2018-09-05 21:28:54 +08:00
|
|
|
sync: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
},
|
|
|
|
);
|
2020-07-28 16:04:12 +08:00
|
|
|
wrapper.findAll('.ant-checkbox-input')[0].trigger('change');
|
2021-03-16 15:39:44 +08:00
|
|
|
await sleep();
|
2020-03-07 19:45:13 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Apple']);
|
2020-07-28 16:04:12 +08:00
|
|
|
wrapper.findAll('.ant-checkbox-input')[1].trigger('change');
|
2021-03-16 15:39:44 +08:00
|
|
|
await sleep();
|
2020-03-07 19:45:13 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Apple', 'Pear']);
|
2020-07-28 16:04:12 +08:00
|
|
|
wrapper.findAll('.ant-checkbox-input')[2].trigger('change');
|
2021-03-16 15:39:44 +08:00
|
|
|
await sleep();
|
2020-03-07 19:45:13 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Apple', 'Pear', 'Orange']);
|
2020-07-28 16:04:12 +08:00
|
|
|
wrapper.findAll('.ant-checkbox-input')[1].trigger('change');
|
2021-03-16 15:39:44 +08:00
|
|
|
await sleep();
|
2020-03-07 19:45:13 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Apple', 'Orange']);
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-05-20 21:42:23 +08:00
|
|
|
|
|
|
|
it('does not trigger onChange callback of both Checkbox and CheckboxGroup when CheckboxGroup is disabled', () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const onChangeGroup = jest.fn();
|
2018-05-20 21:42:23 +08:00
|
|
|
|
2019-12-21 18:37:33 +08:00
|
|
|
const options = [
|
|
|
|
{ label: 'Apple', value: 'Apple' },
|
|
|
|
{ label: 'Pear', value: 'Pear' },
|
|
|
|
];
|
2018-05-20 21:42:23 +08:00
|
|
|
|
|
|
|
const groupWrapper = mount(
|
|
|
|
{
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
|
|
|
return <Checkbox.Group options={options} onChange={onChangeGroup} disabled />;
|
2018-05-20 21:42:23 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
},
|
|
|
|
{
|
2018-09-05 21:28:54 +08:00
|
|
|
sync: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
},
|
|
|
|
);
|
2020-07-28 16:04:12 +08:00
|
|
|
groupWrapper.findAll('.ant-checkbox-input')[0].trigger('change');
|
2019-01-12 11:33:27 +08:00
|
|
|
expect(onChangeGroup).not.toBeCalled();
|
2020-07-28 16:04:12 +08:00
|
|
|
groupWrapper.findAll('.ant-checkbox-input')[1].trigger('change');
|
2019-01-12 11:33:27 +08:00
|
|
|
expect(onChangeGroup).not.toBeCalled();
|
|
|
|
});
|
2018-05-20 21:42:23 +08:00
|
|
|
|
|
|
|
it('does not prevent onChange callback from Checkbox when CheckboxGroup is not disabled', () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const onChangeGroup = jest.fn();
|
2018-05-20 21:42:23 +08:00
|
|
|
|
|
|
|
const options = [
|
|
|
|
{ label: 'Apple', value: 'Apple' },
|
|
|
|
{ label: 'Orange', value: 'Orange', disabled: true },
|
2019-01-12 11:33:27 +08:00
|
|
|
];
|
2018-05-20 21:42:23 +08:00
|
|
|
|
|
|
|
const groupWrapper = mount(
|
|
|
|
{
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
|
|
|
return <Checkbox.Group options={options} onChange={onChangeGroup} />;
|
2018-05-20 21:42:23 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
},
|
|
|
|
{
|
2018-09-05 21:28:54 +08:00
|
|
|
sync: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
},
|
|
|
|
);
|
2020-07-28 16:04:12 +08:00
|
|
|
groupWrapper.findAll('.ant-checkbox-input')[0].trigger('change');
|
2020-03-07 19:45:13 +08:00
|
|
|
expect(onChangeGroup).toHaveBeenCalledWith(['Apple']);
|
2020-07-28 16:04:12 +08:00
|
|
|
groupWrapper.findAll('.ant-checkbox-input')[1].trigger('change');
|
2020-03-07 19:45:13 +08:00
|
|
|
expect(onChangeGroup).toHaveBeenCalledWith(['Apple']);
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-09-05 21:28:54 +08:00
|
|
|
|
|
|
|
it('passes prefixCls down to checkbox', () => {
|
2019-12-21 18:37:33 +08:00
|
|
|
const options = [
|
|
|
|
{ label: 'Apple', value: 'Apple' },
|
|
|
|
{ label: 'Orange', value: 'Orange' },
|
|
|
|
];
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const wrapper = mount({
|
|
|
|
render() {
|
|
|
|
return <Checkbox.Group prefixCls="my-checkbox" options={options} />;
|
|
|
|
},
|
|
|
|
});
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
});
|
2018-09-05 21:28:54 +08:00
|
|
|
it('should be controlled by value', async () => {
|
2019-12-21 18:37:33 +08:00
|
|
|
const options = [
|
|
|
|
{ label: 'Apple', value: 'Apple' },
|
|
|
|
{ label: 'Orange', value: 'Orange' },
|
|
|
|
];
|
2018-09-05 21:28:54 +08:00
|
|
|
|
|
|
|
const wrapper = mount(Checkbox.Group, {
|
2020-07-25 15:46:49 +08:00
|
|
|
props: { options },
|
2018-09-05 21:28:54 +08:00
|
|
|
sync: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2021-12-28 16:47:52 +08:00
|
|
|
expect(wrapper.vm.mergedValue).toEqual([]);
|
2019-01-12 11:33:27 +08:00
|
|
|
wrapper.setProps({ value: ['Apple'] });
|
2018-09-05 21:28:54 +08:00
|
|
|
await asyncExpect(() => {
|
2021-12-28 16:47:52 +08:00
|
|
|
expect(wrapper.vm.mergedValue).toEqual(['Apple']);
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
});
|
2018-11-30 22:58:46 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/12642
|
|
|
|
it('should trigger onChange in sub Checkbox', () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const onChange = jest.fn();
|
2018-11-30 22:58:46 +08:00
|
|
|
const wrapper = mount({
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2018-11-30 22:58:46 +08:00
|
|
|
return (
|
|
|
|
<Checkbox.Group>
|
2019-01-12 11:33:27 +08:00
|
|
|
<Checkbox value="my" onChange={onChange} />
|
2018-11-30 22:58:46 +08:00
|
|
|
</Checkbox.Group>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-11-30 22:58:46 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2020-07-28 16:04:12 +08:00
|
|
|
wrapper.findAll('.ant-checkbox-input')[0].trigger('change');
|
2019-01-12 11:33:27 +08:00
|
|
|
expect(onChange).toBeCalled();
|
|
|
|
expect(onChange.mock.calls[0][0].target.value).toEqual('my');
|
|
|
|
});
|
|
|
|
});
|