Skip to content

Commit b74f8e3

Browse files
feat(index): accept indexId (#4070)
* feat(index): accept indexId The indexid is used in the already existing `getIndexId` function, so all that was needed to be done there was to add a new test to ensure the usage is correct. There can still be conflicts within a scope, but that would be if an indexId is shared IFW-930 * simpler logic 🤔 * chore: typesafe check * chore: wording in test Co-Authored-By: François Chalifour <francoischalifour@users.noreply.github.com>
1 parent c99f822 commit b74f8e3

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

src/widgets/index/__tests__/index-test.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,28 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
13351335
});
13361336
});
13371337

1338+
it('uses `indexId` for scope key', () => {
1339+
const instance = index({ indexName: 'indexName', indexId: 'indexId' });
1340+
const widgets = [createSearchBox(), createPagination()];
1341+
1342+
instance.addWidgets(widgets);
1343+
1344+
instance.init(createInitOptions());
1345+
1346+
// Simulate a state change
1347+
instance
1348+
.getHelper()!
1349+
.setQueryParameter('query', 'Apple')
1350+
.setQueryParameter('page', 5);
1351+
1352+
expect(instance.getWidgetState({})).toEqual({
1353+
indexId: {
1354+
query: 'Apple',
1355+
page: 5,
1356+
},
1357+
});
1358+
});
1359+
13381360
it('does not update the local `uiState` on state changes in `init`', () => {
13391361
const instance = index({ indexName: 'indexName' });
13401362
const widgets = [
@@ -1557,20 +1579,20 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
15571579
const level0 = index({ indexName: 'level0IndexName' });
15581580
const level1 = index({ indexName: 'level1IndexName' });
15591581
const level2 = index({ indexName: 'level2IndexName' });
1560-
const level21 = index({ indexName: 'level21IndeName' });
1582+
const level21 = index({ indexName: 'level21IndexName' });
15611583
const level22 = index({ indexName: 'level22IndexName' });
15621584
const level221 = index({ indexName: 'level221IndexName' });
15631585
const level3 = index({ indexName: 'level3IndexName' });
15641586
const searchBoxLevel0 = createSearchBox();
15651587
const searchBoxLevel1 = createSearchBox();
1566-
const seachBoxLevel21 = createSearchBox();
1588+
const searchBoxLevel21 = createSearchBox();
15671589

15681590
level0.addWidgets([
15691591
searchBoxLevel0,
15701592
level1.addWidgets([searchBoxLevel1]),
15711593
level2.addWidgets([
15721594
createSearchBox(),
1573-
level21.addWidgets([seachBoxLevel21]),
1595+
level21.addWidgets([searchBoxLevel21]),
15741596
level22.addWidgets([
15751597
createSearchBox(),
15761598
level221.addWidgets([createSearchBox()]),
@@ -1609,7 +1631,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
16091631
helper: level2.getHelper(),
16101632
},
16111633
{
1612-
indexId: 'level21IndeName',
1634+
indexId: 'level21IndexName',
16131635
results: expect.any(algoliasearchHelper.SearchResults),
16141636
helper: level21.getHelper(),
16151637
},
@@ -1633,14 +1655,14 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
16331655
);
16341656

16351657
// Sibling index
1636-
expect(seachBoxLevel21.render).toHaveBeenCalledTimes(1);
1637-
expect(seachBoxLevel21.render).toHaveBeenCalledWith(
1658+
expect(searchBoxLevel21.render).toHaveBeenCalledTimes(1);
1659+
expect(searchBoxLevel21.render).toHaveBeenCalledWith(
16381660
expect.objectContaining({
16391661
scopedResults: [
16401662
// Root index
16411663
{
1642-
indexId: 'level21IndeName',
1643-
results: (seachBoxLevel21.render as jest.Mock).mock.calls[0][0]
1664+
indexId: 'level21IndexName',
1665+
results: (searchBoxLevel21.render as jest.Mock).mock.calls[0][0]
16441666
.results,
16451667
helper: level21.getHelper(),
16461668
},
@@ -1684,7 +1706,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
16841706
helper: level2.getHelper(),
16851707
},
16861708
{
1687-
indexId: 'level21IndeName',
1709+
indexId: 'level21IndexName',
16881710
results: expect.any(algoliasearchHelper.SearchResults),
16891711
helper: level21.getHelper(),
16901712
},
@@ -1793,14 +1815,14 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
17931815
// Save the Helper to be able to simulate a change
17941816
const helper = instance.getHelper()!;
17951817

1796-
// Simuate a state change
1818+
// Simulate a state change
17971819
helper.setQueryParameter('query', 'Apple iPhone');
17981820

17991821
expect(searchBox.getWidgetState).toHaveBeenCalledTimes(1);
18001822

18011823
instance.dispose(createDisposeOptions());
18021824

1803-
// Simuate a state change
1825+
// Simulate a state change
18041826
helper.setQueryParameter('query', 'Apple iPhone 5S');
18051827

18061828
expect(searchBox.getWidgetState).toHaveBeenCalledTimes(1);

src/widgets/index/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const withUsage = createDocumentationMessageGenerator({
2727

2828
type IndexProps = {
2929
indexName: string;
30+
indexId?: string;
3031
};
3132

3233
type IndexInitOptions = Pick<InitOptions, 'instantSearchInstance' | 'parent'>;
@@ -105,8 +106,12 @@ function resolveScopedResultsFromIndex(widget: Index): ScopedResult[] {
105106
return resolveScopedResultsFromWidgets(widgetSiblings);
106107
}
107108

108-
const index = (props: IndexProps): Index => {
109-
const { indexName = null } = props || {};
109+
const index = (props?: IndexProps): Index => {
110+
if (props === undefined || props.indexName === undefined) {
111+
throw new Error(withUsage('The `indexName` option is required.'));
112+
}
113+
114+
const { indexName, indexId = indexName } = props;
110115

111116
let localWidgets: Widget[] = [];
112117
let localUiState: UiState = {};
@@ -115,15 +120,11 @@ const index = (props: IndexProps): Index => {
115120
let helper: Helper | null = null;
116121
let derivedHelper: DerivedHelper | null = null;
117122

118-
if (indexName === null) {
119-
throw new Error(withUsage('The `indexName` option is required.'));
120-
}
121-
122123
return {
123124
$$type: 'ais.index',
124125

125126
getIndexId() {
126-
return indexName;
127+
return indexId;
127128
},
128129

129130
getHelper() {

0 commit comments

Comments
 (0)