Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
fix(MultiIndex): handle switch between mono and multi index (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthuret authored Jun 19, 2017
1 parent 6eb1e61 commit e161921
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,95 @@ describe('createInstantSearchManager', () => {
});
expect(store.error).toBe(null);
});
it('updates the store and searches when switching from mono to multi index', () => {
const ism = createInstantSearchManager({
indexName: 'first',
initialState: {},
searchParameters: {},
algoliaClient: client,
});

ism.widgetsManager.registerWidget({
getSearchParameters: params => params.setQuery('first query 1'),
});
ism.widgetsManager.registerWidget({
getSearchParameters: params => params.setQuery('second query 1'),
multiIndexContext: { targetedIndex: 'second' },
});
ism.widgetsManager.registerWidget({
getSearchParameters: params => params.setPage(3),
multiIndexContext: { targetedIndex: 'first' },
});
ism.widgetsManager.registerWidget({
getSearchParameters: params => params.setIndex('second'),
multiIndexContext: { targetedIndex: 'second' },
});

ism.onExternalStateUpdate({});

expect(ism.store.getState().results).toBe(null);

jest.runAllTimers();

let 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);

ism.widgetsManager.getWidgets()[0].getSearchParameters = params =>
params.setQuery('first query 2');
ism.widgetsManager.getWidgets().pop();
ism.widgetsManager.getWidgets().pop();
ism.widgetsManager.getWidgets().pop();

ism.onExternalStateUpdate({});

jest.runAllTimers();

store = ism.store.getState();
expect(store.results).toEqual({
query: 'first query 2',
page: 0,
index: 'first',
});

ism.widgetsManager.registerWidget({
getSearchParameters: params => params.setQuery('second query 2'),
multiIndexContext: { targetedIndex: 'second' },
});
ism.widgetsManager.registerWidget({
getSearchParameters: params => params.setPage(3),
multiIndexContext: { targetedIndex: 'first' },
});
ism.widgetsManager.registerWidget({
getSearchParameters: params => params.setIndex('second'),
multiIndexContext: { targetedIndex: 'second' },
});

ism.onExternalStateUpdate({});

jest.runAllTimers();

store = 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',
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ export default function createInstantSearchManager({

function handleSearchSuccess(content) {
const state = store.getState();
let results = state.results ? state.results : [];
let results = state.results ? state.results : {};

/*if switching from mono index to multi index and vice versa,
results needs to reset to {}*/
results = !isEmpty(derivedHelpers) && results.getFacetByName ? {} : results;

if (!isEmpty(derivedHelpers)) {
results[indexMapping[content.index]] = content;
} else {
Expand Down

0 comments on commit e161921

Please sign in to comment.