-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuseSuggestions.test.tsx
212 lines (171 loc) · 5.98 KB
/
useSuggestions.test.tsx
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
import { renderHook, screen } from '@testing-library/react'
import { useSuggestions } from '../useSuggestions'
describe('useSuggestions', () => {
let input: HTMLInputElement
let list: HTMLUListElement
let inputRef: React.RefObject<HTMLInputElement>
let listRef: React.RefObject<HTMLUListElement>
let target: HTMLElement
const results = [
<a key="1" href="https://twitter.com">
Twitter
</a>,
<a key="2" href="https://facebook.com">
Facebook
</a>,
<a key="3" href="https://reddit.com">
Reddit
</a>,
]
beforeEach(() => {
target = document.createElement('div')
document.body.appendChild(target)
input = document.createElement('input')
input.type = 'search'
target.appendChild(input)
list = document.createElement('ul')
target.appendChild(list)
;[
{ label: 'Reddit', url: 'https://reddit.com' },
{ label: 'Facebook', url: 'https://facebook.com' },
{ label: 'Twitter', url: 'https://twitter.com' },
].forEach(link => {
const item = document.createElement('li')
const anchor = document.createElement('a')
anchor.setAttribute('href', link.url)
anchor.appendChild(document.createTextNode(link.label))
item.appendChild(anchor)
list.appendChild(item)
})
inputRef = { current: input } as React.RefObject<HTMLInputElement>
listRef = { current: list } as React.RefObject<HTMLUListElement>
})
afterEach(() => {
target.removeChild(input)
target.removeChild(list)
})
it('sets the tab index for each list item', () => {
renderHook(() => useSuggestions(inputRef, listRef, results))
screen.getAllByRole('link').forEach(linkItem => {
expect(linkItem).toHaveAttribute('tabIndex', '0')
})
})
it('selects the first initial result', () => {
const { result } = renderHook(() =>
useSuggestions(inputRef, listRef, results)
)
result.current.selectInitialResult({
currentTarget: { value: ' ' },
key: 'ArrowDown',
preventDefault: jest.fn(),
} as unknown as React.KeyboardEvent<HTMLInputElement>)
expect(screen.getByRole('link', { name: 'Reddit' })).toHaveFocus()
})
it('selects the last initial result', () => {
const { result } = renderHook(() =>
useSuggestions(inputRef, listRef, results)
)
result.current.selectInitialResult({
currentTarget: { value: ' ' },
key: 'ArrowUp',
preventDefault: jest.fn(),
} as unknown as React.KeyboardEvent<HTMLInputElement>)
expect(screen.getByRole('link', { name: 'Twitter' })).toHaveFocus()
})
it('sets focus on the hovered element', () => {
const { result } = renderHook(() =>
useSuggestions(inputRef, listRef, results)
)
result.current.onResultsHover({
currentTarget: {
firstChild: screen.getByRole('link', { name: 'Facebook' }),
},
} as unknown as React.MouseEvent<HTMLLIElement, MouseEvent>)
expect(screen.getByRole('link', { name: 'Facebook' })).toHaveFocus()
result.current.onResultsHover({
currentTarget: {
firstChild: screen.getByRole('link', { name: 'Twitter' }),
},
} as unknown as React.MouseEvent<HTMLLIElement, MouseEvent>)
expect(screen.getByRole('link', { name: 'Twitter' })).toHaveFocus()
})
it('navigates through search suggestions', () => {
const { result } = renderHook(() =>
useSuggestions(inputRef, listRef, results)
)
const triggerEvent = (next: string, previous: string, key: string) =>
({
currentTarget: {
value: 'r',
nextSibling: {
firstChild: screen.getByRole('link', { name: next }),
focus: jest.fn(),
},
previousSibling: {
firstChild: screen.getByRole('link', { name: previous }),
focus: jest.fn(),
},
},
key,
preventDefault: jest.fn(),
} as unknown as React.KeyboardEvent<HTMLLIElement>)
result.current.onResultsKeyDown(
triggerEvent('Facebook', 'Reddit', 'ArrowDown')
)
expect(screen.getByRole('link', { name: 'Facebook' })).toHaveFocus()
result.current.onResultsKeyDown(triggerEvent('Twitter', 'Facebook', 'Tab'))
expect(screen.getByRole('link', { name: 'Twitter' })).toHaveFocus()
result.current.onResultsKeyDown(
triggerEvent('Twitter', 'Facebook', 'ArrowUp')
)
expect(screen.getByRole('link', { name: 'Facebook' })).toHaveFocus()
})
it('navigates to first result if nextSibling unavailable', () => {
const { result } = renderHook(() =>
useSuggestions(inputRef, listRef, results)
)
result.current.onResultsKeyDown({
currentTarget: {
value: 'r',
},
key: 'ArrowDown',
preventDefault: jest.fn(),
} as unknown as React.KeyboardEvent<HTMLLIElement>)
expect(screen.getByRole('link', { name: 'Reddit' })).toHaveFocus()
})
it('navigates to last result if previousSibling unavailable', () => {
const { result } = renderHook(() =>
useSuggestions(inputRef, listRef, results)
)
result.current.onResultsKeyDown({
currentTarget: {
value: 'r',
},
key: 'ArrowUp',
preventDefault: jest.fn(),
} as unknown as React.KeyboardEvent<HTMLLIElement>)
expect(screen.getByRole('link', { name: 'Twitter' })).toHaveFocus()
})
it('sets focus back to input element if arrow keys or tab not pressed', () => {
const { result } = renderHook(() =>
useSuggestions(inputRef, listRef, results)
)
result.current.onResultsKeyDown({
currentTarget: {
value: 'r',
},
key: 'e',
preventDefault: jest.fn(),
} as unknown as React.KeyboardEvent<HTMLLIElement>)
expect(screen.getByRole('searchbox')).toHaveFocus()
})
it('sets showSuggestions to true if results exist and input not empty', () => {
if (inputRef.current) {
inputRef.current.value = 'r'
}
const { result } = renderHook(() =>
useSuggestions(inputRef, listRef, results)
)
expect(result.current.showSuggestions).toBeTruthy()
})
})