Skip to content

Commit 828b5ee

Browse files
committed
test: add maxCount test cases
1 parent baf5f26 commit 828b5ee

File tree

1 file changed

+113
-7
lines changed

1 file changed

+113
-7
lines changed

tests/Select.multiple.spec.js

Lines changed: 113 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-undef */
2-
import { render } from '@testing-library/react';
2+
import { render, fireEvent, within } from '@testing-library/react';
33
import { mount } from 'enzyme';
44
import KeyCode from 'rc-util/lib/KeyCode';
55
import React from 'react';
@@ -32,7 +32,10 @@ describe('TreeSelect.multiple', () => {
3232

3333
it('remove by backspace key', () => {
3434
const wrapper = mount(createSelect({ defaultValue: ['0', '1'] }));
35-
wrapper.find('input').first().simulate('keyDown', { which: KeyCode.BACKSPACE, key: 'Backspace' });
35+
wrapper
36+
.find('input')
37+
.first()
38+
.simulate('keyDown', { which: KeyCode.BACKSPACE, key: 'Backspace' });
3639
expect(wrapper.getSelection()).toHaveLength(1);
3740
expect(wrapper.getSelection(0).text()).toBe('label0');
3841
});
@@ -59,9 +62,15 @@ describe('TreeSelect.multiple', () => {
5962
}
6063
}
6164
const wrapper = mount(<App />);
62-
wrapper.find('input').first().simulate('keyDown', { which: KeyCode.BACKSPACE, key: 'Backspace' });
65+
wrapper
66+
.find('input')
67+
.first()
68+
.simulate('keyDown', { which: KeyCode.BACKSPACE, key: 'Backspace' });
6369
wrapper.selectNode(1);
64-
wrapper.find('input').first().simulate('keyDown', { which: KeyCode.BACKSPACE, key: 'Backspace' });
70+
wrapper
71+
.find('input')
72+
.first()
73+
.simulate('keyDown', { which: KeyCode.BACKSPACE, key: 'Backspace' });
6574
expect(wrapper.getSelection()).toHaveLength(1);
6675
expect(wrapper.getSelection(0).text()).toBe('label0');
6776
});
@@ -337,9 +346,7 @@ describe('TreeSelect.multiple', () => {
337346
/>,
338347
);
339348

340-
const values = Array.from(
341-
container.querySelectorAll('.rc-tree-select-selection-item-content'),
342-
); //.map(ele => ele.textContent);
349+
const values = Array.from(container.querySelectorAll('.rc-tree-select-selection-item-content')); //.map(ele => ele.textContent);
343350

344351
expect(values).toHaveLength(0);
345352

@@ -348,4 +355,103 @@ describe('TreeSelect.multiple', () => {
348355
expect(placeholder.textContent).toBe('Fake placeholder');
349356
});
350357

358+
describe('TreeSelect.maxCount', () => {
359+
const treeData = [
360+
{ key: '0', value: '0', title: '0 label' },
361+
{ key: '1', value: '1', title: '1 label' },
362+
{ key: '2', value: '2', title: '2 label' },
363+
{ key: '3', value: '3', title: '3 label' },
364+
];
365+
366+
const renderTreeSelect = props => {
367+
return render(<TreeSelect multiple maxCount={2} treeData={treeData} open {...props} />);
368+
};
369+
370+
const selectOptions = (container, optionTexts) => {
371+
const dropdownList = container.querySelector('.rc-tree-select-dropdown');
372+
optionTexts.forEach(text => {
373+
fireEvent.click(within(dropdownList).getByText(text));
374+
});
375+
};
376+
377+
it('should disable unselected options when selection reaches maxCount', () => {
378+
const { container } = renderTreeSelect();
379+
380+
selectOptions(container, ['0 label', '1 label']);
381+
382+
// Check if third and fourth options are disabled
383+
const dropdownList = container.querySelector('.rc-tree-select-dropdown');
384+
const option3 = within(dropdownList).getByText('2 label');
385+
const option4 = within(dropdownList).getByText('3 label');
386+
387+
expect(option3.closest('div')).toHaveClass('rc-tree-select-tree-treenode-disabled');
388+
expect(option4.closest('div')).toHaveClass('rc-tree-select-tree-treenode-disabled');
389+
});
390+
391+
it('should allow deselecting options after reaching maxCount', () => {
392+
const { container } = renderTreeSelect();
393+
const dropdownList = container.querySelector('.rc-tree-select-dropdown');
394+
395+
selectOptions(container, ['0 label', '1 label']);
396+
397+
// Try selecting third option, should be disabled
398+
const option3 = within(dropdownList).getByText('2 label');
399+
fireEvent.click(option3);
400+
expect(option3.closest('div')).toHaveClass('rc-tree-select-tree-treenode-disabled');
401+
402+
// Deselect first option
403+
fireEvent.click(within(dropdownList).getByText('0 label'));
404+
expect(within(dropdownList).queryByText('0 label')).toBeInTheDocument();
405+
406+
// Now should be able to select third option
407+
fireEvent.click(option3);
408+
expect(option3.closest('div')).not.toHaveClass('rc-tree-select-tree-treenode-disabled');
409+
});
410+
411+
it('should not trigger onChange when trying to select beyond maxCount', () => {
412+
const handleChange = jest.fn();
413+
const { container } = renderTreeSelect({ onChange: handleChange });
414+
415+
selectOptions(container, ['0 label', '1 label']);
416+
expect(handleChange).toHaveBeenCalledTimes(2);
417+
418+
// Try selecting third option
419+
const dropdownList = container.querySelector('.rc-tree-select-dropdown');
420+
fireEvent.click(within(dropdownList).getByText('2 label'));
421+
expect(handleChange).toHaveBeenCalledTimes(2); // Should not increase
422+
});
423+
424+
it('should not affect deselection operations when maxCount is reached', () => {
425+
const handleChange = jest.fn();
426+
const { container } = renderTreeSelect({ onChange: handleChange });
427+
428+
selectOptions(container, ['0 label', '1 label']);
429+
expect(handleChange).toHaveBeenCalledTimes(2);
430+
431+
// Deselect first option
432+
const dropdownList = container.querySelector('.rc-tree-select-dropdown');
433+
fireEvent.click(within(dropdownList).getByText('0 label'));
434+
expect(handleChange).toHaveBeenCalledTimes(3);
435+
436+
// Should be able to select third option
437+
fireEvent.click(within(dropdownList).getByText('2 label'));
438+
expect(handleChange).toHaveBeenCalledTimes(4);
439+
});
440+
441+
it('should not allow any selection when maxCount is 0', () => {
442+
const handleChange = jest.fn();
443+
const { container } = renderTreeSelect({ maxCount: 0, onChange: handleChange });
444+
445+
selectOptions(container, ['0 label', '1 label']);
446+
expect(handleChange).not.toHaveBeenCalled();
447+
});
448+
449+
it('should not limit selection when maxCount is greater than number of options', () => {
450+
const handleChange = jest.fn();
451+
const { container } = renderTreeSelect({ maxCount: 5, onChange: handleChange });
452+
453+
selectOptions(container, ['0 label', '1 label', '2 label', '3 label']);
454+
expect(handleChange).toHaveBeenCalledTimes(4);
455+
});
456+
});
351457
});

0 commit comments

Comments
 (0)