Skip to content

Commit

Permalink
fix(js): render noResults template when openOnFocus is true (#573)
Browse files Browse the repository at this point in the history
* fix(js): render `noResults` template when `openOnFocus` is `true`
  • Loading branch information
shortcuts authored May 6, 2021
1 parent 7d3402e commit f2154c8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
46 changes: 43 additions & 3 deletions packages/autocomplete-js/src/__tests__/autocomplete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ describe('autocomplete-js', () => {
});
});

test("doesn't render noResults template on no query when openOnFocus is false", async () => {
test("doesn't render noResults template on empty query when openOnFocus is false", async () => {
const container = document.createElement('div');
const panelContainer = document.createElement('div');

Expand Down Expand Up @@ -309,7 +309,7 @@ describe('autocomplete-js', () => {

const input = container.querySelector<HTMLInputElement>('.aa-Input');

fireEvent.input(input, { target: { value: '' } });
fireEvent.focus(input);

await waitFor(() => {
expect(
Expand All @@ -318,7 +318,47 @@ describe('autocomplete-js', () => {
});
});

test('render noResults template on query when openOnFocus is false', async () => {
test('renders noResults template on empty query when openOnFocus is true', async () => {
const container = document.createElement('div');
const panelContainer = document.createElement('div');

document.body.appendChild(panelContainer);
autocomplete<{ label: string }>({
container,
panelContainer,
openOnFocus: true,
getSources() {
return [
{
sourceId: 'testSource',
getItems() {
return [];
},
templates: {
item({ item }) {
return item.label;
},
noResults() {
return 'No results template';
},
},
},
];
},
});

const input = container.querySelector<HTMLInputElement>('.aa-Input');

fireEvent.focus(input);

await waitFor(() => {
expect(
panelContainer.querySelector<HTMLElement>('.aa-Panel')
).toHaveTextContent('No results template');
});
});

test('renders noResults template on query when openOnFocus is false', async () => {
const container = document.createElement('div');
const panelContainer = document.createElement('div');

Expand Down
2 changes: 1 addition & 1 deletion packages/autocomplete-js/src/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function renderPanel<TItem extends BaseItem>(
</div>
)}

{items.length === 0 && source.templates.noResults && state.query ? (
{source.templates.noResults && items.length === 0 ? (
<div className={classNames.sourceNoResults}>
{source.templates.noResults({
components,
Expand Down

0 comments on commit f2154c8

Please sign in to comment.