@@ -58,6 +58,30 @@ const dropdownMenuIsClosed = () => {
58
58
}
59
59
}
60
60
61
+ function bodyIsFocused ( ) {
62
+ const isFocused = document . activeElement === document . body
63
+
64
+ isFocused . should . be . true (
65
+ `Expected Dropdown to be the active element but found ${ document . activeElement } instead.` ,
66
+ )
67
+ }
68
+
69
+ function dropdownIsFocused ( ) {
70
+ const isFocused = document . activeElement === document . querySelector ( 'div.dropdown' )
71
+
72
+ isFocused . should . be . true (
73
+ `Expected Dropdown to be the active element but found ${ document . activeElement } instead.` ,
74
+ )
75
+ }
76
+
77
+ function dropdownInputIsFocused ( ) {
78
+ const isFocused = document . activeElement === document . querySelector ( 'input.search' )
79
+
80
+ isFocused . should . be . true (
81
+ `Expected DropdownSearchInput to be the active element but found ${ document . activeElement } instead.` ,
82
+ )
83
+ }
84
+
61
85
const dropdownMenuIsOpen = ( ) => {
62
86
wrapper . childAt ( 0 ) . should . have . className ( 'active' )
63
87
wrapper . childAt ( 0 ) . should . have . className ( 'visible' )
@@ -141,7 +165,8 @@ describe('Dropdown', () => {
141
165
142
166
describe ( 'defaultSearchQuery' , ( ) => {
143
167
it ( 'changes default value of searchQuery' , ( ) => {
144
- shallow ( < Dropdown defaultSearchQuery = 'foo' /> ) . should . have . state ( 'searchQuery' , 'foo' )
168
+ wrapperMount ( < Dropdown defaultSearchQuery = 'foo' search /> )
169
+ wrapper . find ( 'input.search' ) . should . have . value ( 'foo' )
145
170
} )
146
171
} )
147
172
@@ -369,22 +394,23 @@ describe('Dropdown', () => {
369
394
const event = { stopPropagation : _ . noop }
370
395
const onChange = sandbox . spy ( )
371
396
372
- wrapperShallow (
397
+ wrapperMount (
373
398
< Dropdown defaultValue = { defaultValue } clearable onChange = { onChange } options = { options } /> ,
374
399
)
375
- wrapper . find ( 'Icon ' ) . simulate ( 'click' , event )
400
+ wrapper . find ( 'i.clear ' ) . simulate ( 'click' , event )
376
401
377
402
onChange . should . have . been . calledOnce ( )
378
403
onChange . should . have . been . calledWithMatch ( event , { value : '' } )
379
- wrapper . should . have . state ( 'selectedIndex' , 0 )
404
+ wrapper . should . have . exactly ( 1 ) . descendants ( '.selected.item' )
405
+ wrapper . find ( '.item' ) . at ( 0 ) . should . have . className ( 'selected' )
380
406
} )
381
407
382
408
it ( 'clears when value is multiple and is not empty' , ( ) => {
383
409
const defaultValue = _ . map ( options , 'value' )
384
410
const event = { stopPropagation : _ . noop }
385
411
const onChange = sandbox . spy ( )
386
412
387
- wrapperShallow (
413
+ wrapperMount (
388
414
< Dropdown
389
415
defaultValue = { defaultValue }
390
416
clearable
@@ -393,11 +419,12 @@ describe('Dropdown', () => {
393
419
options = { options }
394
420
/> ,
395
421
)
396
- wrapper . find ( 'Icon ' ) . simulate ( 'click' , event )
422
+ wrapper . find ( 'i.clear ' ) . simulate ( 'click' , event )
397
423
398
424
onChange . should . have . been . calledOnce ( )
399
425
onChange . should . have . been . calledWithMatch ( event , { value : [ ] } )
400
- wrapper . should . have . state ( 'selectedIndex' , 0 )
426
+ wrapper . should . have . exactly ( 1 ) . descendants ( '.selected.item' )
427
+ wrapper . find ( '.item' ) . at ( 0 ) . should . have . className ( 'selected' )
401
428
} )
402
429
} )
403
430
@@ -447,19 +474,11 @@ describe('Dropdown', () => {
447
474
onChange . should . have . been . calledOnce ( )
448
475
} )
449
476
450
- it ( 'sets focus state to false' , ( ) => {
451
- wrapperShallow ( < Dropdown selectOnBlur /> )
452
-
453
- wrapper . childAt ( 0 ) . simulate ( 'blur' )
454
- wrapper . should . have . state ( 'focus' , false )
455
- } )
456
-
457
477
it ( 'sets searchQuery state to empty' , ( ) => {
458
- wrapperShallow ( < Dropdown /> )
478
+ wrapperMount ( < Dropdown defaultSearchQuery = 'foo' search /> )
459
479
460
- wrapper . setState ( { searchQuery : 'foo' } )
461
480
wrapper . childAt ( 0 ) . simulate ( 'blur' )
462
- wrapper . should . have . state ( 'searchQuery' , '' )
481
+ wrapper . find ( 'input.search' ) . should . have . value ( '' )
463
482
} )
464
483
465
484
it ( 'does not call onBlur when the mouse is down' , ( ) => {
@@ -486,15 +505,6 @@ describe('Dropdown', () => {
486
505
487
506
instance . makeSelectedItemActive . should . not . have . been . called ( )
488
507
} )
489
-
490
- it ( 'does not set focus state when the mouse is down' , ( ) => {
491
- wrapperShallow ( < Dropdown /> )
492
-
493
- wrapper . setState ( { focus : 'foo' } )
494
- wrapper . simulate ( 'mousedown' )
495
- wrapper . simulate ( 'blur' )
496
- wrapper . should . have . state ( 'focus' , 'foo' )
497
- } )
498
508
} )
499
509
500
510
describe ( 'handleClose' , ( ) => {
@@ -522,7 +532,7 @@ describe('Dropdown', () => {
522
532
dropdownMenuIsOpen ( )
523
533
524
534
wrapper . find ( 'DropdownItem' ) . first ( ) . simulate ( 'click' )
525
- wrapper . should . have . state ( 'value' , options [ 0 ] . value )
535
+ wrapper . find ( '.item' ) . at ( 0 ) . should . have . className ( 'active' )
526
536
dropdownMenuIsClosed ( )
527
537
528
538
// The dropdown will be still focused after an item will be selected, we should remove
@@ -595,21 +605,22 @@ describe('Dropdown', () => {
595
605
596
606
wrapper . simulate ( 'click' )
597
607
wrapper . simulate ( 'keydown' , { key : 'ArrowDown' } )
598
- wrapper . should . have . state ( 'selectedIndex' , 1 )
608
+ wrapper . should . have . exactly ( 1 ) . descendants ( '.selected.item' )
609
+ wrapper . find ( '.item' ) . at ( 1 ) . should . have . className ( 'selected' )
599
610
600
611
wrapper . setProps ( { options : [ ] } )
601
- wrapper . should . have . not . state ( 'selectedIndex ')
612
+ wrapper . should . not . have . descendants ( '.selected.item ')
602
613
} )
603
614
604
615
it ( 'will not call setSelectedIndex if options have not changed' , ( ) => {
605
616
wrapperMount ( < Dropdown options = { options } /> )
606
617
607
618
wrapper . simulate ( 'click' )
608
619
wrapper . simulate ( 'keydown' , { key : 'ArrowDown' } )
609
- wrapper . should . have . state ( 'selectedIndex' , 1 )
620
+ wrapper . find ( '.item' ) . at ( 1 ) . should . have . className ( 'selected' )
610
621
611
622
wrapper . setProps ( { options } )
612
- wrapper . should . have . state ( 'selectedIndex' , 1 )
623
+ wrapper . find ( '.item' ) . at ( 1 ) . should . have . className ( 'selected' )
613
624
} )
614
625
} )
615
626
@@ -623,16 +634,15 @@ describe('Dropdown', () => {
623
634
// open, simulate search and select option
624
635
wrapper . simulate ( 'click' )
625
636
wrapper . simulate ( 'keydown' , { key : 'ArrowDown' } )
626
- wrapper . should . have . state ( 'selectedIndex' , 1 )
637
+ wrapper . find ( '.item' ) . at ( 1 ) . should . have . className ( 'selected' )
627
638
628
639
input . simulate ( 'change' , { target : { value : option . text } } )
629
640
wrapper . simulate ( 'keydown' , { key : 'Enter' } )
630
-
631
- wrapper . should . have . state ( 'selectedIndex' , 4 )
641
+ wrapper . find ( '.item' ) . at ( 4 ) . should . have . className ( 'selected' )
632
642
633
643
// open again
634
644
wrapper . simulate ( 'click' )
635
- wrapper . should . have . state ( 'selectedIndex' , 4 )
645
+ wrapper . find ( '.item' ) . at ( 4 ) . should . have . className ( 'selected' )
636
646
} )
637
647
638
648
it ( 'keeps "selectedIndex" when the same item was selected' , ( ) => {
@@ -644,13 +654,12 @@ describe('Dropdown', () => {
644
654
// simulate search and select option
645
655
input . simulate ( 'change' , { target : { value : option . text } } )
646
656
wrapper . simulate ( 'keydown' , { key : 'Enter' } )
647
-
648
- wrapper . should . have . state ( 'selectedIndex' , 4 )
657
+ wrapper . find ( '.item' ) . at ( 4 ) . should . have . className ( 'selected' )
649
658
650
659
// select the same option again
651
660
input . simulate ( 'change' , { target : { value : option . text } } )
652
661
wrapper . simulate ( 'keydown' , { key : 'Enter' } )
653
- wrapper . should . have . state ( 'selectedIndex' , 4 )
662
+ wrapper . find ( '.item' ) . at ( 4 ) . should . have . className ( 'selected' )
654
663
} )
655
664
} )
656
665
@@ -703,11 +712,13 @@ describe('Dropdown', () => {
703
712
704
713
describe ( 'searchQuery' , ( ) => {
705
714
it ( 'defaults to empty string' , ( ) => {
706
- shallow ( < Dropdown /> ) . should . have . state ( 'searchQuery' , '' )
715
+ wrapperMount ( < Dropdown search /> )
716
+ wrapper . find ( 'input.search' ) . should . have . value ( '' )
707
717
} )
708
718
709
719
it ( 'passes value to state' , ( ) => {
710
- shallow ( < Dropdown searchQuery = 'foo' /> ) . should . have . state ( 'searchQuery' , 'foo' )
720
+ wrapperMount ( < Dropdown search searchQuery = 'foo' /> )
721
+ wrapper . find ( 'input.search' ) . should . have . value ( 'foo' )
711
722
} )
712
723
} )
713
724
@@ -1054,7 +1065,7 @@ describe('Dropdown', () => {
1054
1065
wrapper . simulate ( 'click' )
1055
1066
wrapper . simulate ( 'keydown' , { key : 'ArrowDown' } )
1056
1067
1057
- wrapper . should . have . state ( 'searchQuery' , 'foo' )
1068
+ wrapper . find ( 'input.search' ) . should . have . value ( 'foo' )
1058
1069
} )
1059
1070
} )
1060
1071
@@ -1110,17 +1121,15 @@ describe('Dropdown', () => {
1110
1121
1111
1122
wrapper . simulate ( 'click' )
1112
1123
wrapper . simulate ( 'keydown' , { key : 'ArrowDown' } )
1113
-
1114
- wrapper . should . have . state ( 'value' , options [ 1 ] . value )
1124
+ wrapper . find ( '.item' ) . at ( 1 ) . should . have . className ( 'active' )
1115
1125
} )
1116
1126
1117
1127
it ( 'updates value on up arrow' , ( ) => {
1118
1128
wrapperMount ( < Dropdown options = { options } selection /> )
1119
1129
1120
1130
wrapper . simulate ( 'click' )
1121
1131
wrapper . simulate ( 'keydown' , { key : 'ArrowUp' } )
1122
-
1123
- wrapper . should . have . state ( 'value' , options [ 4 ] . value )
1132
+ wrapper . find ( '.item' ) . at ( 4 ) . should . have . className ( 'active' )
1124
1133
} )
1125
1134
} )
1126
1135
@@ -1359,15 +1368,15 @@ describe('Dropdown', () => {
1359
1368
1360
1369
it ( 'handles focus correctly' , ( ) => {
1361
1370
wrapperMount ( < Dropdown options = { options } selection /> )
1362
- wrapper . should . have . state ( 'focus' , false )
1371
+ bodyIsFocused ( )
1363
1372
1364
1373
// focus
1365
1374
wrapper . getDOMNode ( ) . focus ( )
1366
- wrapper . should . have . state ( 'focus' , true )
1375
+ dropdownIsFocused ( )
1367
1376
1368
1377
// click outside
1369
1378
domEvent . click ( document . body )
1370
- wrapper . should . have . state ( 'focus' , false )
1379
+ bodyIsFocused ( )
1371
1380
} )
1372
1381
1373
1382
it ( 'closes on esc key' , ( ) => {
@@ -1718,6 +1727,7 @@ describe('Dropdown', () => {
1718
1727
spy . should . have . been . calledOnce ( )
1719
1728
spy . should . have . been . calledWithMatch ( { } , { value : expected } )
1720
1729
} )
1730
+
1721
1731
it ( 'removes the last item when there is no search query when uncontrolled' , ( ) => {
1722
1732
const value = _ . map ( options , 'value' )
1723
1733
const expected = _ . dropRight ( value )
@@ -1734,14 +1744,12 @@ describe('Dropdown', () => {
1734
1744
1735
1745
// open
1736
1746
wrapper . simulate ( 'click' )
1737
-
1738
1747
domEvent . keyDown ( document , { key : 'Backspace' } )
1739
1748
1740
1749
spy . should . have . been . calledOnce ( )
1741
1750
spy . should . have . been . calledWithMatch ( { } , { value : expected } )
1742
-
1743
- wrapper . state ( 'value' ) . should . deep . equal ( expected )
1744
1751
} )
1752
+
1745
1753
it ( 'does not remove the last item when there is a search query' , ( ) => {
1746
1754
// search for random item
1747
1755
const searchQuery = _ . sample ( options ) . text
@@ -2099,7 +2107,7 @@ describe('Dropdown', () => {
2099
2107
wrapper . find ( 'DropdownItem' ) . first ( ) . simulate ( 'click' )
2100
2108
2101
2109
// bye bye search query
2102
- wrapper . should . have . state ( 'searchQuery' , '' )
2110
+ wrapper . find ( 'input.search' ) . should . have . value ( '' )
2103
2111
} )
2104
2112
2105
2113
it ( 'opens the menu on change if there is a query and not already open' , ( ) => {
@@ -2173,10 +2181,10 @@ describe('Dropdown', () => {
2173
2181
// avoid it to prevent false positives
2174
2182
wrapper . simulate ( 'click' )
2175
2183
wrapper . simulate ( 'keydown' , { key : 'ArrowUp' } )
2176
- wrapper . should . have . state ( 'selectedIndex' , 3 )
2184
+ wrapper . find ( '.item' ) . at ( 3 ) . should . have . className ( 'selected' )
2177
2185
2178
2186
wrapper . find ( 'input.search' ) . simulate ( 'change' , { target : { value : 'baz' } } )
2179
- wrapper . should . have . state ( 'selectedIndex' , 0 )
2187
+ wrapper . find ( '.item' ) . at ( 0 ) . should . have . className ( 'selected' )
2180
2188
} )
2181
2189
2182
2190
it ( 'still allows moving selection after blur/focus' , ( ) => {
@@ -2226,13 +2234,7 @@ describe('Dropdown', () => {
2226
2234
. simulate ( 'click' , nativeEvent )
2227
2235
2228
2236
dropdownMenuIsClosed ( )
2229
-
2230
- const activeElement = document . activeElement
2231
- const searchIsFocused = activeElement === document . querySelector ( 'input.search' )
2232
- searchIsFocused . should . be . true (
2233
- `Expected "input.search" to be the active element but found ${ activeElement } instead.` ,
2234
- )
2235
- wrapper . should . have . state ( 'focus' , true )
2237
+ dropdownInputIsFocused ( )
2236
2238
} )
2237
2239
2238
2240
it ( 'sets focus to the dropdown after selection' , ( ) => {
@@ -2247,14 +2249,7 @@ describe('Dropdown', () => {
2247
2249
// TODO: try reenable after Enzyme update
2248
2250
// https://github.com/Semantic-Org/Semantic-UI-React/pull/3747#issuecomment-522018329
2249
2251
// dropdownMenuIsClosed()
2250
-
2251
- const activeElement = document . activeElement
2252
- const dropdownIsFocused = activeElement === document . querySelector ( 'div.dropdown' )
2253
- dropdownIsFocused . should . be . true (
2254
- `Expected Dropdown to be the active element but found ${ activeElement } instead.` ,
2255
- )
2256
-
2257
- wrapper . should . have . state ( 'focus' , true )
2252
+ dropdownIsFocused ( )
2258
2253
} )
2259
2254
} )
2260
2255
@@ -2687,7 +2682,7 @@ describe('Dropdown', () => {
2687
2682
search . simulate ( 'change' , { target : { value : 'boo' } } )
2688
2683
search . simulate ( 'keydown' , { key : 'Enter' } )
2689
2684
2690
- wrapper . should . have . state ( 'searchQuery' , '' )
2685
+ wrapper . find ( 'input.search' ) . should . have . value ( '' )
2691
2686
} )
2692
2687
} )
2693
2688
@@ -2755,7 +2750,7 @@ describe('Dropdown', () => {
2755
2750
wrapper . simulate ( 'keydown' , { key : 'ArrowDown' } )
2756
2751
2757
2752
onChange . should . have . been . called ( )
2758
- wrapper . should . have . state ( 'value' , options [ 1 ] . value )
2753
+ wrapper . find ( '.item' ) . at ( 1 ) . should . have . className ( 'active' )
2759
2754
} )
2760
2755
2761
2756
it ( 'does not change value when set to false' , ( ) => {
@@ -2776,7 +2771,7 @@ describe('Dropdown', () => {
2776
2771
wrapper . simulate ( 'keydown' , { key : 'ArrowDown' } )
2777
2772
2778
2773
onChange . should . not . have . been . called ( )
2779
- wrapper . should . have . state ( 'value' , value )
2774
+ wrapper . find ( '.item' ) . at ( 0 ) . should . have . className ( 'active' )
2780
2775
} )
2781
2776
} )
2782
2777
0 commit comments