From 991fea6e25d48e5877e22ee6d8e481336263ee9e Mon Sep 17 00:00:00 2001 From: Marie-Laure Thuret Date: Tue, 18 Apr 2017 10:54:59 +0200 Subject: [PATCH] fix(MultiIndex): derived helper were using main index specifics params (#36) --- ...createInstantSearchManager.derived.test.js | 21 +++++++++- .../src/core/createInstantSearchManager.js | 42 ++++++++++++------- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/packages/react-instantsearch/src/core/createInstantSearchManager.derived.test.js b/packages/react-instantsearch/src/core/createInstantSearchManager.derived.test.js index 720459e7d0..42e1feebc0 100644 --- a/packages/react-instantsearch/src/core/createInstantSearchManager.derived.test.js +++ b/packages/react-instantsearch/src/core/createInstantSearchManager.derived.test.js @@ -12,7 +12,11 @@ jest.mock('algoliasearch-helper/src/algoliasearch.helper.js', () => { ); Helper.prototype._dispatchAlgoliaResponse = function(state) { state.forEach(s => { - this.emit('result', { query: s.state.query, index: s.state.index }, s); + this.emit( + 'result', + { query: s.state.query, page: s.state.page, index: s.state.index }, + s + ); }); }; Helper.prototype.searchForFacetValues = () => @@ -58,6 +62,11 @@ describe('createInstantSearchManager', () => { multiIndexContext: { targetedIndex: 'second' }, }); + ism.widgetsManager.registerWidget({ + getSearchParameters: params => params.setPage(3), + multiIndexContext: { targetedIndex: 'first' }, + }); + expect(ism.store.getState().results).toBe(null); return Promise.resolve().then(() => { @@ -66,10 +75,12 @@ describe('createInstantSearchManager', () => { const store = ism.store.getState(); expect(store.results.first).toEqual({ query: 'first query 1', + page: 3, index: 'first', }); expect(store.results.second).toEqual({ query: 'second query 1', + page: 0, index: 'second', }); expect(store.error).toBe(null); @@ -87,10 +98,12 @@ describe('createInstantSearchManager', () => { const store1 = ism.store.getState(); expect(store.results.first).toEqual({ query: 'first query 2', + page: 3, index: 'first', }); expect(store.results.second).toEqual({ query: 'second query 2', + page: 0, index: 'second', }); expect(store1.error).toBe(null); @@ -114,6 +127,10 @@ describe('createInstantSearchManager', () => { getSearchParameters: params => params.setQuery('second query 1'), multiIndexContext: { targetedIndex: 'second' }, }); + ism.widgetsManager.registerWidget({ + getSearchParameters: params => params.setPage(3), + multiIndexContext: { targetedIndex: 'first' }, + }); ism.onExternalStateUpdate({}); @@ -124,10 +141,12 @@ describe('createInstantSearchManager', () => { const store = ism.store.getState(); expect(store.results.first).toEqual({ query: 'first query 1', + page: 3, index: 'first', }); expect(store.results.second).toEqual({ query: 'second query 1', + page: 0, index: 'second', }); expect(store.error).toBe(null); diff --git a/packages/react-instantsearch/src/core/createInstantSearchManager.js b/packages/react-instantsearch/src/core/createInstantSearchManager.js index 617e706514..81146439db 100644 --- a/packages/react-instantsearch/src/core/createInstantSearchManager.js +++ b/packages/react-instantsearch/src/core/createInstantSearchManager.js @@ -60,20 +60,15 @@ export default function createInstantSearchManager( function getSearchParameters() { indexMapping = {}; - const mainParameters = widgetsManager + const sharedParameters = widgetsManager .getWidgets() .filter(widget => Boolean(widget.getSearchParameters)) - .filter( - widget => - !widget.multiIndexContext || - (widget.multiIndexContext && - widget.multiIndexContext.targetedIndex === indexName) - ) + .filter(widget => !widget.multiIndexContext) .reduce( (res, widget) => widget.getSearchParameters(res), initialSearchParameters ); - indexMapping[mainParameters.index] = indexName; + indexMapping[sharedParameters.index] = indexName; const derivatedWidgets = widgetsManager .getWidgets() @@ -97,25 +92,40 @@ export default function createInstantSearchManager( [] ); - return { mainParameters, derivatedWidgets }; + const mainIndexParameters = widgetsManager + .getWidgets() + .filter(widget => Boolean(widget.getSearchParameters)) + .filter( + widget => + widget.multiIndexContext && + widget.multiIndexContext.targetedIndex === indexName + ) + .reduce( + (res, widget) => widget.getSearchParameters(res), + sharedParameters + ); + + return { sharedParameters, mainIndexParameters, derivatedWidgets }; } function search() { - const { mainParameters, derivatedWidgets } = getSearchParameters( - helper.state - ); + const { + sharedParameters, + mainIndexParameters, + derivatedWidgets, + } = getSearchParameters(helper.state); Object.values(derivedHelpers).forEach(d => d.detach()); derivedHelpers = {}; - helper.setState(mainParameters); + helper.setState(sharedParameters); derivatedWidgets.forEach(derivatedSearchParameters => { const index = derivatedSearchParameters.targetedIndex; - const derivedHelper = helper.derive(sp => { + const derivedHelper = helper.derive(() => { const parameters = derivatedSearchParameters.widgets.reduce( (res, widget) => widget.getSearchParameters(res), - sp.setIndex(index) + sharedParameters.setIndex(index) ); indexMapping[parameters.index] = index; return parameters; @@ -125,6 +135,8 @@ export default function createInstantSearchManager( derivedHelpers[index] = derivedHelper; }); + helper.setState(mainIndexParameters); + helper.search(); }