Skip to content

Commit

Permalink
fix(js): render noResults template when openOnFocus is true
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed May 5, 2021
1 parent 26763c8 commit dbe9a13
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
1 change: 0 additions & 1 deletion examples/starter/app.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { autocomplete } from '@algolia/autocomplete-js';

import '@algolia/autocomplete-theme-classic';

type AutocompleteItem = {
Expand Down
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
4 changes: 3 additions & 1 deletion packages/autocomplete-js/src/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ export function renderPanel<TItem extends BaseItem>(
</div>
)}

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

0 comments on commit dbe9a13

Please sign in to comment.