From bbf9f8f35f840f76bca4e641d274d3c4e518af72 Mon Sep 17 00:00:00 2001 From: Alex S Date: Tue, 29 May 2018 11:17:04 +0200 Subject: [PATCH] fix(infinite-hits): fix #2543 (#2948) The infinite hits widget is based internally on the hits widget which accepts the allItems template. However this template is not usable for the purpose of the infinite hits widget. In order to prevent users from using this buggy and yet accepted option this PR generates a exception if we detect this particular template. Fix #2543 --- .../infinite-hits/__tests__/infinite-hits-test.js | 14 +++++++++++--- src/widgets/infinite-hits/infinite-hits.js | 8 ++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/widgets/infinite-hits/__tests__/infinite-hits-test.js b/src/widgets/infinite-hits/__tests__/infinite-hits-test.js index 00af87acfb..e95cc4bafe 100644 --- a/src/widgets/infinite-hits/__tests__/infinite-hits-test.js +++ b/src/widgets/infinite-hits/__tests__/infinite-hits-test.js @@ -76,9 +76,17 @@ describe('infiniteHits()', () => { expect(ReactDOM.render.secondCall.args[1]).toEqual(container); }); - it('does not accept both item and allItems templates', () => { - expect( - infiniteHits.bind({ container, templates: { item: '', allItems: '' } }) + it('does not accept allItems templates', () => { + expect(() => + infiniteHits({ container, templates: { allItems: '' } }) + ).toThrow(); + + expect(() => + infiniteHits({ container, templates: { allItems: 'not empty' } }) + ).toThrow(); + + expect(() => + infiniteHits({ container, templates: { allItems: () => 'template' } }) ).toThrow(); }); diff --git a/src/widgets/infinite-hits/infinite-hits.js b/src/widgets/infinite-hits/infinite-hits.js index b29651662f..5612b71c3d 100644 --- a/src/widgets/infinite-hits/infinite-hits.js +++ b/src/widgets/infinite-hits/infinite-hits.js @@ -124,6 +124,14 @@ export default function infiniteHits({ throw new Error(`Must provide a container.${usage}`); } + // We have this specific check because unlike the hits, infiniteHits does not support this template. + // This can be misleading as they are very similar. + if (templates.allItems !== undefined) { + throw new Error( + 'allItems is not a valid template for the infiniteHits widget' + ); + } + const containerNode = getContainerNode(container); const cssClasses = { root: cx(bem(null), userCssClasses.root),