-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle late resolving promises with promise cancelation (#864)
Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com> Co-authored-by: Haroen Viaene <hello@haroen.me>
- Loading branch information
1 parent
5c29a20
commit 9640c2d
Showing
15 changed files
with
927 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/autocomplete-core/src/__tests__/debouncing.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { noop } from '@algolia/autocomplete-shared'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import { createAutocomplete, InternalAutocompleteSource } from '..'; | ||
import { createPlayground, createSource, defer } from '../../../../test/utils'; | ||
|
||
type Source = InternalAutocompleteSource<{ label: string }>; | ||
|
||
const delay = 10; | ||
|
||
const debounced = debouncePromise<Source[][], Source[]>( | ||
(items) => Promise.resolve(items), | ||
delay | ||
); | ||
|
||
describe('debouncing', () => { | ||
test('only submits the final query', async () => { | ||
const onStateChange = jest.fn(); | ||
const getItems = jest.fn(({ query }) => [{ label: query }]); | ||
const { inputElement } = createPlayground(createAutocomplete, { | ||
onStateChange, | ||
getSources: () => debounced([createSource({ getItems })]), | ||
}); | ||
|
||
userEvent.type(inputElement, 'abc'); | ||
|
||
await defer(noop, delay); | ||
|
||
expect(getItems).toHaveBeenCalledTimes(1); | ||
expect(onStateChange).toHaveBeenLastCalledWith( | ||
expect.objectContaining({ | ||
state: expect.objectContaining({ | ||
status: 'idle', | ||
isOpen: true, | ||
collections: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
items: [{ __autocomplete_id: 0, label: 'abc' }], | ||
}), | ||
]), | ||
}), | ||
}) | ||
); | ||
}); | ||
|
||
test('triggers subsequent queries after reopening the panel', async () => { | ||
const onStateChange = jest.fn(); | ||
const getItems = jest.fn(({ query }) => [{ label: query }]); | ||
const { inputElement } = createPlayground(createAutocomplete, { | ||
onStateChange, | ||
getSources: () => debounced([createSource({ getItems })]), | ||
}); | ||
|
||
userEvent.type(inputElement, 'abc{esc}'); | ||
|
||
expect(onStateChange).toHaveBeenLastCalledWith( | ||
expect.objectContaining({ | ||
state: expect.objectContaining({ | ||
status: 'idle', | ||
isOpen: false, | ||
}), | ||
}) | ||
); | ||
|
||
userEvent.type(inputElement, 'def'); | ||
|
||
await defer(noop, delay); | ||
|
||
expect(onStateChange).toHaveBeenLastCalledWith( | ||
expect.objectContaining({ | ||
state: expect.objectContaining({ | ||
collections: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
items: [{ __autocomplete_id: 0, label: 'abcdef' }], | ||
}), | ||
]), | ||
status: 'idle', | ||
isOpen: true, | ||
}), | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
function debouncePromise<TParams extends unknown[], TResponse>( | ||
fn: (...params: TParams) => Promise<TResponse>, | ||
time: number | ||
) { | ||
let timerId: ReturnType<typeof setTimeout> | undefined = undefined; | ||
|
||
return function (...args: TParams) { | ||
if (timerId) { | ||
clearTimeout(timerId); | ||
} | ||
|
||
return new Promise<TResponse>((resolve) => { | ||
timerId = setTimeout(() => resolve(fn(...args)), time); | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.