Skip to content

Commit

Permalink
chore: don't allow index widget when in composition mode (#6459)
Browse files Browse the repository at this point in the history
feat: forbid adding an index widget when on a composition-based implementation
  • Loading branch information
e-krebs authored Dec 4, 2024
1 parent 9723a37 commit 16a5de8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/instantsearch.js/src/lib/InstantSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,14 @@ See documentation: ${createDocumentationLink({
);
}

if (this.compositionID && widgets.some(isIndexWidget)) {
throw new Error(
withUsage(
'The `index` widget cannot be used with a composition-based InstantSearch implementation.'
)
);
}

this.mainIndex.addWidgets(widgets);

return this;
Expand Down
25 changes: 25 additions & 0 deletions packages/instantsearch.js/src/lib/__tests__/InstantSearch-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,31 @@ See: https://www.algolia.com/doc/guides/building-search-ui/going-further/backend
'[InstantSearch.js]: No indexName provided, nor an explicit index widget in the widgets tree. This is required to be able to display results.'
);
});

it('throws if compositionID & index widget is provided', () => {
expect(() => {
const search = new InstantSearch({
searchClient: createSearchClient(),
compositionID: 'my-composition',
});

search.addWidgets([index({ indexName: 'indexName' })]);
}).toThrowErrorMatchingInlineSnapshot(`
"The \`index\` widget cannot be used with a composition-based InstantSearch implementation.
See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsearch/js/"
`);
});

it('does not throw if compositionID is not provided while index widget is provided', () => {
expect(() => {
const search = new InstantSearch({
searchClient: createSearchClient(),
});

search.addWidgets([index({ indexName: 'indexName' })]);
}).not.toThrow();
});
});

it('throws if insightsClient is not a function', () => {
Expand Down

0 comments on commit 16a5de8

Please sign in to comment.