Skip to content

Commit

Permalink
fix(core): don't open panel with openOnFocus without items
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Feb 5, 2021
1 parent 11e5667 commit fde8b8a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
46 changes: 44 additions & 2 deletions packages/autocomplete-core/src/__tests__/openOnFocus.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { createSource, createPlayground } from '../../../../test/utils';
import userEvent from '@testing-library/user-event';

import {
createSource,
createPlayground,
runAllMicroTasks,
} from '../../../../test/utils';
import { createAutocomplete } from '../createAutocomplete';
import { BaseItem } from '../types/AutocompleteApi';
import { AutocompleteOptions } from '../types/AutocompleteOptions';

describe('openOnFocus', () => {
function setupTest(props) {
function setupTest<TItem extends BaseItem>(
props: Partial<AutocompleteOptions<TItem>>
) {
return createPlayground(createAutocomplete, {
openOnFocus: true,
defaultActiveItemId: 0,
Expand Down Expand Up @@ -96,4 +106,36 @@ describe('openOnFocus', () => {
})
);
});

test('does not open panel after getSources() without items', async () => {
const onStateChange = jest.fn();
const { inputElement } = setupTest({
onStateChange,
getSources() {
return [];
},
});

inputElement.focus();
await runAllMicroTasks();

expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({
isOpen: false,
}),
})
);

userEvent.type(inputElement, 'a{backspace}');
await runAllMicroTasks();

expect(onStateChange).toHaveBeenLastCalledWith(
expect.objectContaining({
state: expect.objectContaining({
isOpen: false,
}),
})
);
});
});
4 changes: 3 additions & 1 deletion packages/autocomplete-core/src/onInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ export function onInput<TItem extends BaseItem>({
setCollections(collections as any);
setIsOpen(
nextState.isOpen ??
((!query && props.openOnFocus) ||
((props.openOnFocus &&
!query &&
props.shouldPanelShow({ state: store.getState() })) ||
props.shouldPanelShow({ state: store.getState() }))
);

Expand Down

0 comments on commit fde8b8a

Please sign in to comment.