2022-01-20 10:15:42 +08:00
|
|
|
import React, { useState } from 'react';
|
2020-01-31 21:25:34 +08:00
|
|
|
import Collapse from '../../collapse';
|
2020-12-28 18:54:02 +08:00
|
|
|
import Table from '../../table';
|
2017-08-24 12:56:25 +08:00
|
|
|
import Checkbox from '../index';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2022-04-18 21:02:11 +08:00
|
|
|
import { render, fireEvent } from '../../../tests/utils';
|
2022-01-20 10:15:42 +08:00
|
|
|
import Input from '../../input';
|
2017-08-24 12:56:25 +08:00
|
|
|
|
|
|
|
describe('CheckboxGroup', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Checkbox.Group);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Checkbox.Group);
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2017-08-24 12:56:25 +08:00
|
|
|
it('should work basically', () => {
|
|
|
|
const onChange = jest.fn();
|
2022-05-05 10:25:22 +08:00
|
|
|
const { container } = render(
|
2018-12-07 20:02:01 +08:00
|
|
|
<Checkbox.Group options={['Apple', 'Pear', 'Orange']} onChange={onChange} />,
|
2017-08-24 12:56:25 +08:00
|
|
|
);
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[0]);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Apple']);
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[1]);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Apple', 'Pear']);
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[2]);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Apple', 'Pear', 'Orange']);
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[1]);
|
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();
|
|
|
|
|
2020-01-06 11:13:39 +08:00
|
|
|
const options = [
|
|
|
|
{ label: 'Apple', value: 'Apple' },
|
|
|
|
{ label: 'Pear', value: 'Pear' },
|
|
|
|
];
|
2017-08-24 12:56:25 +08:00
|
|
|
|
2022-05-05 10:25:22 +08:00
|
|
|
const { container } = render(
|
2018-12-07 20:02:01 +08:00
|
|
|
<Checkbox.Group options={options} onChange={onChangeGroup} disabled />,
|
2017-08-24 12:56:25 +08:00
|
|
|
);
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[0]);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChangeGroup).not.toHaveBeenCalled();
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[1]);
|
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 },
|
|
|
|
];
|
|
|
|
|
2022-05-05 10:25:22 +08:00
|
|
|
const { container } = render(<Checkbox.Group options={options} onChange={onChangeGroup} />);
|
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[0]);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChangeGroup).toHaveBeenCalledWith(['Apple']);
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[1]);
|
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', () => {
|
2022-05-05 10:25:22 +08:00
|
|
|
const { container } = render(<Checkbox.Group name="checkboxgroup" options={['Yes', 'No']} />);
|
|
|
|
[...container.querySelectorAll('input[type="checkbox"]')].forEach(el => {
|
|
|
|
expect(el.getAttribute('name')).toEqual('checkboxgroup');
|
2019-04-03 15:54:26 +08:00
|
|
|
});
|
2019-04-01 10:16:18 +08:00
|
|
|
});
|
|
|
|
|
2018-04-22 16:35:34 +08:00
|
|
|
it('passes prefixCls down to checkbox', () => {
|
2020-01-06 11:13:39 +08:00
|
|
|
const options = [
|
|
|
|
{ label: 'Apple', value: 'Apple' },
|
2020-02-04 17:34:02 +08:00
|
|
|
{ label: 'Orange', value: 'Orange', style: { fontSize: 12 } },
|
2020-01-06 11:13:39 +08:00
|
|
|
];
|
2018-04-22 16:35:34 +08:00
|
|
|
|
2022-05-05 10:25:22 +08:00
|
|
|
const { container } = render(<Checkbox.Group prefixCls="my-checkbox" options={options} />);
|
2018-04-22 16:35:34 +08:00
|
|
|
|
2022-05-05 10:25:22 +08:00
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2018-04-22 16:35:34 +08:00
|
|
|
});
|
2018-08-10 17:04:40 +08:00
|
|
|
|
|
|
|
it('should be controlled by value', () => {
|
2020-01-06 11:13:39 +08:00
|
|
|
const options = [
|
|
|
|
{ label: 'Apple', value: 'Apple' },
|
|
|
|
{ label: 'Orange', value: 'Orange' },
|
|
|
|
];
|
2022-05-05 10:25:22 +08:00
|
|
|
const renderCheckbox = props => <Checkbox.Group {...props} />;
|
|
|
|
const { container, rerender } = render(renderCheckbox({ options }));
|
|
|
|
expect(container.querySelectorAll('.ant-checkbox-checked').length).toBe(0);
|
|
|
|
rerender(renderCheckbox({ options, value: 'Apple' }));
|
|
|
|
expect(container.querySelectorAll('.ant-checkbox-checked').length).toBe(1);
|
2018-08-10 17:04:40 +08:00
|
|
|
});
|
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();
|
2022-05-05 10:25:22 +08:00
|
|
|
const { container } = render(
|
2018-10-23 23:40:40 +08:00
|
|
|
<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
|
|
|
);
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[0]);
|
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();
|
2022-04-18 21:02:11 +08:00
|
|
|
const { container, rerender } = render(
|
2019-04-30 23:39:01 +08:00
|
|
|
<Checkbox.Group defaultValue={[1]} onChange={onChange}>
|
|
|
|
<Checkbox key={1} value={1} />
|
|
|
|
<Checkbox key={2} value={2} />
|
|
|
|
</Checkbox.Group>,
|
|
|
|
);
|
|
|
|
|
2022-04-18 21:02:11 +08:00
|
|
|
rerender(
|
|
|
|
<Checkbox.Group defaultValue={[1]} onChange={onChange}>
|
|
|
|
<Checkbox key={2} value={2} />
|
|
|
|
</Checkbox.Group>,
|
|
|
|
);
|
|
|
|
fireEvent.click(container.querySelector('.ant-checkbox-input'));
|
2019-04-30 23:39:01 +08:00
|
|
|
|
|
|
|
expect(onChange).toHaveBeenCalledWith([2]);
|
|
|
|
});
|
2019-06-28 11:11:27 +08:00
|
|
|
|
2020-02-28 11:11:00 +08:00
|
|
|
it('checkbox should register value again after value changed', () => {
|
|
|
|
const onChange = jest.fn();
|
2022-04-18 21:02:11 +08:00
|
|
|
const { container, rerender } = render(
|
2020-02-28 11:11:00 +08:00
|
|
|
<Checkbox.Group defaultValue={[1]} onChange={onChange}>
|
|
|
|
<Checkbox key={1} value={1} />
|
|
|
|
</Checkbox.Group>,
|
|
|
|
);
|
|
|
|
|
2022-04-18 21:02:11 +08:00
|
|
|
rerender(
|
|
|
|
<Checkbox.Group defaultValue={[1]} onChange={onChange}>
|
|
|
|
<Checkbox key={1} value={2} />
|
|
|
|
</Checkbox.Group>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(container.querySelector('.ant-checkbox-input')).toHaveAttribute('checked');
|
2020-02-28 11:11:00 +08:00
|
|
|
});
|
|
|
|
|
2019-06-28 11:11:27 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/17297
|
|
|
|
it('onChange should keep the order of the original values', () => {
|
|
|
|
const onChange = jest.fn();
|
2022-05-05 10:25:22 +08:00
|
|
|
const { container } = render(
|
2019-06-28 11:11:27 +08:00
|
|
|
<Checkbox.Group onChange={onChange}>
|
|
|
|
<Checkbox key={1} value={1} />
|
|
|
|
<Checkbox key={2} value={2} />
|
|
|
|
<Checkbox key={3} value={3} />
|
|
|
|
<Checkbox key={4} value={4} />
|
|
|
|
</Checkbox.Group>,
|
|
|
|
);
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[0]);
|
2019-06-28 11:11:27 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith([1]);
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[1]);
|
2019-06-28 11:11:27 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith([1, 2]);
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[0]);
|
2019-06-28 11:11:27 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith([2]);
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[0]);
|
2019-06-28 11:11:27 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith([1, 2]);
|
|
|
|
});
|
2020-01-31 21:25:34 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/21134
|
|
|
|
it('should work when checkbox is wrapped by other components', () => {
|
2022-05-05 10:25:22 +08:00
|
|
|
const { container } = render(
|
2020-01-31 21:25:34 +08:00
|
|
|
<Checkbox.Group>
|
|
|
|
<Collapse bordered={false}>
|
|
|
|
<Collapse.Panel header="test panel">
|
|
|
|
<div>
|
|
|
|
<Checkbox value="1">item</Checkbox>
|
|
|
|
</div>
|
|
|
|
</Collapse.Panel>
|
|
|
|
</Collapse>
|
|
|
|
</Checkbox.Group>,
|
|
|
|
);
|
2022-05-05 10:25:22 +08:00
|
|
|
|
|
|
|
fireEvent.click(
|
|
|
|
container.querySelector('.ant-collapse-item').querySelector('.ant-collapse-header'),
|
|
|
|
);
|
|
|
|
fireEvent.click(container.querySelector('.ant-checkbox-input'));
|
|
|
|
expect(container.querySelectorAll('.ant-checkbox-checked').length).toBe(1);
|
|
|
|
fireEvent.click(container.querySelector('.ant-checkbox-input'));
|
|
|
|
expect(container.querySelectorAll('.ant-checkbox-checked').length).toBe(0);
|
2020-01-31 21:25:34 +08:00
|
|
|
});
|
2020-12-28 18:54:02 +08:00
|
|
|
|
|
|
|
it('skipGroup', () => {
|
|
|
|
const onChange = jest.fn();
|
2022-05-05 10:25:22 +08:00
|
|
|
const { container } = render(
|
2020-12-28 18:54:02 +08:00
|
|
|
<Checkbox.Group onChange={onChange}>
|
|
|
|
<Checkbox value={1} />
|
|
|
|
<Checkbox value={2} skipGroup />
|
|
|
|
</Checkbox.Group>,
|
|
|
|
);
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[1]);
|
2020-12-28 18:54:02 +08:00
|
|
|
expect(onChange).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Table rowSelection', () => {
|
|
|
|
const onChange = jest.fn();
|
2022-05-05 10:25:22 +08:00
|
|
|
const { container } = render(
|
2020-12-28 18:54:02 +08:00
|
|
|
<Checkbox.Group onChange={onChange}>
|
|
|
|
<Table
|
|
|
|
dataSource={[{ key: 1, value: '1' }]}
|
|
|
|
columns={[{ title: 'title', dataIndex: 'value' }]}
|
|
|
|
rowSelection={{}}
|
|
|
|
/>
|
|
|
|
</Checkbox.Group>,
|
|
|
|
);
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[1]);
|
2020-12-28 18:54:02 +08:00
|
|
|
expect(onChange).not.toHaveBeenCalled();
|
|
|
|
});
|
2021-04-08 14:36:50 +08:00
|
|
|
|
|
|
|
it('should get div ref', () => {
|
2022-05-05 10:25:22 +08:00
|
|
|
const refCalls = [];
|
|
|
|
render(
|
2021-04-08 14:36:50 +08:00
|
|
|
<Checkbox.Group
|
|
|
|
options={['Apple', 'Pear', 'Orange']}
|
|
|
|
ref={node => {
|
2022-05-05 10:25:22 +08:00
|
|
|
refCalls.push(node);
|
2021-04-08 14:36:50 +08:00
|
|
|
}}
|
|
|
|
/>,
|
|
|
|
);
|
2022-05-05 10:25:22 +08:00
|
|
|
const [mountCall] = refCalls;
|
|
|
|
expect(mountCall.nodeName).toBe('DIV');
|
2021-04-08 14:36:50 +08:00
|
|
|
});
|
2022-01-12 22:57:43 +08:00
|
|
|
|
|
|
|
it('should support number option', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
2022-05-05 10:25:22 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Checkbox.Group options={[1, 'Pear', 'Orange']} onChange={onChange} />,
|
|
|
|
);
|
2022-01-12 22:57:43 +08:00
|
|
|
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-checkbox-input'));
|
2022-01-12 22:57:43 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith([1]);
|
|
|
|
});
|
2022-01-20 10:15:42 +08:00
|
|
|
|
|
|
|
it('should store latest checkbox value if changed', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
const Demo = () => {
|
|
|
|
const [v, setV] = useState('');
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
setTimeout(setV('1'), 1000);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Input className="my-input" value={v} onChange={e => setV(e.target.value)} />
|
|
|
|
<Checkbox.Group defaultValue={['length1']} style={{ width: '100%' }} onChange={onChange}>
|
|
|
|
<Checkbox className="target-checkbox" value={v ? `length${v}` : 'A'}>
|
|
|
|
A
|
|
|
|
</Checkbox>
|
|
|
|
</Checkbox.Group>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-05-05 10:25:22 +08:00
|
|
|
const { container } = render(<Demo />);
|
|
|
|
fireEvent.click(container.querySelector('.ant-checkbox-input'));
|
2022-01-20 10:15:42 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith([]);
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-checkbox-input'));
|
2022-01-20 10:15:42 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['length1']);
|
2022-05-05 10:25:22 +08:00
|
|
|
fireEvent.change(container.querySelector('.ant-input'), { target: { value: '' } });
|
|
|
|
fireEvent.click(container.querySelector('.ant-checkbox-input'));
|
2022-01-20 10:15:42 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['A']);
|
|
|
|
});
|
2017-08-24 12:56:25 +08:00
|
|
|
});
|