|
| 1 | +import userEvent from '@testing-library/user-event'; |
| 2 | + |
| 3 | +import { createAutocomplete, InternalAutocompleteSource } from '..'; |
| 4 | +import { createPlayground, createSource, defer } from '../../../../test/utils'; |
| 5 | + |
| 6 | +type DebouncedSource = InternalAutocompleteSource<{ label: string }>; |
| 7 | + |
| 8 | +const debounced = debouncePromise<DebouncedSource[][], DebouncedSource[]>( |
| 9 | + (items) => Promise.resolve(items), |
| 10 | + 100 |
| 11 | +); |
| 12 | + |
| 13 | +describe('debouncing', () => { |
| 14 | + test('only submits the final query', async () => { |
| 15 | + const onStateChange = jest.fn(); |
| 16 | + const getItems = jest.fn(({ query }) => [{ label: query }]); |
| 17 | + const { inputElement } = createPlayground(createAutocomplete, { |
| 18 | + onStateChange, |
| 19 | + getSources: () => debounced([createSource({ getItems })]), |
| 20 | + }); |
| 21 | + |
| 22 | + userEvent.type(inputElement, 'abc'); |
| 23 | + |
| 24 | + await defer(() => {}, 300); |
| 25 | + |
| 26 | + expect(getItems).toHaveBeenCalledTimes(1); |
| 27 | + expect(onStateChange).toHaveBeenLastCalledWith( |
| 28 | + expect.objectContaining({ |
| 29 | + state: expect.objectContaining({ |
| 30 | + status: 'idle', |
| 31 | + isOpen: true, |
| 32 | + collections: expect.arrayContaining([ |
| 33 | + expect.objectContaining({ |
| 34 | + items: [{ __autocomplete_id: 0, label: 'abc' }], |
| 35 | + }), |
| 36 | + ]), |
| 37 | + }), |
| 38 | + }) |
| 39 | + ); |
| 40 | + }); |
| 41 | + test('triggers subsequent queries after reopening the panel', async () => { |
| 42 | + const onStateChange = jest.fn(); |
| 43 | + const getItems = jest.fn(({ query }) => [{ label: query }]); |
| 44 | + const { inputElement } = createPlayground(createAutocomplete, { |
| 45 | + onStateChange, |
| 46 | + getSources: () => debounced([createSource({ getItems })]), |
| 47 | + }); |
| 48 | + |
| 49 | + userEvent.type(inputElement, 'abc'); |
| 50 | + |
| 51 | + await defer(() => {}, 300); |
| 52 | + |
| 53 | + expect(onStateChange).toHaveBeenLastCalledWith( |
| 54 | + expect.objectContaining({ |
| 55 | + state: expect.objectContaining({ |
| 56 | + collections: expect.arrayContaining([ |
| 57 | + expect.objectContaining({ |
| 58 | + items: [{ __autocomplete_id: 0, label: 'abc' }], |
| 59 | + }), |
| 60 | + ]), |
| 61 | + status: 'idle', |
| 62 | + isOpen: true, |
| 63 | + }), |
| 64 | + }) |
| 65 | + ); |
| 66 | + |
| 67 | + userEvent.type(inputElement, '{esc}'); |
| 68 | + |
| 69 | + expect(onStateChange).toHaveBeenLastCalledWith( |
| 70 | + expect.objectContaining({ |
| 71 | + state: expect.objectContaining({ |
| 72 | + status: 'idle', |
| 73 | + isOpen: false, |
| 74 | + }), |
| 75 | + }) |
| 76 | + ); |
| 77 | + |
| 78 | + userEvent.type(inputElement, 'def'); |
| 79 | + |
| 80 | + await defer(() => {}, 300); |
| 81 | + |
| 82 | + expect(onStateChange).toHaveBeenLastCalledWith( |
| 83 | + expect.objectContaining({ |
| 84 | + state: expect.objectContaining({ |
| 85 | + collections: expect.arrayContaining([ |
| 86 | + expect.objectContaining({ |
| 87 | + items: [{ __autocomplete_id: 0, label: 'abcdef' }], |
| 88 | + }), |
| 89 | + ]), |
| 90 | + status: 'idle', |
| 91 | + isOpen: true, |
| 92 | + }), |
| 93 | + }) |
| 94 | + ); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +function debouncePromise<TParams extends unknown[], TResponse>( |
| 99 | + fn: (...params: TParams) => Promise<TResponse>, |
| 100 | + time: number |
| 101 | +) { |
| 102 | + let timerId: ReturnType<typeof setTimeout> | undefined = undefined; |
| 103 | + |
| 104 | + return function (...args: TParams) { |
| 105 | + if (timerId) { |
| 106 | + clearTimeout(timerId); |
| 107 | + } |
| 108 | + |
| 109 | + return new Promise<TResponse>((resolve) => { |
| 110 | + timerId = setTimeout(() => resolve(fn(...args)), time); |
| 111 | + }); |
| 112 | + }; |
| 113 | +} |
0 commit comments