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

Commit

Permalink
fix(connectInfiniteHits): always provide an array for hits
Browse files Browse the repository at this point in the history
  • Loading branch information
samouss committed Mar 9, 2018
1 parent 57be1b5 commit 5774ad5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ export default createConnector({
getProvidedProps(props, searchState, searchResults) {
const results = getResults(searchResults, this.context);

this._allResults = this._allResults || [];

if (!results) {
this._allResults = [];
return {
hits: this._allResults,
hits: [],
hasMore: false,
};
}

const { hits, page, nbPages } = results;

// If it is the same page we do not touch the page result list
if (page === 0) {
this._allResults = hits;
} else if (page > this.previousPage) {
Expand All @@ -64,7 +64,9 @@ export default createConnector({

const lastPageIndex = nbPages - 1;
const hasMore = page < lastPageIndex;

this.previousPage = page;

return {
hits: this._allResults,
hasMore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,33 @@ describe('connectInfiniteHits', () => {
const state1 = refine(props, state0);
expect(state1).toEqual({ page: 1 });
});

it('expect to always return an array of hits', () => {
const context = createSingleIndexContext();
const getProvidedProps = connect.getProvidedProps.bind(context);

const props = {};
const searchState = {};

// Retrieve the results from the cache that's why
// the page it's not zero on the first render
const searchResults = {
results: {
hits: [{}, {}, {}],
page: 1,
nbPages: 3,
},
};

const expectation = {
hits: [],
hasMore: true,
};

const actual = getProvidedProps(props, searchState, searchResults);

expect(actual).toEqual(expectation);
});
});

describe('multi index', () => {
Expand Down

0 comments on commit 5774ad5

Please sign in to comment.