Skip to content

Commit

Permalink
fix(core): trigger invariant when user doesn't return anything from `…
Browse files Browse the repository at this point in the history
…getItems` (#607)

* fix: trigger invariant when user doesn't return anything from getItems

* tests: test invariant when not returning from getItems

* fix: remove pre-resolve invariant

* feat: check that every item is truthy in post-resolve invariant

* fix: rmeove unused dependency

* feat: add sourceId in error message

* feat: use second invariant for undefined items

* feat: add link to docs in invariant
  • Loading branch information
sarahdayan authored Jun 16, 2021
1 parent 760f8e7 commit e019b4d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
22 changes: 22 additions & 0 deletions packages/autocomplete-core/src/__tests__/getSources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
runAllMicroTasks,
} from '../../../../test/utils';
import { createAutocomplete } from '../createAutocomplete';
import * as handlers from '../onInput';

describe('getSources', () => {
test('gets calls on input', () => {
Expand Down Expand Up @@ -110,4 +111,25 @@ describe('getSources', () => {

expect(onStateChange.mock.calls.pop()[0].state.collections).toHaveLength(2);
});

test('with nothing returned from getItems throws', async () => {
const spy = jest.spyOn(handlers, 'onInput');

const { inputElement } = createPlayground(createAutocomplete, {
getSources() {
return [createSource({ sourceId: 'source1', getItems: () => {} })];
},
});

await expect(() => {
inputElement.focus();
userEvent.type(inputElement, 'a');

return spy.mock.results[0].value;
}).rejects.toThrow(
'[Autocomplete] The `getItems` function from source "source1" must return an array of items but returned undefined.\n\nDid you forget to return items?\n\nSee: https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/sources/#param-getitems'
);

spy.mockRestore();
});
});
23 changes: 20 additions & 3 deletions packages/autocomplete-core/src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function isDescription<TItem extends BaseItem>(
function isRequesterDescription<TItem extends BaseItem>(
description: TItem[] | TItem[][] | RequesterDescription<TItem>
): description is RequesterDescription<TItem> {
return Boolean((description as RequesterDescription<TItem>).execute);
return Boolean((description as RequesterDescription<TItem>)?.execute);
}

type PackedDescription<TItem extends BaseItem> = {
Expand Down Expand Up @@ -172,9 +172,26 @@ export function postResolve<TItem extends BaseItem>(

invariant(
Array.isArray(items),
`The \`getItems\` function must return an array of items but returned type ${JSON.stringify(
`The \`getItems\` function from source "${
source.sourceId
}" must return an array of items but returned type ${JSON.stringify(
typeof items
)}:\n\n${JSON.stringify(items, null, 2)}`
)}:\n\n${JSON.stringify(items, null, 2)}.
See: https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/sources/#param-getitems`
);

invariant(
(items as Array<typeof items>).every(Boolean),
`The \`getItems\` function from source "${
source.sourceId
}" must return an array of items but returned ${JSON.stringify(
undefined
)}.
Did you forget to return items?
See: https://www.algolia.com/doc/ui-libraries/autocomplete/core-concepts/sources/#param-getitems`
);

return {
Expand Down

0 comments on commit e019b4d

Please sign in to comment.