Skip to content

Commit

Permalink
fix(infiniteHits): work with controlled mode (#4435)
Browse files Browse the repository at this point in the history
* fix(infiniteHits): WIP

* chore(test): expect searchWithoutTriggeringOnStateChange to be called

* fix: always include `page` in uiState

* chore: fix lint error

* fix: return correct page from getWidgetState

* fix: do not add page to uiState if it's the first page

* test: fix test to expect empty uiState on first page

* fix: update condition in getWidgetState

* Revert "fix: update condition in getWidgetState"

This reverts commit c77770f.
  • Loading branch information
Eunjae Lee authored Jul 7, 2020
1 parent eb8c8b3 commit 68b20f4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/connectors/infinite-hits/__tests__/connectInfiniteHits-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
const helper = algoliasearchHelper({} as SearchClient, '', {});
helper.setPage(1);
helper.search = jest.fn();
(helper as any).searchWithoutTriggeringOnStateChange = jest.fn();
helper.emit = jest.fn();

widget.init!(
Expand Down Expand Up @@ -227,7 +228,10 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
showPrevious();
expect(helper.state.page).toBe(0);
expect(helper.emit).not.toHaveBeenCalled();
expect(helper.search).toHaveBeenCalledTimes(1);
expect(helper.search).toHaveBeenCalledTimes(0);
expect(
(helper as any).searchWithoutTriggeringOnStateChange
).toHaveBeenCalledTimes(1);

// the results should be prepended if there is an decrement in page
const previousHits = [
Expand Down Expand Up @@ -789,7 +793,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
});

describe('getWidgetState', () => {
test('returns the `uiState` empty without `showPrevious` option', () => {
test('returns the `uiState` with `page` when `showPrevious` not given', () => {
const render = jest.fn();
const makeWidget = connectInfiniteHits(render);
const helper = algoliasearchHelper(createSearchClient(), 'indexName', {
Expand All @@ -805,10 +809,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
}
);

expect(actual).toEqual({});
expect(actual).toEqual({
page: 2,
});
});

test('returns the `uiState` empty with `showPrevious` option on first page', () => {
test('returns the `uiState` without `page` on first page', () => {
const render = jest.fn();
const makeWidget = connectInfiniteHits(render);
const helper = algoliasearchHelper(createSearchClient(), 'indexName', {
Expand Down
7 changes: 4 additions & 3 deletions src/connectors/infinite-hits/connectInfiniteHits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ const connectInfiniteHits: InfiniteHitsConnector = function connectInfiniteHits(
const {
escapeHTML = true,
transformItems = (items: any[]) => items,
showPrevious: hasShowPrevious = false,
cache = getInMemoryCache(),
} = widgetParams || ({} as typeof widgetParams);
let cachedHits: InfiniteHitsCachedHits | undefined = undefined;
Expand All @@ -158,7 +157,7 @@ const connectInfiniteHits: InfiniteHitsConnector = function connectInfiniteHits(
...helper.state,
page: getFirstReceivedPage() - 1,
})
.search();
.searchWithoutTriggeringOnStateChange();
};
const getShowMore = (helper: Helper): (() => void) => () => {
helper.setPage(getLastReceivedPage() + 1).search();
Expand Down Expand Up @@ -305,7 +304,9 @@ const connectInfiniteHits: InfiniteHitsConnector = function connectInfiniteHits(
getWidgetState(uiState, { searchParameters }) {
const page = searchParameters.page || 0;

if (!hasShowPrevious || !page) {
if (!page) {
// return without adding `page` to uiState
// because we don't want `page=1` in the URL
return uiState;
}

Expand Down
4 changes: 4 additions & 0 deletions src/widgets/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ const index = (props: IndexProps): Index => {
return mainHelper.search();
};

(helper as any).searchWithoutTriggeringOnStateChange = () => {
return mainHelper.search();
};

// We use the same pattern for the `searchForFacetValues`.
helper.searchForFacetValues = (
facetName,
Expand Down

0 comments on commit 68b20f4

Please sign in to comment.