1
1
/* eslint-disable no-undef */
2
- import { render } from '@testing-library/react' ;
2
+ import { render , fireEvent , within } from '@testing-library/react' ;
3
3
import { mount } from 'enzyme' ;
4
4
import KeyCode from 'rc-util/lib/KeyCode' ;
5
5
import React from 'react' ;
@@ -32,7 +32,10 @@ describe('TreeSelect.multiple', () => {
32
32
33
33
it ( 'remove by backspace key' , ( ) => {
34
34
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' } ) ;
36
39
expect ( wrapper . getSelection ( ) ) . toHaveLength ( 1 ) ;
37
40
expect ( wrapper . getSelection ( 0 ) . text ( ) ) . toBe ( 'label0' ) ;
38
41
} ) ;
@@ -59,9 +62,15 @@ describe('TreeSelect.multiple', () => {
59
62
}
60
63
}
61
64
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' } ) ;
63
69
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' } ) ;
65
74
expect ( wrapper . getSelection ( ) ) . toHaveLength ( 1 ) ;
66
75
expect ( wrapper . getSelection ( 0 ) . text ( ) ) . toBe ( 'label0' ) ;
67
76
} ) ;
@@ -337,9 +346,7 @@ describe('TreeSelect.multiple', () => {
337
346
/> ,
338
347
) ;
339
348
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);
343
350
344
351
expect ( values ) . toHaveLength ( 0 ) ;
345
352
@@ -348,4 +355,103 @@ describe('TreeSelect.multiple', () => {
348
355
expect ( placeholder . textContent ) . toBe ( 'Fake placeholder' ) ;
349
356
} ) ;
350
357
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
+ } ) ;
351
457
} ) ;
0 commit comments