|
1 | | -import { fireEvent } from '@testing-library/dom'; |
| 1 | +import { fireEvent, waitFor } from '@testing-library/dom'; |
2 | 2 |
|
| 3 | +import { wait } from '../../../../test/utils'; |
3 | 4 | import { autocomplete } from '../autocomplete'; |
4 | 5 |
|
5 | 6 | describe('autocomplete-js', () => { |
@@ -156,6 +157,200 @@ describe('autocomplete-js', () => { |
156 | 157 | `); |
157 | 158 | }); |
158 | 159 |
|
| 160 | + test('renders empty template on no results', async () => { |
| 161 | + const container = document.createElement('div'); |
| 162 | + const panelContainer = document.createElement('div'); |
| 163 | + |
| 164 | + document.body.appendChild(panelContainer); |
| 165 | + autocomplete<{ label: string }>({ |
| 166 | + container, |
| 167 | + panelContainer, |
| 168 | + getSources() { |
| 169 | + return [ |
| 170 | + { |
| 171 | + getItems() { |
| 172 | + return []; |
| 173 | + }, |
| 174 | + templates: { |
| 175 | + item({ item }) { |
| 176 | + return item.label; |
| 177 | + }, |
| 178 | + empty() { |
| 179 | + return 'No results template'; |
| 180 | + }, |
| 181 | + }, |
| 182 | + }, |
| 183 | + ]; |
| 184 | + }, |
| 185 | + }); |
| 186 | + |
| 187 | + const input = container.querySelector<HTMLInputElement>('.aa-Input'); |
| 188 | + |
| 189 | + fireEvent.input(input, { |
| 190 | + target: { value: 'aasdjfaisdf' }, |
| 191 | + }); |
| 192 | + input.focus(); |
| 193 | + |
| 194 | + await waitFor(() => { |
| 195 | + expect( |
| 196 | + panelContainer.querySelector<HTMLElement>('.aa-Panel') |
| 197 | + ).toBeInTheDocument(); |
| 198 | + }); |
| 199 | + |
| 200 | + expect( |
| 201 | + panelContainer.querySelector<HTMLElement>('.aa-Panel') |
| 202 | + ).toHaveTextContent('No results template'); |
| 203 | + }); |
| 204 | + |
| 205 | + test('calls renderEmpty without empty template on no results', async () => { |
| 206 | + const container = document.createElement('div'); |
| 207 | + const panelContainer = document.createElement('div'); |
| 208 | + const renderEmpty = jest.fn(({ root }) => { |
| 209 | + const div = document.createElement('div'); |
| 210 | + div.innerHTML = 'No results render'; |
| 211 | + |
| 212 | + root.appendChild(div); |
| 213 | + }); |
| 214 | + |
| 215 | + document.body.appendChild(panelContainer); |
| 216 | + autocomplete<{ label: string }>({ |
| 217 | + container, |
| 218 | + panelContainer, |
| 219 | + getSources() { |
| 220 | + return [ |
| 221 | + { |
| 222 | + getItems() { |
| 223 | + return []; |
| 224 | + }, |
| 225 | + templates: { |
| 226 | + item({ item }) { |
| 227 | + return item.label; |
| 228 | + }, |
| 229 | + }, |
| 230 | + }, |
| 231 | + ]; |
| 232 | + }, |
| 233 | + renderEmpty, |
| 234 | + }); |
| 235 | + |
| 236 | + const input = container.querySelector<HTMLInputElement>('.aa-Input'); |
| 237 | + |
| 238 | + fireEvent.input(input, { |
| 239 | + target: { value: 'aasdjfaisdf' }, |
| 240 | + }); |
| 241 | + input.focus(); |
| 242 | + |
| 243 | + await waitFor(() => { |
| 244 | + expect( |
| 245 | + panelContainer.querySelector<HTMLElement>('.aa-Panel') |
| 246 | + ).toBeInTheDocument(); |
| 247 | + }); |
| 248 | + |
| 249 | + expect(renderEmpty).toHaveBeenCalledWith({ |
| 250 | + root: expect.anything(), |
| 251 | + state: expect.anything(), |
| 252 | + sections: expect.anything(), |
| 253 | + }); |
| 254 | + |
| 255 | + expect( |
| 256 | + panelContainer.querySelector<HTMLElement>('.aa-Panel') |
| 257 | + ).toHaveTextContent('No results render'); |
| 258 | + }); |
| 259 | + |
| 260 | + test('renders empty template over renderEmpty method on no results', async () => { |
| 261 | + const container = document.createElement('div'); |
| 262 | + const panelContainer = document.createElement('div'); |
| 263 | + |
| 264 | + document.body.appendChild(panelContainer); |
| 265 | + autocomplete<{ label: string }>({ |
| 266 | + container, |
| 267 | + panelContainer, |
| 268 | + getSources() { |
| 269 | + return [ |
| 270 | + { |
| 271 | + getItems() { |
| 272 | + return []; |
| 273 | + }, |
| 274 | + templates: { |
| 275 | + item({ item }) { |
| 276 | + return item.label; |
| 277 | + }, |
| 278 | + empty() { |
| 279 | + return 'No results template'; |
| 280 | + }, |
| 281 | + }, |
| 282 | + }, |
| 283 | + ]; |
| 284 | + }, |
| 285 | + renderEmpty({ root }) { |
| 286 | + const div = document.createElement('div'); |
| 287 | + div.innerHTML = 'No results render'; |
| 288 | + |
| 289 | + root.appendChild(div); |
| 290 | + }, |
| 291 | + }); |
| 292 | + |
| 293 | + const input = container.querySelector<HTMLInputElement>('.aa-Input'); |
| 294 | + |
| 295 | + fireEvent.input(input, { |
| 296 | + target: { value: 'aasdjfaisdf' }, |
| 297 | + }); |
| 298 | + input.focus(); |
| 299 | + |
| 300 | + await waitFor(() => { |
| 301 | + expect( |
| 302 | + panelContainer.querySelector<HTMLElement>('.aa-Panel') |
| 303 | + ).toBeInTheDocument(); |
| 304 | + }); |
| 305 | + |
| 306 | + expect( |
| 307 | + panelContainer.querySelector<HTMLElement>('.aa-Panel') |
| 308 | + ).toHaveTextContent('No results template'); |
| 309 | + }); |
| 310 | + |
| 311 | + test('allows user-provided shouldPanelShow', async () => { |
| 312 | + const container = document.createElement('div'); |
| 313 | + const panelContainer = document.createElement('div'); |
| 314 | + |
| 315 | + document.body.appendChild(panelContainer); |
| 316 | + autocomplete<{ label: string }>({ |
| 317 | + container, |
| 318 | + panelContainer, |
| 319 | + shouldPanelShow: () => false, |
| 320 | + getSources() { |
| 321 | + return [ |
| 322 | + { |
| 323 | + getItems() { |
| 324 | + return [ |
| 325 | + { label: 'Item 1' }, |
| 326 | + { label: 'Item 2' }, |
| 327 | + { label: 'Item 3' }, |
| 328 | + ]; |
| 329 | + }, |
| 330 | + templates: { |
| 331 | + item({ item }) { |
| 332 | + return item.label; |
| 333 | + }, |
| 334 | + }, |
| 335 | + }, |
| 336 | + ]; |
| 337 | + }, |
| 338 | + }); |
| 339 | + |
| 340 | + const input = container.querySelector<HTMLInputElement>('.aa-Input'); |
| 341 | + |
| 342 | + fireEvent.input(input, { |
| 343 | + target: { value: 'aasdjfaisdf' }, |
| 344 | + }); |
| 345 | + input.focus(); |
| 346 | + |
| 347 | + await wait(50); |
| 348 | + |
| 349 | + expect( |
| 350 | + panelContainer.querySelector<HTMLElement>('.aa-Panel') |
| 351 | + ).not.toBeInTheDocument(); |
| 352 | + }); |
| 353 | + |
159 | 354 | test('renders with autoFocus', () => { |
160 | 355 | const container = document.createElement('div'); |
161 | 356 | autocomplete<{ label: string }>({ |
|
0 commit comments