File size: 10,083 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import React, { useState } from 'react';

import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
import { fireEvent, render } from '../../../tests/utils';
import Collapse from '../../collapse';
import Input from '../../input';
import Table from '../../table';
import type { CheckboxGroupProps } from '../index';
import Checkbox from '../index';

describe('CheckboxGroup', () => {
  mountTest(Checkbox.Group);
  rtlTest(Checkbox.Group);

  it('should work basically', () => {
    const onChange = jest.fn();
    const { container } = render(
      <Checkbox.Group options={['Apple', 'Pear', 'Orange']} onChange={onChange} />,
    );
    fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[0]);
    expect(onChange).toHaveBeenCalledWith(['Apple']);
    fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[1]);
    expect(onChange).toHaveBeenCalledWith(['Apple', 'Pear']);
    fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[2]);
    expect(onChange).toHaveBeenCalledWith(['Apple', 'Pear', 'Orange']);
    fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[1]);
    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 { container } = render(
      <Checkbox.Group options={options} onChange={onChangeGroup} disabled />,
    );
    fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[0]);
    expect(onChangeGroup).not.toHaveBeenCalled();
    fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[1]);
    expect(onChangeGroup).not.toHaveBeenCalled();
  });

  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 { container } = render(<Checkbox.Group options={options} onChange={onChangeGroup} />);
    fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[0]);
    expect(onChangeGroup).toHaveBeenCalledWith(['Apple']);
    fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[1]);
    expect(onChangeGroup).toHaveBeenCalledWith(['Apple']);
  });

  it('all children should have a name property', () => {
    const { container } = render(<Checkbox.Group name="checkboxgroup" options={['Yes', 'No']} />);
    Array.from(container.querySelectorAll<HTMLInputElement>('input[type="checkbox"]')).forEach(
      (el) => {
        expect(el.getAttribute('name')).toEqual('checkboxgroup');
      },
    );
  });

  it('passes prefixCls down to checkbox', () => {
    const options = [
      { label: 'Apple', value: 'Apple' },
      { label: 'Orange', value: 'Orange', style: { fontSize: 12 } },
    ];

    const { container } = render(<Checkbox.Group prefixCls="my-checkbox" options={options} />);

    expect(container.firstChild).toMatchSnapshot();
  });

  it('should be controlled by value', () => {
    const options = [
      { label: 'Apple', value: 'Apple' },
      { label: 'Orange', value: 'Orange' },
    ];
    const renderCheckbox = (props: CheckboxGroupProps) => <Checkbox.Group {...props} />;
    const { container, rerender } = render(renderCheckbox({ options }));
    expect(container.querySelectorAll('.ant-checkbox-checked').length).toBe(0);
    rerender(renderCheckbox({ options, value: 'Apple' as any }));
    expect(container.querySelectorAll('.ant-checkbox-checked').length).toBe(1);
  });

  // https://github.com/ant-design/ant-design/issues/12642
  it('should trigger onChange in sub Checkbox', () => {
    const onChange = jest.fn();
    const { container } = render(
      <Checkbox.Group>
        <Checkbox value="my" onChange={onChange} />
      </Checkbox.Group>,
    );
    fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[0]);
    expect(onChange).toHaveBeenCalled();
    expect(onChange.mock.calls[0][0].target.value).toEqual('my');
  });

  // https://github.com/ant-design/ant-design/issues/16376
  it('onChange should filter removed value', () => {
    const onChange = jest.fn();
    const { container, rerender } = render(
      <Checkbox.Group defaultValue={[1]} onChange={onChange}>
        <Checkbox key={1} value={1} />
        <Checkbox key={2} value={2} />
      </Checkbox.Group>,
    );

    rerender(
      <Checkbox.Group defaultValue={[1]} onChange={onChange}>
        <Checkbox key={2} value={2} />
      </Checkbox.Group>,
    );
    fireEvent.click(container.querySelector('.ant-checkbox-input')!);

    expect(onChange).toHaveBeenCalledWith([2]);
  });

  it('checkbox should register value again after value changed', () => {
    const onChange = jest.fn();
    const { container, rerender } = render(
      <Checkbox.Group defaultValue={[1]} onChange={onChange}>
        <Checkbox key={1} value={1} />
      </Checkbox.Group>,
    );

    rerender(
      <Checkbox.Group defaultValue={[1]} onChange={onChange}>
        <Checkbox key={1} value={2} />
      </Checkbox.Group>,
    );

    expect(container.querySelector('.ant-checkbox-input')).toHaveAttribute('checked');
  });

  // https://github.com/ant-design/ant-design/issues/17297
  it('onChange should keep the order of the original values', () => {
    const onChange = jest.fn();
    const { container } = render(
      <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>,
    );
    fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[0]);
    expect(onChange).toHaveBeenCalledWith([1]);
    fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[1]);
    expect(onChange).toHaveBeenCalledWith([1, 2]);
    fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[0]);
    expect(onChange).toHaveBeenCalledWith([2]);
    fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[0]);
    expect(onChange).toHaveBeenCalledWith([1, 2]);
  });

  // https://github.com/ant-design/ant-design/issues/21134
  it('should work when checkbox is wrapped by other components', () => {
    const { container } = render(
      <Checkbox.Group>
        <Collapse
          items={[
            {
              key: 'test panel',
              label: 'test panel',
              children: (
                <div>
                  <Checkbox value="1">item</Checkbox>
                </div>
              ),
            },
          ]}
          bordered={false}
        />
      </Checkbox.Group>,
    );

    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);
  });

  it('skipGroup', () => {
    const onChange = jest.fn();
    const { container } = render(
      <Checkbox.Group onChange={onChange}>
        <Checkbox value={1} />
        <Checkbox value={2} skipGroup />
      </Checkbox.Group>,
    );
    fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[1]);
    expect(onChange).not.toHaveBeenCalled();
  });

  it('Table rowSelection', () => {
    const onChange = jest.fn();
    const { container } = render(
      <Checkbox.Group onChange={onChange}>
        <Table
          dataSource={[{ key: 1, value: '1' }]}
          columns={[{ title: 'title', dataIndex: 'value' }]}
          rowSelection={{}}
        />
      </Checkbox.Group>,
    );
    fireEvent.click(container.querySelectorAll('.ant-checkbox-input')[1]);
    expect(onChange).not.toHaveBeenCalled();
  });

  it('should get div ref', () => {
    const ref = React.createRef<HTMLDivElement>();
    render(<Checkbox.Group options={['Apple', 'Pear', 'Orange']} ref={ref} />);
    expect(ref.current?.nodeName).toBe('DIV');
  });

  it('should support number option', () => {
    const onChange = jest.fn();

    const { container } = render(
      <Checkbox.Group options={[1, 'Pear', 'Orange']} onChange={onChange} />,
    );

    fireEvent.click(container.querySelector('.ant-checkbox-input')!);
    expect(onChange).toHaveBeenCalledWith([1]);
  });

  it('should store latest checkbox value if changed', () => {
    const onChange = jest.fn();

    const Demo: React.FC = () => {
      const [v, setV] = useState('');

      React.useEffect(() => {
        setV('1');
      }, []);

      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>
      );
    };

    const { container } = render(<Demo />);
    fireEvent.click(container.querySelector('.ant-checkbox-input')!);
    expect(onChange).toHaveBeenCalledWith([]);
    fireEvent.click(container.querySelector('.ant-checkbox-input')!);
    expect(onChange).toHaveBeenCalledWith(['length1']);
    fireEvent.change(container.querySelector('.ant-input')!, { target: { value: '' } });
    fireEvent.click(container.querySelector('.ant-checkbox-input')!);
    expect(onChange).toHaveBeenCalledWith(['A']);
  });

  it('options support id', () => {
    const { container } = render(
      <Checkbox.Group options={[{ label: 'bamboo', id: 'bamboo', value: 'bamboo' }]} />,
    );
    expect(container.querySelector('#bamboo')).toBeTruthy();
  });
});