1
+ import React from 'react'
2
+
1
3
import { screen } from '@testing-library/react'
2
4
import { renderHook } from '@testing-library/react-hooks'
3
5
@@ -9,6 +11,17 @@ describe('useSuggestions', () => {
9
11
let inputRef : React . RefObject < HTMLInputElement >
10
12
let listRef : React . RefObject < HTMLUListElement >
11
13
let target : HTMLElement
14
+ const results = [
15
+ < a key = "1" href = "https://twitter.com" >
16
+ Twitter
17
+ </ a > ,
18
+ < a key = "2" href = "https://facebook.com" >
19
+ Facebook
20
+ </ a > ,
21
+ < a key = "3" href = "https://reddit.com" >
22
+ Reddit
23
+ </ a > ,
24
+ ]
12
25
13
26
beforeEach ( ( ) => {
14
27
target = document . createElement ( 'div' )
@@ -45,15 +58,17 @@ describe('useSuggestions', () => {
45
58
} )
46
59
47
60
it ( 'sets the tab index for each list item' , ( ) => {
48
- renderHook ( ( ) => useSuggestions ( inputRef , listRef ) )
61
+ renderHook ( ( ) => useSuggestions ( inputRef , listRef , results ) )
49
62
50
63
screen . getAllByRole ( 'link' ) . forEach ( linkItem => {
51
- expect ( linkItem ) . toHaveAttribute ( 'tabIndex' , '-1 ' )
64
+ expect ( linkItem ) . toHaveAttribute ( 'tabIndex' , '0 ' )
52
65
} )
53
66
} )
54
67
55
68
it ( 'selects the first initial result' , ( ) => {
56
- const { result } = renderHook ( ( ) => useSuggestions ( inputRef , listRef ) )
69
+ const { result } = renderHook ( ( ) =>
70
+ useSuggestions ( inputRef , listRef , results )
71
+ )
57
72
58
73
result . current . selectInitialResult ( {
59
74
currentTarget : { value : ' ' } ,
@@ -65,7 +80,9 @@ describe('useSuggestions', () => {
65
80
} )
66
81
67
82
it ( 'selects the last initial result' , ( ) => {
68
- const { result } = renderHook ( ( ) => useSuggestions ( inputRef , listRef ) )
83
+ const { result } = renderHook ( ( ) =>
84
+ useSuggestions ( inputRef , listRef , results )
85
+ )
69
86
70
87
result . current . selectInitialResult ( {
71
88
currentTarget : { value : ' ' } ,
@@ -77,7 +94,9 @@ describe('useSuggestions', () => {
77
94
} )
78
95
79
96
it ( 'sets focus on the hovered element' , ( ) => {
80
- const { result } = renderHook ( ( ) => useSuggestions ( inputRef , listRef ) )
97
+ const { result } = renderHook ( ( ) =>
98
+ useSuggestions ( inputRef , listRef , results )
99
+ )
81
100
82
101
result . current . onResultsHover ( {
83
102
currentTarget : {
@@ -97,7 +116,9 @@ describe('useSuggestions', () => {
97
116
} )
98
117
99
118
it ( 'navigates through search suggestions' , ( ) => {
100
- const { result } = renderHook ( ( ) => useSuggestions ( inputRef , listRef ) )
119
+ const { result } = renderHook ( ( ) =>
120
+ useSuggestions ( inputRef , listRef , results )
121
+ )
101
122
const triggerEvent = ( next : string , previous : string , key : string ) =>
102
123
( {
103
124
currentTarget : {
@@ -121,9 +142,7 @@ describe('useSuggestions', () => {
121
142
122
143
expect ( screen . getByRole ( 'link' , { name : 'Facebook' } ) ) . toHaveFocus ( )
123
144
124
- result . current . onResultsKeyDown (
125
- triggerEvent ( 'Twitter' , 'Facebook' , 'ArrowDown' )
126
- )
145
+ result . current . onResultsKeyDown ( triggerEvent ( 'Twitter' , 'Facebook' , 'Tab' ) )
127
146
128
147
expect ( screen . getByRole ( 'link' , { name : 'Twitter' } ) ) . toHaveFocus ( )
129
148
@@ -135,7 +154,9 @@ describe('useSuggestions', () => {
135
154
} )
136
155
137
156
it ( 'navigates to first result if nextSibling unavailable' , ( ) => {
138
- const { result } = renderHook ( ( ) => useSuggestions ( inputRef , listRef ) )
157
+ const { result } = renderHook ( ( ) =>
158
+ useSuggestions ( inputRef , listRef , results )
159
+ )
139
160
140
161
result . current . onResultsKeyDown ( {
141
162
currentTarget : {
@@ -149,7 +170,9 @@ describe('useSuggestions', () => {
149
170
} )
150
171
151
172
it ( 'navigates to last result if previousSibling unavailable' , ( ) => {
152
- const { result } = renderHook ( ( ) => useSuggestions ( inputRef , listRef ) )
173
+ const { result } = renderHook ( ( ) =>
174
+ useSuggestions ( inputRef , listRef , results )
175
+ )
153
176
154
177
result . current . onResultsKeyDown ( {
155
178
currentTarget : {
@@ -162,17 +185,31 @@ describe('useSuggestions', () => {
162
185
expect ( screen . getByRole ( 'link' , { name : 'Twitter' } ) ) . toHaveFocus ( )
163
186
} )
164
187
165
- it ( 'sets focus back to input element if arrow keys not pressed' , ( ) => {
166
- const { result } = renderHook ( ( ) => useSuggestions ( inputRef , listRef ) )
188
+ it ( 'sets focus back to input element if arrow keys or tab not pressed' , ( ) => {
189
+ const { result } = renderHook ( ( ) =>
190
+ useSuggestions ( inputRef , listRef , results )
191
+ )
167
192
168
193
result . current . onResultsKeyDown ( {
169
194
currentTarget : {
170
195
value : 'r' ,
171
196
} ,
172
- key : 'Tab ' ,
197
+ key : 'e ' ,
173
198
preventDefault : jest . fn ( ) ,
174
199
} as unknown as React . KeyboardEvent < HTMLLIElement > )
175
200
176
201
expect ( screen . getByRole ( 'searchbox' ) ) . toHaveFocus ( )
177
202
} )
203
+
204
+ it ( 'sets showSuggestions to true if results exist and input not empty' , ( ) => {
205
+ if ( inputRef . current ) {
206
+ inputRef . current . value = 'r'
207
+ }
208
+
209
+ const { result } = renderHook ( ( ) =>
210
+ useSuggestions ( inputRef , listRef , results )
211
+ )
212
+
213
+ expect ( result . current . showSuggestions ) . toBeTruthy ( )
214
+ } )
178
215
} )
0 commit comments