-
Notifications
You must be signed in to change notification settings - Fork 268
/
index.keyboardNav.test.js
255 lines (218 loc) · 9.74 KB
/
index.keyboardNav.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
import test from 'ava'
import React from 'react'
import { spy, stub, assert } from 'sinon'
import { mount } from 'enzyme'
import DropdownTreeSelect from './index'
const node = (id, label) => ({ id, label, value: label })
const tree = {
...node('c1', 'ccc 1'),
children: [
{
...node('c2', 'ccc 2'),
children: [node('a1', 'aaa 1'), node('a2', 'aaa 2')],
},
{
...node('c3', 'ccc 3'),
children: [node('a3', 'aaa 3'), node('b1', 'bbb 1'), node('b2', 'bbb 2')],
},
],
}
const triggerOnKeyboardKeyDown = (wrapper, keys) => {
const elements = [].concat(keys || [])
elements.forEach(key => wrapper.find('.search').simulate('keyDown', { key }))
}
test('some key presses opens dropdown on keyboardNavigation', t => {
;['ArrowUp', 'ArrowDown', 'Home', 'PageUp', 'End', 'PageDown', 'a', 'B'].forEach(key => {
const wrapper = mount(<DropdownTreeSelect data={tree} />)
triggerOnKeyboardKeyDown(wrapper, key)
t.true(wrapper.state().showDropdown)
})
})
test('esc closes dropdown on keyboardNavigation', async t => {
const wrapper = mount(<DropdownTreeSelect data={tree} />)
triggerOnKeyboardKeyDown(wrapper, ['ArrowDown', 'Escape'])
const showDropdown = await new Promise(resolve =>
setTimeout(() => {
resolve(wrapper.state().showDropdown)
}, 1)
)
t.false(showDropdown)
})
test('other key presses does not open dropdown on keyboardNavigation', t => {
;['Enter', 'ArrowLeft', 'ArrowRight'].forEach(key => {
const wrapper = mount(<DropdownTreeSelect data={tree} />)
triggerOnKeyboardKeyDown(wrapper, key)
t.false(wrapper.state().showDropdown)
})
})
test('can navigate and focus child on keyboardNavigation', t => {
const wrapper = mount(<DropdownTreeSelect data={tree} />)
triggerOnKeyboardKeyDown(wrapper, [
'ArrowDown',
'ArrowRight',
'ArrowRight',
'ArrowDown',
'ArrowRight',
'ArrowRight',
'ArrowDown',
])
t.deepEqual(wrapper.find('li.focused').text(), 'bbb 1')
})
test('can navigate circular on keyboardNavigation', t => {
const wrapper = mount(<DropdownTreeSelect data={tree} />)
triggerOnKeyboardKeyDown(wrapper, ['ArrowDown', 'ArrowRight', 'ArrowRight', 'ArrowDown', 'ArrowDown'])
t.deepEqual(wrapper.find('li.focused').text(), 'ccc 1')
triggerOnKeyboardKeyDown(wrapper, 'ArrowUp')
t.deepEqual(wrapper.find('li.focused').text(), 'ccc 3')
})
test('can navigate to edge on keyboardNavigation', t => {
const wrapper = mount(<DropdownTreeSelect data={tree} />)
triggerOnKeyboardKeyDown(wrapper, ['ArrowDown', 'ArrowRight', 'ArrowRight', 'ArrowRight'])
;['PageDown', 'PageUp', 'End', 'Home'].forEach((key, index) => {
const expected = index % 2 === 0 ? 'ccc 3' : 'ccc 1'
triggerOnKeyboardKeyDown(wrapper, key)
t.deepEqual(wrapper.find('li.focused').text(), expected)
})
})
test('can collapse on keyboardNavigation', t => {
const wrapper = mount(<DropdownTreeSelect data={tree} />)
triggerOnKeyboardKeyDown(wrapper, ['ArrowDown', 'ArrowRight', 'ArrowRight', 'ArrowDown'])
t.deepEqual(wrapper.find('li.focused').text(), 'ccc 3')
triggerOnKeyboardKeyDown(wrapper, 'ArrowLeft')
t.deepEqual(wrapper.find('li.focused').text(), 'ccc 1')
t.true(wrapper.find('#c3').exists())
triggerOnKeyboardKeyDown(wrapper, 'ArrowLeft')
t.false(wrapper.find('#c3').exists())
})
test('can navigate searchresult on keyboardNavigation', t => {
const wrapper = mount(<DropdownTreeSelect data={tree} showDropdown="initial" />)
wrapper.instance().onInputChange('bb')
triggerOnKeyboardKeyDown(wrapper, ['b', 'ArrowDown', 'ArrowDown', 'ArrowDown'])
t.deepEqual(wrapper.find('li.focused').text(), 'bbb 1')
})
test('can navigate with keepTreOnSearch on keyboardNavigation', t => {
const wrapper = mount(<DropdownTreeSelect data={tree} keepTreeOnSearch />)
wrapper.instance().onInputChange('bb')
triggerOnKeyboardKeyDown(wrapper, ['b', 'ArrowDown', 'ArrowDown', 'ArrowDown'])
t.deepEqual(wrapper.find('li.focused').text(), 'bbb 1')
t.true(wrapper.find('#c1').exists())
})
test('can delete tags on empty search input with backspace on keyboardNavigation', t => {
const data = [{ ...node('a', 'a'), checked: true }, { ...node('b', 'b'), checked: true }]
const wrapper = mount(<DropdownTreeSelect data={data} />)
wrapper.instance().searchInput.value = 'x'
triggerOnKeyboardKeyDown(wrapper, 'Backspace')
t.deepEqual(wrapper.state().tags.length, 2)
wrapper.instance().searchInput.value = ''
triggerOnKeyboardKeyDown(wrapper, 'Backspace')
t.deepEqual(wrapper.state().tags.length, 1)
triggerOnKeyboardKeyDown(wrapper, 'Backspace')
t.deepEqual(wrapper.state().tags.length, 0)
})
test('can select with enter on keyboardNavigation', t => {
const wrapper = mount(<DropdownTreeSelect data={tree} />)
triggerOnKeyboardKeyDown(wrapper, ['ArrowDown', 'ArrowDown', 'Enter'])
t.deepEqual(wrapper.state().tags.length, 1)
})
test('can delete tags with backspace/delete on keyboardNavigation', t => {
const data = [{ ...node('a', 'a'), checked: true }, { ...node('b', 'b'), checked: true }]
const wrapper = mount(<DropdownTreeSelect data={data} />)
const event = {
type: 'keydown',
stopPropagation: spy(),
nativeEvent: { stopImmediatePropagation: spy() },
}
wrapper.find('#a_tag > .tag-remove').simulate('keyDown', { ...event, key: 'Backspace' })
t.deepEqual(wrapper.state().tags.length, 1)
wrapper.find('#b_tag > .tag-remove').simulate('keyUp', { ...event, key: 'Delete' })
t.deepEqual(wrapper.state().tags.length, 0)
})
test('cannot delete tags on empty search input with backspace on keyboardNavigation when disablePoppingOnBackspace is true', t => {
const data = [{ ...node('a', 'a'), checked: true }, { ...node('b', 'b'), checked: true }]
const wrapper = mount(<DropdownTreeSelect data={data} disablePoppingOnBackspace={true} />)
wrapper.instance().searchInput.value = 'x'
triggerOnKeyboardKeyDown(wrapper, 'Backspace')
t.deepEqual(wrapper.state().tags.length, 2)
wrapper.instance().searchInput.value = ''
triggerOnKeyboardKeyDown(wrapper, 'Backspace')
t.deepEqual(wrapper.state().tags.length, 2)
})
test('remembers current focus between prop updates', t => {
const wrapper = mount(<DropdownTreeSelect data={tree} showDropdown="initial" />)
t.false(wrapper.find('li.focused').exists())
triggerOnKeyboardKeyDown(wrapper, ['ArrowDown'])
t.deepEqual(wrapper.find('li.focused').text(), 'ccc 1')
wrapper.setProps({ data: tree })
wrapper.instance().handleClick()
t.deepEqual(wrapper.find('li.focused').text(), 'ccc 1')
})
test('should set current focus as selected on tab out for simpleSelect', t => {
const wrapper = mount(<DropdownTreeSelect data={tree} mode="simpleSelect" />)
triggerOnKeyboardKeyDown(wrapper, ['ArrowDown', 'ArrowRight', 'ArrowRight', 'Tab'])
t.deepEqual(wrapper.state().tags[0].label, 'ccc 1')
})
test('should scroll on keyboard navigation', t => {
const largeTree = [...Array(150).keys()].map(i => node(`id${i}`, `label${i}`))
const scrollIntoView = (Element.prototype.scrollIntoView = spy())
const wrapper = mount(<DropdownTreeSelect data={largeTree} showDropdown="initial" />)
const getElementById = stub(document, 'getElementById')
const contentNode = wrapper.find('.dropdown-content').getDOMNode()
t.deepEqual(contentNode.scrollTop, 0)
triggerOnKeyboardKeyDown(wrapper, ['ArrowUp'])
largeTree.forEach((n, index) => {
getElementById.withArgs(`${n.id}_li`).returns({ offsetTop: index, clientHeight: 1, scrollIntoView })
})
triggerOnKeyboardKeyDown(wrapper, ['ArrowUp'])
t.deepEqual(wrapper.find('li.focused').text(), 'label148')
assert.calledOnce(scrollIntoView)
getElementById.restore()
})
test('should only scroll on keyboard navigation', t => {
const largeTree = [...Array(150).keys()].map(i => node(`id${i}`, `label${i}`))
const getElementById = stub(document, 'getElementById')
const scrollIntoView = (Element.prototype.scrollIntoView = spy())
const wrapper = mount(<DropdownTreeSelect data={largeTree} showDropdown="initial" />)
const contentNode = wrapper.find('.dropdown-content').getDOMNode()
triggerOnKeyboardKeyDown(wrapper, ['ArrowUp'])
largeTree.forEach((n, index) => {
getElementById.withArgs(`${n.id}_li`).returns({ offsetTop: index, clientHeight: 1, scrollIntoView })
})
triggerOnKeyboardKeyDown(wrapper, ['ArrowUp'])
const scrollTop = contentNode.scrollTop
// Simulate scroll up and setting new props
contentNode.scrollTop -= 20
const newTree = largeTree.map(n => {
return { checked: true, ...n }
})
wrapper.setProps({ data: newTree, showDropdown: 'initial' })
t.notDeepEqual(contentNode.scrollTop, scrollTop)
// Verify scroll is restored to previous position after keyboard nav
triggerOnKeyboardKeyDown(wrapper, ['ArrowUp', 'ArrowDown'])
// Called once for each input, 3 in this case.
assert.calledThrice(scrollIntoView)
getElementById.restore()
})
const keyDownTests = [
{ keyCode: 13, expected: true }, // Enter
{ keyCode: 32, expected: true }, // Space
{ keyCode: 40, expected: true }, // Arrow down
{ keyCode: 9, expected: false }, // Tab
{ keyCode: 38, expected: false }, // Up arrow
]
keyDownTests.forEach(testArgs => {
test(`Key code ${testArgs.keyCode} ${testArgs.expected ? 'can' : "can't"} open dropdown on keyDown`, t => {
const wrapper = mount(<DropdownTreeSelect data={tree} />)
const trigger = wrapper.find('.dropdown-trigger')
trigger.instance().focus()
trigger.simulate('keyDown', { key: 'mock', keyCode: testArgs.keyCode })
t.is(wrapper.state().showDropdown, testArgs.expected)
})
})
test(`Key event should not trigger if not focused/active element`, t => {
const wrapper = mount(<DropdownTreeSelect data={tree} />)
const trigger = wrapper.find('.dropdown-trigger')
const input = wrapper.find('.search')
input.instance().focus()
trigger.simulate('keyDown', { key: 'mock', keyCode: 13 })
t.is(wrapper.state().showDropdown, false)
})