@@ -207,3 +207,170 @@ describe('TreeSelect.maxCount keyboard operations', () => {
207
207
expect ( container . querySelectorAll ( '.rc-tree-select-tree-treenode-selected' ) ) . toHaveLength ( 2 ) ;
208
208
} ) ;
209
209
} ) ;
210
+
211
+ describe ( 'TreeSelect.maxCount with different strategies' , ( ) => {
212
+ const treeData = [
213
+ {
214
+ key : '0' ,
215
+ value : '0' ,
216
+ title : 'parent' ,
217
+ children : [
218
+ { key : '0-0' , value : '0-0' , title : 'child 1' } ,
219
+ { key : '0-1' , value : '0-1' , title : 'child 2' } ,
220
+ { key : '0-2' , value : '0-2' , title : 'child 3' } ,
221
+ ] ,
222
+ } ,
223
+ ] ;
224
+
225
+ it ( 'should respect maxCount with SHOW_PARENT strategy' , ( ) => {
226
+ const handleChange = jest . fn ( ) ;
227
+ const { container } = render (
228
+ < TreeSelect
229
+ treeData = { treeData }
230
+ treeCheckable
231
+ treeDefaultExpandAll
232
+ multiple
233
+ maxCount = { 1 }
234
+ showCheckedStrategy = { TreeSelect . SHOW_PARENT }
235
+ onChange = { handleChange }
236
+ open
237
+ /> ,
238
+ ) ;
239
+
240
+ // Select parent node - should work as it only shows as one option
241
+ const parentCheckbox = within ( container ) . getByText ( 'parent' ) ;
242
+ fireEvent . click ( parentCheckbox ) ;
243
+ expect ( handleChange ) . toHaveBeenCalledTimes ( 1 ) ;
244
+ } ) ;
245
+
246
+ it ( 'should respect maxCount with SHOW_CHILD strategy' , ( ) => {
247
+ const handleChange = jest . fn ( ) ;
248
+ const { container } = render (
249
+ < TreeSelect
250
+ treeData = { treeData }
251
+ treeCheckable
252
+ treeDefaultExpandAll
253
+ multiple
254
+ maxCount = { 2 }
255
+ showCheckedStrategy = { TreeSelect . SHOW_CHILD }
256
+ onChange = { handleChange }
257
+ open
258
+ /> ,
259
+ ) ;
260
+
261
+ // Select parent node - should not work as it would show three children
262
+ const parentCheckbox = within ( container ) . getByText ( 'parent' ) ;
263
+ fireEvent . click ( parentCheckbox ) ;
264
+ expect ( handleChange ) . not . toHaveBeenCalled ( ) ;
265
+
266
+ // Select individual children - should work until maxCount
267
+ const childCheckboxes = within ( container ) . getAllByText ( / c h i l d / ) ;
268
+ fireEvent . click ( childCheckboxes [ 0 ] ) ; // first child
269
+ fireEvent . click ( childCheckboxes [ 1 ] ) ; // second child
270
+ expect ( handleChange ) . toHaveBeenCalledTimes ( 2 ) ;
271
+
272
+ // Try to select third child - should not work
273
+ fireEvent . click ( childCheckboxes [ 2 ] ) ;
274
+ expect ( handleChange ) . toHaveBeenCalledTimes ( 2 ) ;
275
+ } ) ;
276
+
277
+ it ( 'should respect maxCount with SHOW_ALL strategy' , ( ) => {
278
+ const handleChange = jest . fn ( ) ;
279
+ const { container } = render (
280
+ < TreeSelect
281
+ treeData = { treeData }
282
+ treeCheckable
283
+ treeDefaultExpandAll
284
+ multiple
285
+ maxCount = { 2 }
286
+ showCheckedStrategy = { TreeSelect . SHOW_ALL }
287
+ onChange = { handleChange }
288
+ open
289
+ /> ,
290
+ ) ;
291
+
292
+ // Select parent node - should not work as it would show both parent and children
293
+ const parentCheckbox = within ( container ) . getByText ( 'parent' ) ;
294
+ fireEvent . click ( parentCheckbox ) ;
295
+ expect ( handleChange ) . not . toHaveBeenCalled ( ) ;
296
+
297
+ // Select individual children
298
+ const childCheckboxes = within ( container ) . getAllByText ( / c h i l d / ) ;
299
+ fireEvent . click ( childCheckboxes [ 0 ] ) ;
300
+ fireEvent . click ( childCheckboxes [ 1 ] ) ;
301
+ expect ( handleChange ) . toHaveBeenCalledTimes ( 2 ) ;
302
+ } ) ;
303
+ } ) ;
304
+
305
+ describe ( 'TreeSelect.maxCount with treeCheckStrictly' , ( ) => {
306
+ const treeData = [
307
+ {
308
+ key : '0' ,
309
+ value : '0' ,
310
+ title : 'parent' ,
311
+ children : [
312
+ { key : '0-0' , value : '0-0' , title : 'child 1' } ,
313
+ { key : '0-1' , value : '0-1' , title : 'child 2' } ,
314
+ ] ,
315
+ } ,
316
+ ] ;
317
+
318
+ it ( 'should count parent and children separately when treeCheckStrictly is true' , ( ) => {
319
+ const handleChange = jest . fn ( ) ;
320
+ const { container } = render (
321
+ < TreeSelect
322
+ treeData = { treeData }
323
+ treeCheckable
324
+ treeCheckStrictly
325
+ treeDefaultExpandAll
326
+ multiple
327
+ maxCount = { 2 }
328
+ onChange = { handleChange }
329
+ open
330
+ /> ,
331
+ ) ;
332
+
333
+ // Select parent and one child - should work as they are counted separately
334
+ const parentCheckbox = within ( container ) . getByText ( 'parent' ) ;
335
+ const checkboxes = within ( container ) . getAllByText ( / c h i l d / ) ;
336
+ fireEvent . click ( parentCheckbox ) ;
337
+ fireEvent . click ( checkboxes [ 0 ] ) ; // first child
338
+ expect ( handleChange ) . toHaveBeenCalledTimes ( 2 ) ;
339
+
340
+ // Try to select second child - should not work as maxCount is reached
341
+ fireEvent . click ( checkboxes [ 1 ] ) ;
342
+ expect ( handleChange ) . toHaveBeenCalledTimes ( 2 ) ;
343
+ } ) ;
344
+
345
+ it ( 'should allow deselecting when maxCount is reached' , ( ) => {
346
+ const handleChange = jest . fn ( ) ;
347
+ const { container } = render (
348
+ < TreeSelect
349
+ treeData = { treeData }
350
+ treeCheckable
351
+ treeCheckStrictly
352
+ treeDefaultExpandAll
353
+ multiple
354
+ maxCount = { 2 }
355
+ onChange = { handleChange }
356
+ open
357
+ /> ,
358
+ ) ;
359
+
360
+ const parentCheckbox = within ( container ) . getByText ( 'parent' ) ;
361
+ const checkboxes = within ( container ) . getAllByText ( / c h i l d / ) ;
362
+
363
+ // Select parent and first child
364
+ fireEvent . click ( parentCheckbox ) ;
365
+ fireEvent . click ( checkboxes [ 0 ] ) ;
366
+ expect ( handleChange ) . toHaveBeenCalledTimes ( 2 ) ;
367
+
368
+ // Deselect parent
369
+ fireEvent . click ( parentCheckbox ) ;
370
+ expect ( handleChange ) . toHaveBeenCalledTimes ( 3 ) ;
371
+
372
+ // Now should be able to select second child
373
+ fireEvent . click ( checkboxes [ 1 ] ) ;
374
+ expect ( handleChange ) . toHaveBeenCalledTimes ( 4 ) ;
375
+ } ) ;
376
+ } ) ;
0 commit comments