Skip to content

Commit c14844c

Browse files
committed
fix: avoid swallowing errors and return promise chain instead
1 parent ffd16fe commit c14844c

4 files changed

Lines changed: 15 additions & 13 deletions

File tree

packages/autocomplete-core/src/onInput.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,5 @@ export function onInput<TItem extends BaseItem>({
169169
}
170170
});
171171

172-
store.pendingRequests.add(request);
173-
174-
return request;
172+
return store.pendingRequests.add(request);
175173
}

packages/autocomplete-core/src/utils/__tests__/createCancelablePromiseList.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('createCancelablePromiseList', () => {
3131
const cancelablePromiseList = createCancelablePromiseList();
3232
const cancelablePromise = createCancelablePromise.reject();
3333

34-
cancelablePromiseList.add(cancelablePromise);
34+
cancelablePromiseList.add(cancelablePromise).catch(noop);
3535

3636
expect(cancelablePromiseList.isEmpty()).toBe(false);
3737

@@ -59,9 +59,9 @@ describe('createCancelablePromiseList', () => {
5959
const cancelablePromise2 = createCancelablePromise.reject();
6060
const cancelablePromise3 = createCancelablePromise(noop);
6161

62-
cancelablePromiseList.add(cancelablePromise1);
63-
cancelablePromiseList.add(cancelablePromise2);
64-
cancelablePromiseList.add(cancelablePromise3);
62+
cancelablePromiseList.add(cancelablePromise1).catch(noop);
63+
cancelablePromiseList.add(cancelablePromise2).catch(noop);
64+
cancelablePromiseList.add(cancelablePromise3).catch(noop);
6565

6666
expect(cancelablePromise1.isCanceled()).toBe(false);
6767
expect(cancelablePromise2.isCanceled()).toBe(false);

packages/autocomplete-core/src/utils/createCancelablePromiseList.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { noop } from '@algolia/autocomplete-shared';
2-
31
import { CancelablePromise } from '.';
42

53
export type CancelablePromiseList<TValue> = {
6-
add(cancelablePromise: CancelablePromise<TValue>): void;
4+
add(cancelablePromise: CancelablePromise<TValue>): CancelablePromise<TValue>;
75
cancelAll(): void;
86
isEmpty(): boolean;
97
};
@@ -17,7 +15,7 @@ export function createCancelablePromiseList<
1715
add(cancelablePromise) {
1816
list.push(cancelablePromise);
1917

20-
cancelablePromise.catch(noop).finally(() => {
18+
return cancelablePromise.finally(() => {
2119
list = list.filter((item) => item !== cancelablePromise);
2220
});
2321
},

packages/autocomplete-js/src/__tests__/autocomplete.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import * as autocompleteShared from '@algolia/autocomplete-shared';
22
import { fireEvent, waitFor } from '@testing-library/dom';
33

4-
import { castToJestMock, createMatchMedia } from '../../../../test/utils';
4+
import {
5+
castToJestMock,
6+
createMatchMedia,
7+
runAllMicroTasks,
8+
} from '../../../../test/utils';
59
import { autocomplete } from '../autocomplete';
610

711
jest.mock('@algolia/autocomplete-shared', () => {
@@ -524,7 +528,7 @@ describe('autocomplete-js', () => {
524528
expect(input).toHaveValue('Query');
525529
});
526530

527-
test('renders on input', () => {
531+
test('renders on input', async () => {
528532
const container = document.createElement('div');
529533
autocomplete<{ label: string }>({
530534
id: 'autocomplete',
@@ -554,6 +558,8 @@ describe('autocomplete-js', () => {
554558

555559
fireEvent.input(input, { target: { value: 'a' } });
556560

561+
await runAllMicroTasks();
562+
557563
expect(input).toHaveValue('a');
558564
});
559565
});

0 commit comments

Comments
 (0)