2019-01-12 03:33:27 +00:00
|
|
|
import { mount } from '@vue/test-utils';
|
2021-09-25 08:51:32 +00:00
|
|
|
import { asyncExpect, sleep } from '../../../tests/utils';
|
2019-01-12 03:33:27 +00:00
|
|
|
import Checkbox from '../index';
|
2020-03-07 11:45:13 +00:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2018-05-20 13:42:23 +00:00
|
|
|
|
|
|
|
describe('CheckboxGroup', () => {
|
2020-03-07 11:45:13 +00:00
|
|
|
mountTest(Checkbox.Group);
|
2021-03-16 07:39:44 +00:00
|
|
|
it('should work basically', async () => {
|
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
|
|
|
},
|
|
|
|
);
|
2020-07-28 08:04:12 +00:00
|
|
|
wrapper.findAll('.ant-checkbox-input')[0].trigger('change');
|
2021-03-16 07:39:44 +00:00
|
|
|
await sleep();
|
2020-03-07 11:45:13 +00:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Apple']);
|
2020-07-28 08:04:12 +00:00
|
|
|
wrapper.findAll('.ant-checkbox-input')[1].trigger('change');
|
2021-03-16 07:39:44 +00:00
|
|
|
await sleep();
|
2020-03-07 11:45:13 +00:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Apple', 'Pear']);
|
2020-07-28 08:04:12 +00:00
|
|
|
wrapper.findAll('.ant-checkbox-input')[2].trigger('change');
|
2021-03-16 07:39:44 +00:00
|
|
|
await sleep();
|
2020-03-07 11:45:13 +00:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Apple', 'Pear', 'Orange']);
|
2020-07-28 08:04:12 +00:00
|
|
|
wrapper.findAll('.ant-checkbox-input')[1].trigger('change');
|
2021-03-16 07:39:44 +00:00
|
|
|
await sleep();
|
2020-03-07 11:45:13 +00:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Apple', 'Orange']);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
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-12-21 10:37:33 +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
|
|
|
},
|
|
|
|
);
|
2020-07-28 08:04:12 +00:00
|
|
|
groupWrapper.findAll('.ant-checkbox-input')[0].trigger('change');
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(onChangeGroup).not.toBeCalled();
|
2020-07-28 08:04:12 +00:00
|
|
|
groupWrapper.findAll('.ant-checkbox-input')[1].trigger('change');
|
2019-01-12 03:33:27 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
);
|
2020-07-28 08:04:12 +00:00
|
|
|
groupWrapper.findAll('.ant-checkbox-input')[0].trigger('change');
|
2020-03-07 11:45:13 +00:00
|
|
|
expect(onChangeGroup).toHaveBeenCalledWith(['Apple']);
|
2020-07-28 08:04:12 +00:00
|
|
|
groupWrapper.findAll('.ant-checkbox-input')[1].trigger('change');
|
2020-03-07 11:45:13 +00:00
|
|
|
expect(onChangeGroup).toHaveBeenCalledWith(['Apple']);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
it('passes prefixCls down to checkbox', () => {
|
2019-12-21 10:37:33 +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-12-21 10:37:33 +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, {
|
2020-07-25 07:46:49 +00:00
|
|
|
props: { options },
|
2018-09-05 13:28:54 +00:00
|
|
|
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
|
|
|
});
|
2020-07-28 08:04:12 +00:00
|
|
|
wrapper.findAll('.ant-checkbox-input')[0].trigger('change');
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(onChange).toBeCalled();
|
|
|
|
expect(onChange.mock.calls[0][0].target.value).toEqual('my');
|
|
|
|
});
|
|
|
|
});
|