|
| 1 | +/* eslint-env mocha */ |
| 2 | + |
| 3 | +import expect from 'expect'; |
| 4 | +import sinon from 'sinon'; |
| 5 | + |
| 6 | +import jsHelper from 'algoliasearch-helper'; |
| 7 | +const SearchResults = jsHelper.SearchResults; |
| 8 | + |
| 9 | +import connectHitsPerPageSelector from '../connectHitsPerPageSelector.js'; |
| 10 | + |
| 11 | +const fakeClient = {addAlgoliaAgent: () => {}}; |
| 12 | + |
| 13 | +describe('connectHitsPerPageSelector', () => { |
| 14 | + it('Renders during init and render', () => { |
| 15 | + const container = document.createElement('div'); |
| 16 | + // test that the dummyRendering is called with the isFirstRendering |
| 17 | + // flag set accordingly |
| 18 | + const rendering = sinon.stub(); |
| 19 | + const makeWidget = connectHitsPerPageSelector(rendering); |
| 20 | + const widget = makeWidget({ |
| 21 | + container, |
| 22 | + options: [ |
| 23 | + {value: 3, label: '3 items per page'}, |
| 24 | + {value: 10, label: '10 items per page'}, |
| 25 | + ], |
| 26 | + }); |
| 27 | + |
| 28 | + expect(widget.getConfiguration).toEqual(undefined); |
| 29 | + |
| 30 | + // test if widget is not rendered yet at this point |
| 31 | + expect(rendering.callCount).toBe(0); |
| 32 | + |
| 33 | + const helper = jsHelper(fakeClient, '', { |
| 34 | + hitsPerPage: 3, |
| 35 | + }); |
| 36 | + helper.search = sinon.stub(); |
| 37 | + |
| 38 | + widget.init({ |
| 39 | + helper, |
| 40 | + state: helper.state, |
| 41 | + createURL: () => '#', |
| 42 | + onHistoryChange: () => {}, |
| 43 | + }); |
| 44 | + |
| 45 | + // test that rendering has been called during init with isFirstRendering = true |
| 46 | + expect(rendering.callCount).toBe(1); |
| 47 | + // test if isFirstRendering is true during init |
| 48 | + expect(rendering.lastCall.args[1]).toBe(true); |
| 49 | + |
| 50 | + const firstRenderingOptions = rendering.lastCall.args[0]; |
| 51 | + expect(firstRenderingOptions.containerNode).toBe(container); |
| 52 | + expect(firstRenderingOptions.shouldAutoHideContainer).toBe(false); |
| 53 | + |
| 54 | + widget.render({ |
| 55 | + results: new SearchResults(helper.state, [{}]), |
| 56 | + state: helper.state, |
| 57 | + helper, |
| 58 | + createURL: () => '#', |
| 59 | + }); |
| 60 | + |
| 61 | + // test that rendering has been called during init with isFirstRendering = false |
| 62 | + expect(rendering.callCount).toBe(2); |
| 63 | + expect(rendering.lastCall.args[1]).toBe(false); |
| 64 | + |
| 65 | + const secondRenderingOptions = rendering.lastCall.args[0]; |
| 66 | + expect(secondRenderingOptions.containerNode).toBe(container); |
| 67 | + expect(secondRenderingOptions.shouldAutoHideContainer).toBe(false); |
| 68 | + }); |
| 69 | + |
| 70 | + it('Provide a function to change the current hits per page, and provide the current value', () => { |
| 71 | + const container = document.createElement('div'); |
| 72 | + const rendering = sinon.stub(); |
| 73 | + const makeWidget = connectHitsPerPageSelector(rendering); |
| 74 | + const widget = makeWidget({ |
| 75 | + container, |
| 76 | + options: [ |
| 77 | + {value: 3, label: '3 items per page'}, |
| 78 | + {value: 10, label: '10 items per page'}, |
| 79 | + {value: 11, label: ''}, |
| 80 | + ], |
| 81 | + }); |
| 82 | + |
| 83 | + const helper = jsHelper(fakeClient, '', { |
| 84 | + hitsPerPage: 11, |
| 85 | + }); |
| 86 | + helper.search = sinon.stub(); |
| 87 | + |
| 88 | + widget.init({ |
| 89 | + helper, |
| 90 | + state: helper.state, |
| 91 | + createURL: () => '#', |
| 92 | + onHistoryChange: () => {}, |
| 93 | + }); |
| 94 | + |
| 95 | + const firstRenderingOptions = rendering.lastCall.args[0]; |
| 96 | + const {setValue} = firstRenderingOptions; |
| 97 | + expect(helper.getQueryParameter('hitsPerPage')).toBe(11); |
| 98 | + setValue(3); |
| 99 | + expect(helper.getQueryParameter('hitsPerPage')).toBe(3); |
| 100 | + |
| 101 | + widget.render({ |
| 102 | + results: new SearchResults(helper.state, [{}]), |
| 103 | + state: helper.state, |
| 104 | + helper, |
| 105 | + createURL: () => '#', |
| 106 | + }); |
| 107 | + |
| 108 | + const secondRenderingOptions = rendering.lastCall.args[0]; |
| 109 | + const {setValue: renderSetValue} = secondRenderingOptions; |
| 110 | + expect(helper.getQueryParameter('hitsPerPage')).toBe(3); |
| 111 | + renderSetValue(10); |
| 112 | + expect(helper.getQueryParameter('hitsPerPage')).toBe(10); |
| 113 | + |
| 114 | + expect(helper.search.callCount).toBe(2); |
| 115 | + }); |
| 116 | + |
| 117 | + it('provides the current hitsPerPage value', () => { |
| 118 | + const container = document.createElement('div'); |
| 119 | + const rendering = sinon.stub(); |
| 120 | + const makeWidget = connectHitsPerPageSelector(rendering); |
| 121 | + const widget = makeWidget({ |
| 122 | + container, |
| 123 | + options: [ |
| 124 | + {value: 3, label: '3 items per page'}, |
| 125 | + {value: 10, label: '10 items per page'}, |
| 126 | + {value: 7, label: ''}, |
| 127 | + ], |
| 128 | + }); |
| 129 | + |
| 130 | + const helper = jsHelper(fakeClient, '', { |
| 131 | + hitsPerPage: 7, |
| 132 | + }); |
| 133 | + helper.search = sinon.stub(); |
| 134 | + |
| 135 | + widget.init({ |
| 136 | + helper, |
| 137 | + state: helper.state, |
| 138 | + createURL: () => '#', |
| 139 | + onHistoryChange: () => {}, |
| 140 | + }); |
| 141 | + |
| 142 | + const firstRenderingOptions = rendering.lastCall.args[0]; |
| 143 | + expect(firstRenderingOptions.currentValue).toBe(7); |
| 144 | + firstRenderingOptions.setValue(3); |
| 145 | + |
| 146 | + widget.render({ |
| 147 | + results: new SearchResults(helper.state, [{}]), |
| 148 | + state: helper.state, |
| 149 | + helper, |
| 150 | + createURL: () => '#', |
| 151 | + }); |
| 152 | + |
| 153 | + const secondRenderingOptions = rendering.lastCall.args[0]; |
| 154 | + expect(secondRenderingOptions.currentValue).toBe(3); |
| 155 | + }); |
| 156 | + |
| 157 | + it('adds an option for the unselecting values, when the current hitsPerPage is defined elsewhere', () => { |
| 158 | + const container = document.createElement('div'); |
| 159 | + const rendering = sinon.stub(); |
| 160 | + const makeWidget = connectHitsPerPageSelector(rendering); |
| 161 | + const widget = makeWidget({ |
| 162 | + container, |
| 163 | + options: [ |
| 164 | + {value: 3, label: '3 items per page'}, |
| 165 | + {value: 10, label: '10 items per page'}, |
| 166 | + ], |
| 167 | + }); |
| 168 | + |
| 169 | + const helper = jsHelper(fakeClient, '', { |
| 170 | + hitsPerPage: 7, |
| 171 | + }); |
| 172 | + helper.search = sinon.stub(); |
| 173 | + |
| 174 | + widget.init({ |
| 175 | + helper, |
| 176 | + state: helper.state, |
| 177 | + createURL: () => '#', |
| 178 | + onHistoryChange: () => {}, |
| 179 | + }); |
| 180 | + |
| 181 | + const firstRenderingOptions = rendering.lastCall.args[0]; |
| 182 | + expect(firstRenderingOptions.options.length).toBe(3); |
| 183 | + firstRenderingOptions.setValue(firstRenderingOptions.options[0].value); |
| 184 | + expect(helper.getQueryParameter('hitsPerPage')).toBe(undefined); |
| 185 | + |
| 186 | + // Reset the hitsPerPage to an actual value |
| 187 | + helper.setQueryParameter('hitsPerPage', 7); |
| 188 | + |
| 189 | + widget.render({ |
| 190 | + results: new SearchResults(helper.state, [{}]), |
| 191 | + state: helper.state, |
| 192 | + helper, |
| 193 | + createURL: () => '#', |
| 194 | + }); |
| 195 | + |
| 196 | + const secondRenderingOptions = rendering.lastCall.args[0]; |
| 197 | + expect(secondRenderingOptions.options.length).toBe(3); |
| 198 | + secondRenderingOptions.setValue(secondRenderingOptions.options[0].value); |
| 199 | + expect(helper.getQueryParameter('hitsPerPage')).toBe(undefined); |
| 200 | + }); |
| 201 | +}); |
0 commit comments