-
Notifications
You must be signed in to change notification settings - Fork 938
Expand file tree
/
Copy pathdownshift.get-item-props.js
More file actions
401 lines (355 loc) · 11.2 KB
/
downshift.get-item-props.js
File metadata and controls
401 lines (355 loc) · 11.2 KB
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
import * as React from 'react'
import {render, fireEvent, screen} from '@testing-library/react'
import Downshift from '../'
import {setIdCounter} from '../utils'
beforeEach(() => {
setIdCounter(1)
jest.spyOn(console, 'error').mockImplementation(() => {})
})
afterEach(() => console.error.mockRestore())
test('clicking on a DOM node within an item selects that item', () => {
// inspiration: https://github.com/downshift-js/downshift/issues/113
const items = [{item: 'Chess'}, {item: 'Dominion'}, {item: 'Checkers'}]
const {childrenSpy} = renderDownshift({items})
const firstButton = screen.queryByTestId('item-0-button')
childrenSpy.mockClear()
fireEvent.click(firstButton)
expect(childrenSpy).toHaveBeenCalledWith(
expect.objectContaining({
selectedItem: items[0].item,
}),
)
})
test('clicking anywhere within the rendered downshift but outside an item does not select an item', () => {
const childrenSpy = jest.fn(() => (
<div>
<button />
</div>
))
const {container} = render(<Downshift>{childrenSpy}</Downshift>)
childrenSpy.mockClear()
fireEvent.click(container.querySelector('button'))
expect(childrenSpy).not.toHaveBeenCalled()
})
test('on mousemove of an item updates the highlightedIndex to that item', () => {
const {childrenSpy} = renderDownshift()
const thirdButton = screen.queryByTestId('item-2-button')
childrenSpy.mockClear()
fireEvent.mouseMove(thirdButton)
expect(childrenSpy).toHaveBeenCalledWith(
expect.objectContaining({
highlightedIndex: 2,
}),
)
})
test('on mousemove of the highlighted item should not emit changes', () => {
const {childrenSpy} = renderDownshift({
props: {defaultHighlightedIndex: 1},
})
const secondButton = screen.queryByTestId('item-1-button')
childrenSpy.mockClear()
fireEvent.mouseMove(secondButton)
expect(childrenSpy).not.toHaveBeenCalled()
})
test('on mousedown of the item should not change current focused element', () => {
const childrenSpy = jest.fn(({getItemProps}) => (
<div>
<button data-testid="external-button" />
<div {...getItemProps({item: 'item-0'})}>
<button data-testid="in-item-button" />
</div>
</div>
))
render(<Downshift>{childrenSpy}</Downshift>)
const externalButton = screen.queryByTestId('external-button')
const inItemButton = screen.queryByTestId('in-item-button')
childrenSpy.mockClear()
externalButton.focus()
expect(externalButton).toHaveFocus()
fireEvent.mouseDown(inItemButton)
expect(externalButton).toHaveFocus()
})
test('after selecting an item highlightedIndex should be reset to defaultHighlightIndex', () => {
const {childrenSpy} = renderDownshift({
props: {defaultHighlightedIndex: 1},
})
const firstButton = screen.queryByTestId('item-0-button')
childrenSpy.mockClear()
fireEvent.click(firstButton)
expect(childrenSpy).toHaveBeenCalledWith(
expect.objectContaining({
highlightedIndex: 1,
}),
)
})
test('getItemProps logs a helpful error when no object is given', () => {
render(
<Downshift>
{({getItemProps}) => (
<div>
<span {...getItemProps()} />
</div>
)}
</Downshift>,
)
expect(console.error.mock.calls[0][0]).toMatchSnapshot()
})
test('getItemProps defaults the index when no index is given', () => {
expect(
render(
<Downshift>
{({getItemProps}) => (
<div>
<span {...getItemProps({item: 0})}>0</span>
<span {...getItemProps({item: 1})}>1</span>
<span {...getItemProps({item: 2})}>2</span>
<span {...getItemProps({item: 3})}>3</span>
<span {...getItemProps({item: 4})}>4</span>
</div>
)}
</Downshift>,
).container.firstChild,
).toMatchSnapshot()
})
test('getItemProps logs error when no item is given', () => {
render(
<Downshift>
{({getItemProps}) => (
<div>
<span {...getItemProps({index: 0})} />
</div>
)}
</Downshift>,
)
expect(console.error.mock.calls[0][0]).toMatchSnapshot()
})
// normally this test would be like the others where we render and then simulate a click on an
// item to ensure that a disabled item cannot be clicked, however this is only a problem in IE11
// so we have to get into the implementation details a little bit (unless we want to run these tests
// in IE11... no thank you 🙅)
test(`getItemProps doesn't include event handlers when disabled is passed (for IE11 support)`, () => {
const {getItemProps} = setupWithDownshiftController()
const props = getItemProps({item: 'dog', disabled: true})
const entry = Object.entries(props).find(
// eslint-disable-next-line jest/no-conditional-in-test
([key, value]) => key !== 'onMouseDown' && typeof value === 'function',
)
// eslint-disable-next-line jest/no-conditional-in-test
if (entry) {
throw new Error(
`getItemProps should not have any props that are callbacks. It has ${entry[0]}.`,
)
}
})
test(`disabled item can't be selected by pressing enter`, () => {
const items = [
{item: 'Chess', disabled: true},
{item: 'Dominion', disabled: true},
{item: 'Checkers', disabled: true},
]
const utils = renderDownshift({items})
const {input, arrowDownInput, enterOnInput, changeInputValue} = utils
const firstItem = screen.queryByTestId('item-0')
// eslint-disable-next-line jest-dom/prefer-enabled-disabled
expect(firstItem).toHaveAttribute('disabled')
changeInputValue('c')
// ↓
arrowDownInput()
// ENTER to select the first one
enterOnInput()
// item was not selected -> input value should still be 'c'
expect(input).toHaveValue('c')
})
test(`disabled item can't be highlighted when navigating via keyDown`, () => {
const items = [
{item: 'Chess'},
{item: 'Dominion', disabled: true},
{item: 'Checkers'},
{item: 'Backgammon'},
]
const utils = renderDownshift({items, props: {initialHighlightedIndex: 0}})
const {input, arrowDownInput, enterOnInput} = utils
// ↓
arrowDownInput()
// ↓ (should skip the first and second option)
// ENTER to select
enterOnInput()
expect(input).toHaveValue('Checkers')
})
test(`disabled item can't be highlighted and may wrap when navigating via keyDown`, () => {
const items = [
{item: 'Chess'},
{item: 'Dominion'},
{item: 'Checkers', disabled: true},
{item: 'Backgammon', disabled: true},
]
const utils = renderDownshift({items, props: {initialHighlightedIndex: 1}})
const {input, arrowDownInput, enterOnInput} = utils
// ↓
arrowDownInput()
// ↓ (should skip the first and second option)
// ENTER to select
enterOnInput()
expect(input).toHaveValue('Chess')
})
test(`disabled item can't be highlighted when navigating via keyUp`, () => {
const items = [
{item: 'Chess'},
{item: 'Dominion', disabled: true},
{item: 'Checkers'},
{item: 'Backgammon'},
]
const utils = renderDownshift({items, props: {initialHighlightedIndex: 2}})
const {input, arrowUpInput, enterOnInput} = utils
// ↑
arrowUpInput()
// ENTER to select
enterOnInput()
expect(input).toHaveValue('Chess')
})
test(`disabled item can't be highlighted and it may wrap when navigating via keyUp`, () => {
const items = [
{item: 'Chess', disabled: true},
{item: 'Dominion', disabled: true},
{item: 'Checkers'},
{item: 'Backgammon'},
]
const utils = renderDownshift({items, props: {initialHighlightedIndex: 2}})
const {input, arrowUpInput, enterOnInput} = utils
// ↑
arrowUpInput()
// ENTER to select
enterOnInput()
expect(input).toHaveValue('Backgammon')
})
test(`disabled item can't be highlighted when navigating via end`, () => {
const items = [
{item: 'Backgammon'},
{item: 'Chess'},
{item: 'Dominion', disabled: true},
{item: 'Checkers', disabled: true},
]
const utils = renderDownshift({items})
const {input, endOnInput, enterOnInput} = utils
// end
endOnInput()
// ENTER to select
enterOnInput()
expect(input).toHaveValue('Chess')
})
test(`disabled item can't be highlighted when navigating via home`, () => {
const items = [
{item: 'Chess', disabled: true},
{item: 'Dominion', disabled: true},
{item: 'Checkers'},
{item: 'Backgammon'},
]
const utils = renderDownshift({items})
const {input, homeOnInput, enterOnInput} = utils
// home
homeOnInput()
// ENTER to select
enterOnInput()
expect(input).toHaveValue('Checkers')
})
test(`highlight wrapping works with disabled items upwards`, () => {
const items = [
{item: 'Chess', disabled: true},
{item: 'Dominion'},
{item: 'Checkers'},
]
const utils = renderDownshift({items, props: {initialHighlightedIndex: 1}})
const {input, arrowUpInput, enterOnInput} = utils
// ↑
arrowUpInput()
// ENTER to select
enterOnInput()
expect(input).toHaveValue('Checkers')
})
test(`highlight wrapping works with disabled items downwards`, () => {
const items = [
{item: 'Chess'},
{item: 'Dominion'},
{item: 'Checkers', disabled: true},
]
const utils = renderDownshift({items, props: {initialHighlightedIndex: 1}})
const {input, arrowDownInput, enterOnInput} = utils
// ↓
arrowDownInput()
// ENTER to select
enterOnInput()
expect(input).toHaveValue('Chess')
})
test('cannot check if node is disabled without environment', () => {
const items = [
{item: 'Chess', disabled: true},
]
const utils = renderDownshift({items, props: {initialHighlightedIndex: 1, environment: null}})
const {input, arrowDownInput, enterOnInput} = utils
// ↓
arrowDownInput()
// ENTER to select
enterOnInput()
expect(input).toHaveValue('Chess')
})
function renderDownshift({
items = [{item: 'Chess'}, {item: 'Dominion'}, {item: 'Checkers'}],
props,
} = {}) {
const childrenSpy = jest.fn(({getItemProps, getInputProps}) => (
<div>
<input {...getInputProps({'data-testid': 'input'})} />
{items.map((item, index) => (
<div
{...getItemProps({
...item,
index,
})}
key={index}
data-testid={`item-${index}`}
>
<button data-testid={`item-${index}-button`}>{item.item}</button>
</div>
))}
</div>
))
const utils = render(
<Downshift
isOpen={true}
onChange={() => {}}
children={childrenSpy}
{...props}
/>,
)
const input = screen.queryByTestId('input')
return {
...utils,
childrenSpy,
input,
homeOnInput: extraEventProps =>
fireEvent.keyDown(input, {key: 'Home', ...extraEventProps}),
endOnInput: extraEventProps =>
fireEvent.keyDown(input, {key: 'End', ...extraEventProps}),
arrowDownInput: extraEventProps =>
fireEvent.keyDown(input, {key: 'ArrowDown', ...extraEventProps}),
arrowUpInput: extraEventProps =>
fireEvent.keyDown(input, {key: 'ArrowUp', ...extraEventProps}),
enterOnInput: extraEventProps =>
fireEvent.keyDown(input, {key: 'Enter', ...extraEventProps}),
changeInputValue: (value, extraEventProps) => {
fireEvent.change(input, {target: {value}, ...extraEventProps})
},
}
}
function setupWithDownshiftController() {
let renderArg
render(
<Downshift>
{controllerArg => {
renderArg = controllerArg
return null
}}
</Downshift>,
)
return renderArg
}