Skip to content

Commit 0670a37

Browse files
committed
Dropdown: remove .state() usage in Enzyme
1 parent cb4b816 commit 0670a37

File tree

2 files changed

+68
-73
lines changed

2 files changed

+68
-73
lines changed

test/specs/modules/Dropdown/Dropdown-test.js

Lines changed: 67 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,30 @@ const dropdownMenuIsClosed = () => {
5858
}
5959
}
6060

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+
6185
const dropdownMenuIsOpen = () => {
6286
wrapper.childAt(0).should.have.className('active')
6387
wrapper.childAt(0).should.have.className('visible')
@@ -141,7 +165,8 @@ describe('Dropdown', () => {
141165

142166
describe('defaultSearchQuery', () => {
143167
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')
145170
})
146171
})
147172

@@ -369,22 +394,23 @@ describe('Dropdown', () => {
369394
const event = { stopPropagation: _.noop }
370395
const onChange = sandbox.spy()
371396

372-
wrapperShallow(
397+
wrapperMount(
373398
<Dropdown defaultValue={defaultValue} clearable onChange={onChange} options={options} />,
374399
)
375-
wrapper.find('Icon').simulate('click', event)
400+
wrapper.find('i.clear').simulate('click', event)
376401

377402
onChange.should.have.been.calledOnce()
378403
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')
380406
})
381407

382408
it('clears when value is multiple and is not empty', () => {
383409
const defaultValue = _.map(options, 'value')
384410
const event = { stopPropagation: _.noop }
385411
const onChange = sandbox.spy()
386412

387-
wrapperShallow(
413+
wrapperMount(
388414
<Dropdown
389415
defaultValue={defaultValue}
390416
clearable
@@ -393,11 +419,12 @@ describe('Dropdown', () => {
393419
options={options}
394420
/>,
395421
)
396-
wrapper.find('Icon').simulate('click', event)
422+
wrapper.find('i.clear').simulate('click', event)
397423

398424
onChange.should.have.been.calledOnce()
399425
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')
401428
})
402429
})
403430

@@ -447,19 +474,11 @@ describe('Dropdown', () => {
447474
onChange.should.have.been.calledOnce()
448475
})
449476

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-
457477
it('sets searchQuery state to empty', () => {
458-
wrapperShallow(<Dropdown />)
478+
wrapperMount(<Dropdown defaultSearchQuery='foo' search />)
459479

460-
wrapper.setState({ searchQuery: 'foo' })
461480
wrapper.childAt(0).simulate('blur')
462-
wrapper.should.have.state('searchQuery', '')
481+
wrapper.find('input.search').should.have.value('')
463482
})
464483

465484
it('does not call onBlur when the mouse is down', () => {
@@ -486,15 +505,6 @@ describe('Dropdown', () => {
486505

487506
instance.makeSelectedItemActive.should.not.have.been.called()
488507
})
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-
})
498508
})
499509

500510
describe('handleClose', () => {
@@ -522,7 +532,7 @@ describe('Dropdown', () => {
522532
dropdownMenuIsOpen()
523533

524534
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')
526536
dropdownMenuIsClosed()
527537

528538
// The dropdown will be still focused after an item will be selected, we should remove
@@ -595,21 +605,22 @@ describe('Dropdown', () => {
595605

596606
wrapper.simulate('click')
597607
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')
599610

600611
wrapper.setProps({ options: [] })
601-
wrapper.should.have.not.state('selectedIndex')
612+
wrapper.should.not.have.descendants('.selected.item')
602613
})
603614

604615
it('will not call setSelectedIndex if options have not changed', () => {
605616
wrapperMount(<Dropdown options={options} />)
606617

607618
wrapper.simulate('click')
608619
wrapper.simulate('keydown', { key: 'ArrowDown' })
609-
wrapper.should.have.state('selectedIndex', 1)
620+
wrapper.find('.item').at(1).should.have.className('selected')
610621

611622
wrapper.setProps({ options })
612-
wrapper.should.have.state('selectedIndex', 1)
623+
wrapper.find('.item').at(1).should.have.className('selected')
613624
})
614625
})
615626

@@ -623,16 +634,15 @@ describe('Dropdown', () => {
623634
// open, simulate search and select option
624635
wrapper.simulate('click')
625636
wrapper.simulate('keydown', { key: 'ArrowDown' })
626-
wrapper.should.have.state('selectedIndex', 1)
637+
wrapper.find('.item').at(1).should.have.className('selected')
627638

628639
input.simulate('change', { target: { value: option.text } })
629640
wrapper.simulate('keydown', { key: 'Enter' })
630-
631-
wrapper.should.have.state('selectedIndex', 4)
641+
wrapper.find('.item').at(4).should.have.className('selected')
632642

633643
// open again
634644
wrapper.simulate('click')
635-
wrapper.should.have.state('selectedIndex', 4)
645+
wrapper.find('.item').at(4).should.have.className('selected')
636646
})
637647

638648
it('keeps "selectedIndex" when the same item was selected', () => {
@@ -644,13 +654,12 @@ describe('Dropdown', () => {
644654
// simulate search and select option
645655
input.simulate('change', { target: { value: option.text } })
646656
wrapper.simulate('keydown', { key: 'Enter' })
647-
648-
wrapper.should.have.state('selectedIndex', 4)
657+
wrapper.find('.item').at(4).should.have.className('selected')
649658

650659
// select the same option again
651660
input.simulate('change', { target: { value: option.text } })
652661
wrapper.simulate('keydown', { key: 'Enter' })
653-
wrapper.should.have.state('selectedIndex', 4)
662+
wrapper.find('.item').at(4).should.have.className('selected')
654663
})
655664
})
656665

@@ -703,11 +712,13 @@ describe('Dropdown', () => {
703712

704713
describe('searchQuery', () => {
705714
it('defaults to empty string', () => {
706-
shallow(<Dropdown />).should.have.state('searchQuery', '')
715+
wrapperMount(<Dropdown search />)
716+
wrapper.find('input.search').should.have.value('')
707717
})
708718

709719
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')
711722
})
712723
})
713724

@@ -1054,7 +1065,7 @@ describe('Dropdown', () => {
10541065
wrapper.simulate('click')
10551066
wrapper.simulate('keydown', { key: 'ArrowDown' })
10561067

1057-
wrapper.should.have.state('searchQuery', 'foo')
1068+
wrapper.find('input.search').should.have.value('foo')
10581069
})
10591070
})
10601071

@@ -1110,17 +1121,15 @@ describe('Dropdown', () => {
11101121

11111122
wrapper.simulate('click')
11121123
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')
11151125
})
11161126

11171127
it('updates value on up arrow', () => {
11181128
wrapperMount(<Dropdown options={options} selection />)
11191129

11201130
wrapper.simulate('click')
11211131
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')
11241133
})
11251134
})
11261135

@@ -1359,15 +1368,15 @@ describe('Dropdown', () => {
13591368

13601369
it('handles focus correctly', () => {
13611370
wrapperMount(<Dropdown options={options} selection />)
1362-
wrapper.should.have.state('focus', false)
1371+
bodyIsFocused()
13631372

13641373
// focus
13651374
wrapper.getDOMNode().focus()
1366-
wrapper.should.have.state('focus', true)
1375+
dropdownIsFocused()
13671376

13681377
// click outside
13691378
domEvent.click(document.body)
1370-
wrapper.should.have.state('focus', false)
1379+
bodyIsFocused()
13711380
})
13721381

13731382
it('closes on esc key', () => {
@@ -1718,6 +1727,7 @@ describe('Dropdown', () => {
17181727
spy.should.have.been.calledOnce()
17191728
spy.should.have.been.calledWithMatch({}, { value: expected })
17201729
})
1730+
17211731
it('removes the last item when there is no search query when uncontrolled', () => {
17221732
const value = _.map(options, 'value')
17231733
const expected = _.dropRight(value)
@@ -1734,14 +1744,12 @@ describe('Dropdown', () => {
17341744

17351745
// open
17361746
wrapper.simulate('click')
1737-
17381747
domEvent.keyDown(document, { key: 'Backspace' })
17391748

17401749
spy.should.have.been.calledOnce()
17411750
spy.should.have.been.calledWithMatch({}, { value: expected })
1742-
1743-
wrapper.state('value').should.deep.equal(expected)
17441751
})
1752+
17451753
it('does not remove the last item when there is a search query', () => {
17461754
// search for random item
17471755
const searchQuery = _.sample(options).text
@@ -2099,7 +2107,7 @@ describe('Dropdown', () => {
20992107
wrapper.find('DropdownItem').first().simulate('click')
21002108

21012109
// bye bye search query
2102-
wrapper.should.have.state('searchQuery', '')
2110+
wrapper.find('input.search').should.have.value('')
21032111
})
21042112

21052113
it('opens the menu on change if there is a query and not already open', () => {
@@ -2173,10 +2181,10 @@ describe('Dropdown', () => {
21732181
// avoid it to prevent false positives
21742182
wrapper.simulate('click')
21752183
wrapper.simulate('keydown', { key: 'ArrowUp' })
2176-
wrapper.should.have.state('selectedIndex', 3)
2184+
wrapper.find('.item').at(3).should.have.className('selected')
21772185

21782186
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')
21802188
})
21812189

21822190
it('still allows moving selection after blur/focus', () => {
@@ -2226,13 +2234,7 @@ describe('Dropdown', () => {
22262234
.simulate('click', nativeEvent)
22272235

22282236
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()
22362238
})
22372239

22382240
it('sets focus to the dropdown after selection', () => {
@@ -2247,14 +2249,7 @@ describe('Dropdown', () => {
22472249
// TODO: try reenable after Enzyme update
22482250
// https://github.com/Semantic-Org/Semantic-UI-React/pull/3747#issuecomment-522018329
22492251
// 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()
22582253
})
22592254
})
22602255

@@ -2687,7 +2682,7 @@ describe('Dropdown', () => {
26872682
search.simulate('change', { target: { value: 'boo' } })
26882683
search.simulate('keydown', { key: 'Enter' })
26892684

2690-
wrapper.should.have.state('searchQuery', '')
2685+
wrapper.find('input.search').should.have.value('')
26912686
})
26922687
})
26932688

@@ -2755,7 +2750,7 @@ describe('Dropdown', () => {
27552750
wrapper.simulate('keydown', { key: 'ArrowDown' })
27562751

27572752
onChange.should.have.been.called()
2758-
wrapper.should.have.state('value', options[1].value)
2753+
wrapper.find('.item').at(1).should.have.className('active')
27592754
})
27602755

27612756
it('does not change value when set to false', () => {
@@ -2776,7 +2771,7 @@ describe('Dropdown', () => {
27762771
wrapper.simulate('keydown', { key: 'ArrowDown' })
27772772

27782773
onChange.should.not.have.been.called()
2779-
wrapper.should.have.state('value', value)
2774+
wrapper.find('.item').at(0).should.have.className('active')
27802775
})
27812776
})
27822777

test/specs/modules/Transition/Transition-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let wrapper
1717
const wrapperMount = (...args) => (wrapper = mount(...args))
1818
const wrapperShallow = (...args) => (wrapper = shallow(...args))
1919

20-
describe.only('Transition', () => {
20+
describe('Transition', () => {
2121
common.hasSubcomponents(Transition, [TransitionGroup])
2222
common.hasValidTypings(Transition)
2323

0 commit comments

Comments
 (0)