fix(#248): add console.error to typeahead catch block and expose setActiveIndex
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -66,4 +66,22 @@ describe('createTypeahead', () => {
|
||||
expect(fetchUrl).toHaveBeenCalledTimes(1);
|
||||
expect(fetchUrl).toHaveBeenCalledWith('foo');
|
||||
});
|
||||
|
||||
it('fetch error logs to console.error and sets results to empty', async () => {
|
||||
const fetchUrl = vi.fn().mockRejectedValue(new Error('network error'));
|
||||
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
const ta = createTypeahead({ fetchUrl, debounceMs: 0 });
|
||||
ta.setQuery('foo');
|
||||
await vi.advanceTimersByTimeAsync(0);
|
||||
expect(errorSpy).toHaveBeenCalled();
|
||||
expect(ta.results).toEqual([]);
|
||||
errorSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('setActiveIndex updates activeIndex', () => {
|
||||
const ta = createTypeahead({ fetchUrl: vi.fn().mockResolvedValue([]) });
|
||||
expect(ta.activeIndex).toBe(-1);
|
||||
ta.setActiveIndex(2);
|
||||
expect(ta.activeIndex).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -23,7 +23,8 @@ export function createTypeahead<T>(options: Options<T>) {
|
||||
loading = true;
|
||||
try {
|
||||
results = await fetchUrl(q);
|
||||
} catch {
|
||||
} catch (e) {
|
||||
console.error('typeahead fetch error', e);
|
||||
results = [];
|
||||
} finally {
|
||||
loading = false;
|
||||
@@ -36,6 +37,10 @@ export function createTypeahead<T>(options: Options<T>) {
|
||||
activeIndex = -1;
|
||||
}
|
||||
|
||||
function setActiveIndex(idx: number) {
|
||||
activeIndex = idx;
|
||||
}
|
||||
|
||||
function select(item: T) {
|
||||
onSelect?.(item);
|
||||
close();
|
||||
@@ -64,6 +69,7 @@ export function createTypeahead<T>(options: Options<T>) {
|
||||
return activeIndex;
|
||||
},
|
||||
setQuery,
|
||||
setActiveIndex,
|
||||
close,
|
||||
select,
|
||||
openWith
|
||||
|
||||
Reference in New Issue
Block a user