From e67e75e9403f76c188d907244f026ef35456e87b Mon Sep 17 00:00:00 2001 From: Alexandre Stanislawski Date: Tue, 21 Feb 2017 14:21:05 +0100 Subject: [PATCH] feat(connector): test connectInfiniteHits --- .../__tests__/connectInfiniteHits-test.js | 148 ++++++++++++++++++ .../infinite-hits/connectInfiniteHits.js | 2 +- 2 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 src/connectors/infinite-hits/__tests__/connectInfiniteHits-test.js diff --git a/src/connectors/infinite-hits/__tests__/connectInfiniteHits-test.js b/src/connectors/infinite-hits/__tests__/connectInfiniteHits-test.js new file mode 100644 index 0000000000..75a75b7178 --- /dev/null +++ b/src/connectors/infinite-hits/__tests__/connectInfiniteHits-test.js @@ -0,0 +1,148 @@ +/* eslint-env mocha */ + +import expect from 'expect'; +import sinon from 'sinon'; + +import jsHelper from 'algoliasearch-helper'; +const SearchResults = jsHelper.SearchResults; + +import connectInfiniteHits from '../connectInfiniteHits.js'; + +const fakeClient = {addAlgoliaAgent: () => {}}; + +describe('connectInfiniteHits', () => { + it('Renders during init and render', () => { + const container = document.createElement('div'); + // test that the dummyRendering is called with the isFirstRendering + // flag set accordingly + const rendering = sinon.stub(); + const makeWidget = connectInfiniteHits(rendering); + const widget = makeWidget({ + container, + hitsPerPage: 10, + }); + + const config = widget.getConfiguration(); + expect(config).toEqual({hitsPerPage: 10}); + + // test if widget is not rendered yet at this point + expect(rendering.callCount).toBe(0); + + const helper = jsHelper(fakeClient, '', config); + helper.search = sinon.stub(); + + widget.init({ + helper, + state: helper.state, + createURL: () => '#', + onHistoryChange: () => {}, + }); + + // test that rendering has been called during init with isFirstRendering = true + expect(rendering.callCount).toBe(1); + // test if isFirstRendering is true during init + expect(rendering.lastCall.args[1]).toBe(true); + + const firstRenderingOptions = rendering.lastCall.args[0]; + expect(firstRenderingOptions.containerNode).toBe(container); + + widget.render({ + results: new SearchResults(helper.state, [{ + hits: [], + }]), + state: helper.state, + helper, + createURL: () => '#', + }); + + // test that rendering has been called during init with isFirstRendering = false + expect(rendering.callCount).toBe(2); + expect(rendering.lastCall.args[1]).toBe(false); + + const secondRenderingOptions = rendering.lastCall.args[0]; + expect(secondRenderingOptions.containerNode).toBe(container); + }); + + it('Provides the hits and the whole results', () => { + const container = document.createElement('div'); + const rendering = sinon.stub(); + const makeWidget = connectInfiniteHits(rendering); + const widget = makeWidget({ + container, + }); + + const helper = jsHelper(fakeClient, '', {}); + helper.search = sinon.stub(); + + widget.init({ + helper, + state: helper.state, + createURL: () => '#', + onHistoryChange: () => {}, + }); + + const firstRenderingOptions = rendering.lastCall.args[0]; + expect(firstRenderingOptions.hits).toEqual([]); + expect(firstRenderingOptions.results).toBe(undefined); + + const hits = [ + {fake: 'data'}, + {sample: 'infos'}, + ]; + const results = new SearchResults(helper.state, [{hits}]); + widget.render({ + results, + state: helper.state, + helper, + createURL: () => '#', + }); + + const secondRenderingOptions = rendering.lastCall.args[0]; + const {showMore} = secondRenderingOptions; + expect(secondRenderingOptions.hits).toEqual(hits); + expect(secondRenderingOptions.results).toEqual(results); + showMore(); + expect(helper.search.callCount).toBe(1); + + // the results should accumulate if there is an increment in page + const otherHits = [ + {fake: 'data 2'}, + {sample: 'infos 2'}, + ]; + const otherResults = new SearchResults(helper.state, [{ + hits: otherHits, + }]); + widget.render({ + results: otherResults, + state: helper.state, + helper, + createURL: () => '#', + }); + + const thirdRenderingOptions = rendering.lastCall.args[0]; + expect(thirdRenderingOptions.hits).toEqual([...hits, ...otherHits]); + expect(thirdRenderingOptions.results).toEqual(otherResults); + + helper.setPage(0); + + // If the page goes back to 0, the hits cache should be flushed + + const thirdHits = [ + {fake: 'data 3'}, + {sample: 'infos 3'}, + ]; + const thirdResults = new SearchResults(helper.state, [{ + hits: thirdHits, + }]); + widget.render({ + results: thirdResults, + state: helper.state, + helper, + createURL: () => '#', + }); + + const fourthRenderingOptions = rendering.lastCall.args[0]; + expect(fourthRenderingOptions.hits).toEqual(thirdHits); + expect(fourthRenderingOptions.results).toEqual(thirdResults); + }); +}); diff --git a/src/connectors/infinite-hits/connectInfiniteHits.js b/src/connectors/infinite-hits/connectInfiniteHits.js index c39fe3f49c..9a95ea0a26 100644 --- a/src/connectors/infinite-hits/connectInfiniteHits.js +++ b/src/connectors/infinite-hits/connectInfiniteHits.js @@ -73,7 +73,7 @@ const connectInfiniteHits = infiniteHitsRendering => ({ infiniteHitsRendering({ cssClasses, hits: hitsCache, - results: [], + results: undefined, showMore: this.showMore, showMoreLabel, templateProps: this._templateProps,