Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/runtime/src/experiences/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type ExperienceWidget = Widget & {
'ais.hits': SupportedWidget<Parameters<HitsWidget>[0]>;
'ais.infiniteHits': SupportedWidget<Parameters<InfiniteHitsWidget>[0]>;
'ais.pagination': SupportedWidget<Parameters<PaginationWidget>[0]>;
'ais.refinementList': SupportedWidget;
'ais.searchBox': SupportedWidget<Parameters<SearchBoxWidget>[0]>;
'ais.stats': SupportedWidget<Parameters<StatsWidget>[0]>;
} & Record<
Expand All @@ -76,6 +77,7 @@ export type ExperienceWidget = Widget & {
| 'ais.hits'
| 'ais.infiniteHits'
| 'ais.pagination'
| 'ais.refinementList'
| 'ais.searchBox'
| 'ais.stats'
| (string & {}),
Expand Down
9 changes: 9 additions & 0 deletions packages/runtime/src/experiences/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import infiniteHits from 'instantsearch.js/es/widgets/infinite-hits/infinite-hit
import { EXPERIMENTAL_autocomplete } from 'instantsearch.js/es/widgets/autocomplete/autocomplete';
import clearRefinements from 'instantsearch.js/es/widgets/clear-refinements/clear-refinements';
import pagination from 'instantsearch.js/es/widgets/pagination/pagination';
import refinementList from 'instantsearch.js/es/widgets/refinement-list/refinement-list';
import searchBox from 'instantsearch.js/es/widgets/search-box/search-box';
import stats from 'instantsearch.js/es/widgets/stats/stats';

Expand Down Expand Up @@ -177,6 +178,14 @@ export default (function experience(widgetParams: ExperienceWidgetParams) {
return parameters;
},
},
// TODO: Add support for `templates` (item, showMoreText, searchableNoResults, searchableSubmit, searchableReset, searchableLoadingIndicator)
// TODO: Add support for `transformItems` (bucket 3 function)
'ais.refinementList': {
widget: refinementList,
async transformParams(parameters) {
return parameters;
},
},
// TODO: Add support for `templates` (submit, reset, loadingIndicator)
// TODO: Add support for `queryHook` (bucket 3 — function)
'ais.searchBox': {
Expand Down
87 changes: 85 additions & 2 deletions packages/toolbar/__tests__/ai-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,12 @@ describe('describeWidgetTypes', () => {
expect(result).toContain('excludedAttributes');
});

it('excludes disabled widget types', () => {
it('includes refinementList widget type', () => {
const result = describeWidgetTypes();
expect(result).not.toContain('ais.refinementList');
expect(result).toContain('ais.refinementList');
expect(result).toContain('Refinement List');
expect(result).toContain('attribute');
expect(result).toContain('searchable');
});
});

Expand Down Expand Up @@ -844,6 +847,44 @@ describe('getTools', () => {
});
});

it('adds refinementList widget with parameters', async () => {
const experience: ExperienceApiResponse = {
blocks: [],
indexName: '',
};
const callbacks = createCallbacks(experience);
const tools = getTools(callbacks);

const result = await tools.add_widget.execute!(
{
type: 'ais.refinementList',
container: '#filters',
parameters: { attribute: 'brand', searchable: true },
},
{ toolCallId: 'tc1', messages: [] }
);

expect(result).toMatchObject({
success: true,
applied: expect.arrayContaining([
'placement',
'container',
'attribute',
'searchable',
]),
});
expect(callbacks.onParameterChange).toHaveBeenCalledWith(
[0],
'attribute',
'brand'
);
expect(callbacks.onParameterChange).toHaveBeenCalledWith(
[0],
'searchable',
true
);
});

it('computes the correct index for non-empty experiences', async () => {
const experience: ExperienceApiResponse = {
blocks: [
Expand Down Expand Up @@ -1533,6 +1574,48 @@ describe('getTools', () => {
);
});

it('applies parameter changes on refinementList', async () => {
const experience: ExperienceApiResponse = {
blocks: [
{
type: 'ais.refinementList',
parameters: {
container: '#filters',
attribute: 'brand',
searchable: false,
},
},
],
indexName: '',
};
const callbacks = createCallbacks(experience);
const tools = getTools(callbacks);

const result = await tools.edit_widget.execute!(
{
path: '0',
parameters: { attribute: 'color', showMore: true, limit: 5 },
},
{ toolCallId: 'tc1', messages: [] }
);

expect(result).toMatchObject({
success: true,
applied: expect.arrayContaining(['attribute', 'showMore', 'limit']),
});
expect(callbacks.onParameterChange).toHaveBeenCalledWith(
[0],
'attribute',
'color'
);
expect(callbacks.onParameterChange).toHaveBeenCalledWith(
[0],
'showMore',
true
);
expect(callbacks.onParameterChange).toHaveBeenCalledWith([0], 'limit', 5);
});

it('returns empty applied when all parameters are rejected', async () => {
const experience: ExperienceApiResponse = {
blocks: [
Expand Down
Loading