Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(index): accept indexId #4070

Merged
merged 4 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 33 additions & 11 deletions src/widgets/index/__tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,28 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
});
});

it('indexId is used for scope key', () => {
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
const instance = index({ indexName: 'indexName', indexId: 'indexId' });
const widgets = [createSearchBox(), createPagination()];

instance.addWidgets(widgets);

instance.init(createInitOptions());

// Simulate a state change
instance
.getHelper()!
.setQueryParameter('query', 'Apple')
.setQueryParameter('page', 5);

expect(instance.getWidgetState({})).toEqual({
indexId: {
query: 'Apple',
page: 5,
},
});
});

it('does not update the local `uiState` on state changes in `init`', () => {
const instance = index({ indexName: 'indexName' });
const widgets = [
Expand Down Expand Up @@ -1557,20 +1579,20 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
const level0 = index({ indexName: 'level0IndexName' });
const level1 = index({ indexName: 'level1IndexName' });
const level2 = index({ indexName: 'level2IndexName' });
const level21 = index({ indexName: 'level21IndeName' });
const level21 = index({ indexName: 'level21IndexName' });
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
const level22 = index({ indexName: 'level22IndexName' });
const level221 = index({ indexName: 'level221IndexName' });
const level3 = index({ indexName: 'level3IndexName' });
const searchBoxLevel0 = createSearchBox();
const searchBoxLevel1 = createSearchBox();
const seachBoxLevel21 = createSearchBox();
const searchBoxLevel21 = createSearchBox();

level0.addWidgets([
searchBoxLevel0,
level1.addWidgets([searchBoxLevel1]),
level2.addWidgets([
createSearchBox(),
level21.addWidgets([seachBoxLevel21]),
level21.addWidgets([searchBoxLevel21]),
level22.addWidgets([
createSearchBox(),
level221.addWidgets([createSearchBox()]),
Expand Down Expand Up @@ -1609,7 +1631,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
helper: level2.getHelper(),
},
{
indexId: 'level21IndeName',
indexId: 'level21IndexName',
results: expect.any(algoliasearchHelper.SearchResults),
helper: level21.getHelper(),
},
Expand All @@ -1633,14 +1655,14 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
);

// Sibling index
expect(seachBoxLevel21.render).toHaveBeenCalledTimes(1);
expect(seachBoxLevel21.render).toHaveBeenCalledWith(
expect(searchBoxLevel21.render).toHaveBeenCalledTimes(1);
expect(searchBoxLevel21.render).toHaveBeenCalledWith(
expect.objectContaining({
scopedResults: [
// Root index
{
indexId: 'level21IndeName',
results: (seachBoxLevel21.render as jest.Mock).mock.calls[0][0]
indexId: 'level21IndexName',
results: (searchBoxLevel21.render as jest.Mock).mock.calls[0][0]
.results,
helper: level21.getHelper(),
},
Expand Down Expand Up @@ -1684,7 +1706,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
helper: level2.getHelper(),
},
{
indexId: 'level21IndeName',
indexId: 'level21IndexName',
results: expect.any(algoliasearchHelper.SearchResults),
helper: level21.getHelper(),
},
Expand Down Expand Up @@ -1793,14 +1815,14 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
// Save the Helper to be able to simulate a change
const helper = instance.getHelper()!;

// Simuate a state change
// Simulate a state change
helper.setQueryParameter('query', 'Apple iPhone');

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

instance.dispose(createDisposeOptions());

// Simuate a state change
// Simulate a state change
helper.setQueryParameter('query', 'Apple iPhone 5S');

expect(searchBox.getWidgetState).toHaveBeenCalledTimes(1);
Expand Down
5 changes: 3 additions & 2 deletions src/widgets/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const withUsage = createDocumentationMessageGenerator({

type IndexProps = {
indexName: string;
indexId?: string;
};

type IndexInitOptions = Pick<InitOptions, 'instantSearchInstance' | 'parent'>;
Expand Down Expand Up @@ -106,7 +107,7 @@ function resolveScopedResultsFromIndex(widget: Index): ScopedResult[] {
}

const index = (props: IndexProps): Index => {
const { indexName = null } = props || {};
const { indexName = null, indexId = null } = props || {};

let localWidgets: Widget[] = [];
let localUiState: UiState = {};
Expand All @@ -123,7 +124,7 @@ const index = (props: IndexProps): Index => {
$$type: 'ais.index',

getIndexId() {
return indexName;
return indexId || indexName;
},

getHelper() {
Expand Down