Skip to content

Commit

Permalink
change type in test
Browse files Browse the repository at this point in the history
  • Loading branch information
Haroenv committed Oct 23, 2020
1 parent 4b9f503 commit dc4ef68
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createAutocomplete, AutocompleteSuggestion } from '..';
import { InternalAutocompleteSource } from '../types';

function createSuggestion<TItem extends { label: string }>(
items: TItem[] = []
Expand All @@ -15,6 +16,26 @@ function createSuggestion<TItem extends { label: string }>(
};
}

interface AutocompleteMultiSuggestion<TItem> {
source: InternalAutocompleteSource<TItem>;
items: TItem[][];
}

function createMultiSuggestion<TItem extends { label: string }>(
items: TItem[][] = []
): AutocompleteMultiSuggestion<TItem> {
return {
source: {
getInputValue: ({ suggestion }) => suggestion.label,
getSuggestionUrl: () => undefined,
onHighlight: () => {},
onSelect: () => {},
getSuggestions: () => items,
},
items,
};
}

describe('createAutocomplete', () => {
test('setHighlightedIndex', () => {
const onStateChange = jest.fn();
Expand Down Expand Up @@ -95,10 +116,9 @@ describe('createAutocomplete', () => {
onStateChange,
});

// @ts-expect-error AutocompleteSuggestion has an array of items
// while it also accepts a nested array.
// There's only one type for both input and output however,
setSuggestions([createSuggestion([[{ label: 'hi' }]])]);
setSuggestions([createMultiSuggestion([[{ label: 'hi' }]])]);

expect(onStateChange).toHaveBeenCalledWith({
state: expect.objectContaining({
Expand Down

0 comments on commit dc4ef68

Please sign in to comment.