forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.test.js
129 lines (117 loc) · 3.84 KB
/
group.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { mount } from '@vue/test-utils';
import { asyncExpect, sleep } from '../../../tests/utils';
import Checkbox from '../index';
import mountTest from '../../../tests/shared/mountTest';
describe('CheckboxGroup', () => {
mountTest(Checkbox.Group);
it('should work basically', async () => {
const onChange = jest.fn();
const wrapper = mount(
{
render() {
return <Checkbox.Group options={['Apple', 'Pear', 'Orange']} onChange={onChange} />;
},
},
{
sync: false,
},
);
wrapper.findAll('.ant-checkbox-input')[0].trigger('change');
await sleep();
expect(onChange).toHaveBeenCalledWith(['Apple']);
wrapper.findAll('.ant-checkbox-input')[1].trigger('change');
await sleep();
expect(onChange).toHaveBeenCalledWith(['Apple', 'Pear']);
wrapper.findAll('.ant-checkbox-input')[2].trigger('change');
await sleep();
expect(onChange).toHaveBeenCalledWith(['Apple', 'Pear', 'Orange']);
wrapper.findAll('.ant-checkbox-input')[1].trigger('change');
await sleep();
expect(onChange).toHaveBeenCalledWith(['Apple', 'Orange']);
});
it('does not trigger onChange callback of both Checkbox and CheckboxGroup when CheckboxGroup is disabled', () => {
const onChangeGroup = jest.fn();
const options = [
{ label: 'Apple', value: 'Apple' },
{ label: 'Pear', value: 'Pear' },
];
const groupWrapper = mount(
{
render() {
return <Checkbox.Group options={options} onChange={onChangeGroup} disabled />;
},
},
{
sync: false,
},
);
groupWrapper.findAll('.ant-checkbox-input')[0].trigger('change');
expect(onChangeGroup).not.toBeCalled();
groupWrapper.findAll('.ant-checkbox-input')[1].trigger('change');
expect(onChangeGroup).not.toBeCalled();
});
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 },
];
const groupWrapper = mount(
{
render() {
return <Checkbox.Group options={options} onChange={onChangeGroup} />;
},
},
{
sync: false,
},
);
groupWrapper.findAll('.ant-checkbox-input')[0].trigger('change');
expect(onChangeGroup).toHaveBeenCalledWith(['Apple']);
groupWrapper.findAll('.ant-checkbox-input')[1].trigger('change');
expect(onChangeGroup).toHaveBeenCalledWith(['Apple']);
});
it('passes prefixCls down to checkbox', () => {
const options = [
{ label: 'Apple', value: 'Apple' },
{ label: 'Orange', value: 'Orange' },
];
const wrapper = mount({
render() {
return <Checkbox.Group prefixCls="my-checkbox" options={options} />;
},
});
expect(wrapper.html()).toMatchSnapshot();
});
it('should be controlled by value', async () => {
const options = [
{ label: 'Apple', value: 'Apple' },
{ label: 'Orange', value: 'Orange' },
];
const wrapper = mount(Checkbox.Group, {
props: { options },
sync: false,
});
expect(wrapper.vm.mergedValue).toEqual([]);
wrapper.setProps({ value: ['Apple'] });
await asyncExpect(() => {
expect(wrapper.vm.mergedValue).toEqual(['Apple']);
});
});
// https://github.com/ant-design/ant-design/issues/12642
it('should trigger onChange in sub Checkbox', () => {
const onChange = jest.fn();
const wrapper = mount({
render() {
return (
<Checkbox.Group>
<Checkbox value="my" onChange={onChange} />
</Checkbox.Group>
);
},
});
wrapper.findAll('.ant-checkbox-input')[0].trigger('change');
expect(onChange).toBeCalled();
expect(onChange.mock.calls[0][0].target.value).toEqual('my');
});
});