/
Dropdown-test.js
2902 lines (2321 loc) · 89.2 KB
/
Dropdown-test.js
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import _ from 'lodash'
import faker from 'faker'
import React from 'react'
import * as common from 'test/specs/commonTests'
import { consoleUtil, domEvent, sandbox } from 'test/utils'
import Dropdown from 'src/modules/Dropdown/Dropdown'
import DropdownDivider from 'src/modules/Dropdown/DropdownDivider'
import DropdownHeader from 'src/modules/Dropdown/DropdownHeader'
import DropdownItem from 'src/modules/Dropdown/DropdownItem'
import DropdownMenu from 'src/modules/Dropdown/DropdownMenu'
import DropdownSearchInput from 'src/modules/Dropdown/DropdownSearchInput'
let attachTo
let options
let wrapper
// ----------------------------------------
// Wrapper
// ----------------------------------------
// we need to unmount the dropdown after every test to ensure all event listeners are cleaned up
// wrap the render methods to update a global wrapper that is unmounted after each test
const wrapperMount = (node, opts) => {
attachTo = document.createElement('div')
document.body.appendChild(attachTo)
wrapper = mount(node, { ...opts, attachTo })
return wrapper
}
const wrapperShallow = (...args) => (wrapper = shallow(...args))
const wrapperRender = (...args) => (wrapper = render(...args))
// ----------------------------------------
// Options
// ----------------------------------------
const getOptions = (count = 5) =>
_.times(count, (i) => {
const text = [i, ..._.times(3, faker.hacker.noun)].join(' ')
const value = _.snakeCase(text)
return { text, value }
})
// -------------------------------
// Common Assertions
// -------------------------------
const dropdownMenuIsClosed = () => {
wrapper.should.not.have.className('visible')
const menu = wrapper.find('DropdownMenu')
try {
// when shallow rendered
menu.should.not.have.prop('open', true)
} catch (err) {
// when mounted
menu.should.not.have.className('visible')
}
}
const dropdownMenuIsOpen = () => {
wrapper.childAt(0).should.have.className('active')
wrapper.childAt(0).should.have.className('visible')
const menu = wrapper.find('DropdownMenu')
try {
// when shallow rendered
menu.should.have.prop('open', true)
} catch (err) {
// when mounted
menu.should.have.className('visible')
}
}
const nativeEvent = { nativeEvent: { stopImmediatePropagation: _.noop } }
describe('Dropdown', () => {
beforeEach(() => {
attachTo = undefined
wrapper = undefined
options = getOptions()
})
afterEach(() => {
if (wrapper && wrapper.unmount) wrapper.unmount()
if (attachTo) document.body.removeChild(attachTo)
})
common.isConformant(Dropdown)
common.hasUIClassName(Dropdown)
common.hasSubcomponents(Dropdown, [
DropdownDivider,
DropdownHeader,
DropdownItem,
DropdownMenu,
DropdownSearchInput,
])
common.implementsIconProp(Dropdown, {
assertExactMatch: false,
autoGenerateKey: false,
})
common.implementsShorthandProp(Dropdown, {
autoGenerateKey: false,
propKey: 'header',
ShorthandComponent: DropdownHeader,
mapValueToProps: (val) => ({ content: val }),
})
common.propKeyOnlyToClassName(Dropdown, 'disabled')
common.propKeyOnlyToClassName(Dropdown, 'error')
common.propKeyOnlyToClassName(Dropdown, 'loading')
common.propKeyOnlyToClassName(Dropdown, 'basic')
common.propKeyOnlyToClassName(Dropdown, 'button')
common.propKeyOnlyToClassName(Dropdown, 'compact')
common.propKeyOnlyToClassName(Dropdown, 'fluid')
common.propKeyOnlyToClassName(Dropdown, 'floating')
common.propKeyOnlyToClassName(Dropdown, 'inline')
// TODO: See Dropdown cx notes
// common.propKeyOnlyToClassName(Dropdown, 'icon')
common.propKeyOnlyToClassName(Dropdown, 'labeled')
common.propKeyOnlyToClassName(Dropdown, 'item')
common.propKeyOnlyToClassName(Dropdown, 'multiple')
common.propKeyOnlyToClassName(Dropdown, 'search')
common.propKeyOnlyToClassName(Dropdown, 'selection')
common.propKeyOnlyToClassName(Dropdown, 'simple')
common.propKeyOnlyToClassName(Dropdown, 'scrolling')
common.propKeyOnlyToClassName(Dropdown, 'upward')
common.propKeyOrValueAndKeyToClassName(Dropdown, 'pointing', [
'left',
'right',
'top',
'top left',
'top right',
'bottom',
'bottom left',
'bottom right',
])
describe('defaultSearchQuery', () => {
it('changes default value of searchQuery', () => {
shallow(<Dropdown defaultSearchQuery='foo' />).should.have.state('searchQuery', 'foo')
})
})
it('closes on blur', () => {
wrapperMount(<Dropdown options={options} />).simulate('click')
dropdownMenuIsOpen()
wrapper.simulate('blur')
dropdownMenuIsClosed()
})
it('does not close on blur with closeOnBlur set to false', () => {
wrapperMount(<Dropdown options={options} closeOnBlur={false} />).simulate('click')
dropdownMenuIsOpen()
wrapper.simulate('blur')
dropdownMenuIsOpen()
})
it('blurs the Dropdown node on close', () => {
wrapperMount(<Dropdown options={options} selection defaultOpen />)
const instance = wrapper.instance()
sandbox.spy(instance.ref.current, 'blur')
dropdownMenuIsOpen()
wrapper.simulate('click')
dropdownMenuIsClosed()
instance.ref.current.blur.should.have.been.calledOnce()
})
it('blurs the Dropdown node on close by clicking outside component', () => {
wrapperMount(<Dropdown options={options} selection defaultOpen />)
const instance = wrapper.instance()
sandbox.spy(instance.ref.current, 'blur')
dropdownMenuIsOpen()
document.body.click()
dropdownMenuIsClosed()
instance.ref.current.blur.should.have.been.calledOnce()
})
it('does not close on click when search is true and options are empty', () => {
wrapperMount(<Dropdown options={[]} search selection defaultOpen />)
const instance = wrapper.instance()
sandbox.spy(instance.ref.current, 'blur')
dropdownMenuIsOpen()
wrapper.simulate('click')
dropdownMenuIsOpen()
})
it('opens on focus', () => {
wrapperMount(<Dropdown options={options} />)
dropdownMenuIsClosed()
wrapper.simulate('focus')
dropdownMenuIsOpen()
})
describe('disabled', () => {
it('does not open on click', () => {
wrapperMount(<Dropdown options={options} disabled />)
dropdownMenuIsClosed()
wrapper.simulate('click')
dropdownMenuIsClosed()
})
it('does not open on click with pointer events enabled', () => {
wrapperMount(<Dropdown options={options} disabled style={{ pointerEvents: 'all' }} />)
dropdownMenuIsClosed()
wrapper.simulate('click')
dropdownMenuIsClosed()
})
it('does not open on focus', () => {
wrapperMount(<Dropdown options={options} disabled />)
dropdownMenuIsClosed()
wrapper.simulate('focus')
dropdownMenuIsClosed()
})
})
describe('tabIndex', () => {
it('defaults to 0', () => {
wrapperShallow(<Dropdown options={options} />)
wrapper.childAt(0).should.have.prop('tabIndex', 0)
})
it('defaults to -1 when disabled', () => {
wrapperShallow(<Dropdown disabled options={options} />)
wrapper.childAt(0).should.have.prop('tabIndex', -1)
})
it('applies when defined', () => {
wrapperShallow(<Dropdown options={options} tabIndex={1} />)
wrapper.childAt(0).should.have.prop('tabIndex', 1)
})
describe('tabIndex', () => {
it('defaults the search input to 0', () => {
shallow(<Dropdown options={options} selection search />)
.find(DropdownSearchInput)
.should.have.prop('tabIndex', 0)
})
it('defaults the disabled search input to -1', () => {
shallow(<Dropdown disabled options={options} selection search />)
.find(DropdownSearchInput)
.should.have.prop('tabIndex', -1)
})
it('allows explicitly setting the search input value', () => {
shallow(<Dropdown options={options} selection search tabIndex={123} />)
.find(DropdownSearchInput)
.should.have.prop('tabIndex', 123)
})
it('allows explicitly setting the search input value when disabled', () => {
shallow(<Dropdown disabled options={options} selection search tabIndex={123} />)
.find(DropdownSearchInput)
.should.have.prop('tabIndex', 123)
})
it('is not present on the root when is search', () => {
shallow(<Dropdown options={options} selection search />).should.not.have.prop('tabIndex')
})
it('is not present on the root when is search and defined', () => {
shallow(<Dropdown options={options} selection search tabIndex={1} />).should.not.have.prop(
'tabIndex',
)
})
})
})
describe('aria', () => {
it('should label normal dropdown as a listbox', () => {
wrapperMount(<Dropdown />)
wrapper
.find('div')
.at(0)
.should.have.prop('role', 'listbox')
})
it('should render an aria-live region with aria-atomic', () => {
wrapperMount(<Dropdown />)
wrapper
.find('div')
.at(1)
.should.have.props({ 'aria-live': 'polite', 'aria-atomic': true, role: 'alert' })
})
it('should label search dropdown as a combobox', () => {
wrapperMount(<Dropdown search />)
wrapper
.find('div')
.at(0)
.should.have.prop('role', 'combobox')
})
it('should label search dropdownMenu as a listbox', () => {
wrapperMount(<Dropdown search />)
wrapper.find('DropdownMenu').should.have.prop('role', 'listbox')
})
it('should label search multiple dropdownMenu as aria-multiselectable', () => {
wrapperMount(<Dropdown search multiple />)
wrapper.find('DropdownMenu').should.have.prop('aria-multiselectable', true)
})
it('should not label normal dropdownMenu with a role', () => {
wrapperMount(<Dropdown />)
wrapper.find('DropdownMenu').should.not.have.prop('role')
})
it('should label disabled dropdown as aria-disabled', () => {
wrapperMount(<Dropdown disabled />)
wrapper
.find('div')
.at(0)
.should.have.prop('aria-disabled', true)
})
it('should label normal dropdown without aria-disabled', () => {
wrapperMount(<Dropdown />)
wrapper
.find('div')
.at(0)
.should.not.have.prop('aria-disabled')
})
it('should label multiple dropdown as aria-multiselectable', () => {
wrapperMount(<Dropdown multiple />)
wrapper
.find('div')
.at(0)
.should.have.prop('aria-multiselectable', true)
})
it('should not label multiple search dropdown as aria-multiselectable', () => {
wrapperMount(<Dropdown search multiple />)
wrapper
.find('div')
.at(0)
.should.not.have.prop('aria-multiselectable')
})
it('should label normal dropdown without aria-multiselectable', () => {
wrapperMount(<Dropdown />)
wrapper
.find('div')
.at(0)
.should.not.have.prop('aria-multiselectable')
})
it('should label loading dropdown as aria-busy', () => {
wrapperMount(<Dropdown loading />)
wrapper
.find('div')
.at(0)
.should.have.prop('aria-busy', true)
})
it('should label normal dropdown without aria-busy', () => {
wrapperMount(<Dropdown />)
wrapper
.find('div')
.at(0)
.should.not.have.prop('aria-busy')
})
it('should label search dropdown input aria-autocomplete=list', () => {
wrapperMount(<Dropdown search />)
wrapper.find('input').should.have.prop('aria-autocomplete', 'list')
})
it('should label search dropdown input type=text', () => {
wrapperMount(<Dropdown search />)
wrapper.find('input').should.have.prop('type', 'text')
})
})
describe('clearable', () => {
it('does not clear when value is empty', () => {
const onChange = sandbox.spy()
wrapperShallow(<Dropdown clearable onChange={onChange} />)
wrapper.find('Icon').simulate('click', { stopPropagation: _.noop })
onChange.should.have.not.been.called()
})
it('does not clear when is multiple and value is empty', () => {
const onChange = sandbox.spy()
wrapperShallow(<Dropdown clearable multiple onChange={onChange} />)
wrapper.find('Icon').simulate('click', { stopPropagation: _.noop })
onChange.should.have.not.been.called()
})
it('clears when value is not empty', () => {
const defaultValue = options[1].value
const event = { stopPropagation: _.noop }
const onChange = sandbox.spy()
wrapperShallow(
<Dropdown defaultValue={defaultValue} clearable onChange={onChange} options={options} />,
)
wrapper.find('Icon').simulate('click', event)
onChange.should.have.been.calledOnce()
onChange.should.have.been.calledWithMatch(event, { value: '' })
wrapper.should.have.state('selectedIndex', 0)
})
it('clears when value is multiple and is not empty', () => {
const defaultValue = _.map(options, 'value')
const event = { stopPropagation: _.noop }
const onChange = sandbox.spy()
wrapperShallow(
<Dropdown
defaultValue={defaultValue}
clearable
multiple
onChange={onChange}
options={options}
/>,
)
wrapper.find('Icon').simulate('click', event)
onChange.should.have.been.calledOnce()
onChange.should.have.been.calledWithMatch(event, { value: [] })
wrapper.should.have.state('selectedIndex', 0)
})
})
describe('handleBlur', () => {
it('passes the event to the onBlur prop', () => {
const onBlur = sandbox.spy()
const event = { foo: 'bar' }
wrapperShallow(<Dropdown onBlur={onBlur} />)
wrapper.childAt(0).simulate('blur', event)
onBlur.should.have.been.calledOnce()
onBlur.should.have.been.calledWithMatch(event)
})
it('calls handleChange with the selected option on blur', () => {
wrapperShallow(<Dropdown selectOnBlur options={options} />)
const instance = wrapper.instance()
wrapper.childAt(0).simulate('click', { stopPropagation: _.noop })
dropdownMenuIsOpen()
sandbox.spy(instance, 'handleChange')
const event = { stopPropagation: _.noop }
wrapper.childAt(0).simulate('blur', event)
instance.handleChange.should.have.been.calledWithMatch(event, options[0].value)
})
it('does not call handleChange if the value has not changed', () => {
wrapperShallow(<Dropdown selectOnBlur options={options} />)
const instance = wrapper.instance()
wrapper.setState({ selectedIndex: 2, value: options[2].value })
wrapper.childAt(0).simulate('click', { stopPropagation: _.noop })
dropdownMenuIsOpen()
sandbox.spy(instance, 'handleChange')
wrapper.childAt(0).simulate('blur')
instance.handleChange.should.not.have.been.called()
})
it('sets focus state to false', () => {
wrapperShallow(<Dropdown selectOnBlur />)
wrapper.childAt(0).simulate('blur')
wrapper.should.have.state('focus', false)
})
it('sets searchQuery state to empty', () => {
wrapperShallow(<Dropdown />)
wrapper.setState({ searchQuery: 'foo' })
wrapper.childAt(0).simulate('blur')
wrapper.should.have.state('searchQuery', '')
})
it('does not call onBlur when the mouse is down', () => {
const onBlur = sandbox.spy()
wrapperShallow(<Dropdown onBlur={onBlur} selectOnBlur />)
wrapper.simulate('mousedown')
wrapper.simulate('blur')
onBlur.should.not.have.been.called()
})
it('does not call makeSelectedItemActive when the mouse is down', () => {
const spy = sandbox.spy()
wrapperShallow(<Dropdown onBlur={spy} selectOnBlur />)
const instance = wrapper.instance()
sandbox.spy(instance, 'makeSelectedItemActive')
wrapper.simulate('mousedown')
wrapper.simulate('blur')
instance.makeSelectedItemActive.should.not.have.been.called()
})
it('does not set focus state when the mouse is down', () => {
wrapperShallow(<Dropdown />)
wrapper.setState({ focus: 'foo' })
wrapper.simulate('mousedown')
wrapper.simulate('blur')
wrapper.should.have.state('focus', 'foo')
})
})
describe('handleClose', () => {
it('is called when open changes to false', () => {
wrapperMount(<Dropdown options={options} selection />)
wrapper.simulate('click')
dropdownMenuIsOpen()
const instance = wrapper.instance()
sandbox.spy(instance, 'handleClose')
wrapper.simulate('click')
dropdownMenuIsClosed()
instance.handleClose.should.have.been.calledOnce()
})
it('prevents Space from opening a search Dropdown after selecting an item', () => {
// Prevent a bug where pressing space in another control opens the Dropdown
// https://github.com/Semantic-Org/Semantic-UI-React/issues/692
wrapperMount(<Dropdown options={options} search selection />)
// open, click an item, assert it is active and in the value
wrapper.simulate('click')
dropdownMenuIsOpen()
wrapper
.find('DropdownItem')
.first()
.simulate('click')
wrapper.should.have.state('value', options[0].value)
dropdownMenuIsClosed()
// The dropdown will be still focused after an item will be selected, we should remove
// focus from it before
document.activeElement.blur()
// doesn't open on space
domEvent.keyDown(document, { key: ' ' })
wrapper.update()
dropdownMenuIsClosed()
})
})
describe('closeOnChange', () => {
it('will close when defined and dropdown is multiple', () => {
wrapperMount(<Dropdown selection multiple search closeOnChange options={options} />).simulate(
'click',
)
dropdownMenuIsOpen()
wrapper
.find('DropdownItem')
.first()
.simulate('click', nativeEvent)
dropdownMenuIsClosed()
})
it('will remain open when undefined and dropdown is multiple', () => {
wrapperMount(<Dropdown selection multiple search options={options} />).simulate('click')
dropdownMenuIsOpen()
wrapper
.find('DropdownItem')
.first()
.simulate('click', nativeEvent)
dropdownMenuIsOpen()
})
})
describe('closeOnEscape', () => {
it('closes the dropdown when Escape key is pressed by default', () => {
wrapperMount(<Dropdown defaultOpen />)
dropdownMenuIsOpen()
domEvent.keyDown(document, { key: 'Escape' })
dropdownMenuIsClosed()
})
it('closes the dropdown when is "true" and Escape key is pressed', () => {
wrapperMount(<Dropdown defaultOpen closeOnEscape />)
dropdownMenuIsOpen()
domEvent.keyDown(document, { key: 'Escape' })
dropdownMenuIsClosed()
})
it('does not close the dropdown when false and Escape key is pressed', () => {
wrapperMount(<Dropdown defaultOpen closeOnEscape={false} />)
dropdownMenuIsOpen()
domEvent.keyDown(document, { key: 'Escape' })
dropdownMenuIsOpen()
})
})
describe('setSelectedIndex', () => {
it('will call setSelectedIndex if options change', () => {
wrapperMount(<Dropdown options={options} />)
const instance = wrapper.instance()
sandbox.spy(instance, 'setSelectedIndex')
wrapper.setProps({ options: [] })
instance.setSelectedIndex.should.have.been.calledOnce()
})
it('will not call setSelectedIndex if options have not changed', () => {
wrapperMount(<Dropdown options={options} />)
const instance = wrapper.instance()
sandbox.spy(instance, 'setSelectedIndex')
wrapper.setProps({ options })
instance.setSelectedIndex.should.not.have.been.calledOnce()
})
})
describe('selectedIndex', () => {
it('sets "selectedIndex" when an item was selected', () => {
const option = _.last(options)
wrapperMount(<Dropdown options={options} search selection />)
const input = wrapper.find('input.search')
// open, simulate search and select option
wrapper.simulate('click')
input.simulate('change', { target: { value: option.text } })
domEvent.keyDown(document, { key: 'Enter' })
wrapper.should.have.state('selectedIndex', 4)
// open again
wrapper.simulate('click')
wrapper.should.have.state('selectedIndex', 4)
})
})
describe('isMouseDown', () => {
it('tracks when the mouse is down', () => {
wrapperMount(<Dropdown />).simulate('mousedown')
wrapper.instance().isMouseDown.should.equal(true)
domEvent.mouseUp(document)
wrapper.instance().isMouseDown.should.equal(false)
})
})
describe('icon', () => {
it('defaults to a dropdown icon', () => {
Dropdown.defaultProps.icon.should.equal('dropdown')
wrapperRender(<Dropdown />).should.contain.descendants('.dropdown.icon')
})
it('always opens a dropdown on click', () => {
wrapperMount(<Dropdown options={options} selection search />)
.find('i.icon')
.simulate('click')
dropdownMenuIsOpen()
})
it('always opens a dropdown on click', () => {
wrapperMount(<Dropdown options={options} selection search />)
.find('i.icon')
.simulate('click')
dropdownMenuIsOpen()
})
it('passes onClick handler', () => {
const event = { target: null }
const onClick = sandbox.spy()
const props = { name: 'user', onClick }
wrapperMount(<Dropdown icon={props} options={options} />)
.find('i.icon')
.simulate('click', event)
onClick.should.have.been.calledOnce()
onClick.should.have.been.calledWithMatch(event, props)
})
})
describe('searchQuery', () => {
it('defaults to empty string', () => {
shallow(<Dropdown />).should.have.state('searchQuery', '')
})
it('passes value to state', () => {
shallow(<Dropdown searchQuery='foo' />).should.have.state('searchQuery', 'foo')
})
})
describe('selected item', () => {
it('defaults to the first item', () => {
wrapperShallow(<Dropdown options={options} selection />)
.find('DropdownItem')
.first()
.should.have.prop('selected', true)
})
it('defaults to the first non-disabled item', () => {
options[0].disabled = true
wrapperShallow(<Dropdown options={options} selection />)
// selection moved to second item
wrapper
.find('DropdownItem')
.first()
.should.have.prop('selected', false)
wrapper
.find('DropdownItem')
.at(1)
.should.have.prop('selected', true)
})
it('defaults to selected item when options are initially empty', () => {
const randomIndex = 1 + _.random(options.length - 2)
const value = options[randomIndex].value
wrapperShallow(<Dropdown options={[]} selection value={value} />)
wrapper.setProps({ options, value })
wrapper
.find('DropdownItem')
.at(randomIndex)
.should.have.prop('selected', true)
})
it('is null when all options disabled', () => {
const disabledOptions = options.map((o) => ({ ...o, disabled: true }))
wrapperRender(<Dropdown options={disabledOptions} selection />).should.not.have.descendants(
'.selected',
)
})
it('is set when clicking an item', () => {
// random item, skip the first as it's selected by default
const randomIndex = 1 + _.random(options.length - 2)
wrapperMount(<Dropdown options={options} selection />)
wrapper
.find('DropdownItem')
.at(randomIndex)
.simulate('click')
wrapper
.find('DropdownItem')
.at(randomIndex)
.should.have.prop('selected', true)
})
it('is ignored when clicking a disabled item', () => {
// random item, skip the first as it's selected by default
const randomIndex = 1 + _.random(options.length - 2)
options[randomIndex].disabled = true
wrapperMount(<Dropdown options={options} selection />)
.simulate('click', nativeEvent)
.find('DropdownItem')
.at(randomIndex)
.simulate('click', nativeEvent)
.should.not.have.prop('selected', true)
dropdownMenuIsOpen()
})
it('moves down on arrow down when open', () => {
wrapperMount(<Dropdown options={options} selection />)
// open
wrapper.simulate('click')
dropdownMenuIsOpen()
// arrow down
domEvent.keyDown(document, { key: 'ArrowDown' })
wrapper.update()
// selection moved to second item
wrapper
.find('DropdownItem')
.first()
.should.have.prop('selected', false)
wrapper
.find('DropdownItem')
.at(1)
.should.have.prop('selected', true)
})
it('moves up on arrow up when open', () => {
wrapperMount(<Dropdown options={options} selection />)
// open
wrapper
.simulate('click')
.find('DropdownItem')
.first()
.should.have.prop('selected', true)
// arrow down
domEvent.keyDown(document, { key: 'ArrowUp' })
wrapper.update()
// selection moved to last item
wrapper
.find('DropdownItem')
.first()
.should.have.prop('selected', false)
wrapper
.find('DropdownItem')
.at(options.length - 1)
.should.have.prop('selected', true)
})
it('skips over items filtered by search', () => {
const opts = [
{ text: 'a1', value: 'a1' },
{ text: 'skip this one', value: 'skip this one' },
{ text: 'a2', value: 'a2' },
]
// search for 'a'
wrapperMount(<Dropdown options={opts} search selection />)
.simulate('click')
.find('input.search')
.simulate('change', { target: { value: 'a' } })
wrapper.find('.selected').should.contain.text('a1')
// move selection down
domEvent.keyDown(document, { key: 'ArrowDown' })
wrapper.update()
wrapper.find('.selected').should.contain.text('a2')
})
it('filters diacritics on options when using deburr prop', () => {
const inputText = 'floresti'
const textToFind = 'FLOREŞTI'
const opts = [
{ text: textToFind, value: '1' },
{ text: `ŞANŢU ${textToFind}`, value: '2' },
{ text: `${textToFind} Alba`, value: '3' },
]
// search for 'floresti'
wrapperMount(<Dropdown options={opts} search deburr selection />)
.simulate('click')
.find('input.search')
.simulate('change', { target: { value: inputText } })
wrapper.find('.selected').should.contain.text(textToFind)
})
it('filters diacritics on input when using deburr prop', () => {
const inputText = 'FLORÉŞTI'
const textToFind = 'FLORESTI'
const opts = [
{ text: textToFind, value: '1' },
{ text: `SANTU ${textToFind}`, value: '2' },
{ text: `${textToFind} Alba`, value: '3' },
]
// search for 'floresti'
wrapperMount(<Dropdown options={opts} search deburr selection />)
.simulate('click')
.find('input.search')
.simulate('change', { target: { value: inputText } })
wrapper.find('.selected').should.contain.text(textToFind)
})
it('should not filter diacritics when deburr is not set', () => {
const inputText = 'FLORÉŞTI'
const textToFind = 'FLORESTI'
// Add this in case the default 'no results text' changes.
const noResultsText = 'NoResultsFound'
const opts = [
{ text: textToFind, value: '1' },
{ text: `SANTU ${textToFind}`, value: '2' },
{ text: `${textToFind} Alba`, value: '3' },
]
// search for 'floresti'
wrapperMount(<Dropdown options={opts} search selection noResultsMessage={noResultsText} />)
.simulate('click')
.find('input.search')
.simulate('change', { target: { value: inputText } })
wrapper.find('.message').should.contain.text(noResultsText)
})
it('still works after encountering "no results"', () => {
const opts = [
{ text: 'a1', value: 'a1' },
{ text: 'a2', value: 'a2' },
{ text: 'a3', value: 'a3' },
]
wrapperMount(<Dropdown options={opts} search selection />)
// search for 'a4'
// no results appears
wrapper
.simulate('click')
.find('input.search')
.simulate('change', { target: { value: 'a4' } })
wrapper.should.have.exactly(1).descendants('.message')
// search for 'a' (simulated backspace)
// no results is removed
// first item is selected
// down arrow moves selection
wrapper.find('input.search').simulate('change', { target: { value: 'a' } })
wrapper.should.not.have.descendants('.message')
wrapper.should.have
.exactly(1)
.descendants('.selected')
.which.contain.text('a1')
// move selection down
domEvent.keyDown(document, { key: 'ArrowDown' })
wrapper.should.have
.exactly(1)
.descendants('.selected')
.which.contain.text('a2')
})
it('skips over disabled items', () => {
const opts = [
{ text: 'a1', value: 'a1' },
{ text: 'skip this one', value: 'skip this one', disabled: true },
{ text: 'a2', value: 'a2' },
]
wrapperMount(<Dropdown options={opts} search selection />)
wrapper.simulate('click')
wrapper.find('.selected').should.contain.text('a1')
// move selection down
domEvent.keyDown(document, { key: 'ArrowDown' })
wrapper.update()
wrapper.find('.selected').should.contain.text('a2')
})
it('does not enter an infinite loop when all items are disabled', () => {
const opts = [
{ text: '1', value: '1', disabled: true },
{ text: '2', value: '2', disabled: true },
]
wrapperMount(<Dropdown options={opts} search selection />).simulate('click')
const instance = wrapper.instance()
sandbox.spy(instance, 'moveSelectionBy')
// move selection down
domEvent.keyDown(document, { key: 'ArrowDown' })
instance.moveSelectionBy.should.have.been.calledOnce()
})
it('scrolls the selected item into view', () => {
// get enough options to make the menu scrollable
const opts = getOptions(20)
wrapperMount(<Dropdown options={opts} selection />).simulate('click')
dropdownMenuIsOpen()
const menu = document.querySelector('.ui.dropdown .menu.visible')
// Limit the menu's height and set an overflow so it's scrollable