|
| 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 connectRangeSlider from '../connectRangeSlider.js'; |
| 10 | + |
| 11 | +const fakeClient = {addAlgoliaAgent: () => {}}; |
| 12 | + |
| 13 | +describe('connectRangeSlider', () => { |
| 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 = connectRangeSlider(rendering); |
| 20 | + |
| 21 | + const attributeName = 'price'; |
| 22 | + const widget = makeWidget({ |
| 23 | + container, |
| 24 | + attributeName, |
| 25 | + }); |
| 26 | + |
| 27 | + const config = widget.getConfiguration(); |
| 28 | + expect(config).toEqual({ |
| 29 | + disjunctiveFacets: [attributeName], |
| 30 | + }); |
| 31 | + |
| 32 | + const helper = jsHelper(fakeClient, '', config); |
| 33 | + helper.search = sinon.stub(); |
| 34 | + |
| 35 | + widget.init({ |
| 36 | + helper, |
| 37 | + state: helper.state, |
| 38 | + createURL: () => '#', |
| 39 | + onHistoryChange: () => {}, |
| 40 | + }); |
| 41 | + |
| 42 | + { // should call the rendering once with isFirstRendering to true |
| 43 | + expect(rendering.callCount).toBe(1); |
| 44 | + const isFirstRendering = rendering.lastCall.args[1]; |
| 45 | + expect(isFirstRendering).toBe(true); |
| 46 | + |
| 47 | + // should provide good values for the first rendering |
| 48 | + const {range, collapsible, start, |
| 49 | + shouldAutoHideContainer, containerNode} = rendering.lastCall.args[0]; |
| 50 | + expect(range).toEqual({min: 0, max: 0}); |
| 51 | + expect(start).toEqual([-Infinity, Infinity]); |
| 52 | + expect(collapsible).toBe(false); |
| 53 | + expect(shouldAutoHideContainer).toBe(true); |
| 54 | + expect(containerNode).toBe(container); |
| 55 | + } |
| 56 | + |
| 57 | + widget.render({ |
| 58 | + results: new SearchResults(helper.state, [{ |
| 59 | + hits: [{test: 'oneTime'}], |
| 60 | + facets: {price: {10: 1, 20: 1, 30: 1}}, |
| 61 | + facets_stats: { // eslint-disable-line |
| 62 | + price: { |
| 63 | + avg: 20, |
| 64 | + max: 30, |
| 65 | + min: 10, |
| 66 | + sum: 60, |
| 67 | + }, |
| 68 | + }, |
| 69 | + nbHits: 1, |
| 70 | + nbPages: 1, |
| 71 | + page: 0, |
| 72 | + }]), |
| 73 | + state: helper.state, |
| 74 | + helper, |
| 75 | + createURL: () => '#', |
| 76 | + }); |
| 77 | + |
| 78 | + { // Should call the rendering a second time, with isFirstRendering to false |
| 79 | + expect(rendering.callCount).toBe(2); |
| 80 | + const isFirstRendering = rendering.lastCall.args[1]; |
| 81 | + expect(isFirstRendering).toBe(false); |
| 82 | + |
| 83 | + // should provide good values for the first rendering |
| 84 | + const {range, collapsible, start, |
| 85 | + shouldAutoHideContainer, containerNode} = rendering.lastCall.args[0]; |
| 86 | + expect(range).toEqual({min: 10, max: 30}); |
| 87 | + expect(start).toEqual([-Infinity, Infinity]); |
| 88 | + expect(collapsible).toBe(false); |
| 89 | + expect(shouldAutoHideContainer).toBe(false); |
| 90 | + expect(containerNode).toBe(container); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + it('Accepts some user bounds', () => { |
| 95 | + const container = document.createElement('div'); |
| 96 | + const makeWidget = connectRangeSlider(() => {}); |
| 97 | + |
| 98 | + const attributeName = 'price'; |
| 99 | + |
| 100 | + expect(makeWidget({container, attributeName, min: 0}).getConfiguration()).toEqual({ |
| 101 | + disjunctiveFacets: [attributeName], |
| 102 | + numericRefinements: { |
| 103 | + [attributeName]: {'>=': [0]}, |
| 104 | + }, |
| 105 | + }); |
| 106 | + |
| 107 | + expect(makeWidget({container, attributeName, max: 100}).getConfiguration()).toEqual({ |
| 108 | + disjunctiveFacets: [attributeName], |
| 109 | + numericRefinements: { |
| 110 | + [attributeName]: {'<=': [100]}, |
| 111 | + }, |
| 112 | + }); |
| 113 | + |
| 114 | + expect(makeWidget({container, attributeName, min: 0, max: 100}).getConfiguration()).toEqual({ |
| 115 | + disjunctiveFacets: [attributeName], |
| 116 | + numericRefinements: { |
| 117 | + [attributeName]: { |
| 118 | + '>=': [0], |
| 119 | + '<=': [100], |
| 120 | + }, |
| 121 | + }, |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('Provides a function to update the refinements at each step', () => { |
| 126 | + const container = document.createElement('div'); |
| 127 | + |
| 128 | + const rendering = sinon.stub(); |
| 129 | + const makeWidget = connectRangeSlider(rendering); |
| 130 | + |
| 131 | + const attributeName = 'price'; |
| 132 | + const widget = makeWidget({ |
| 133 | + container, |
| 134 | + attributeName, |
| 135 | + }); |
| 136 | + |
| 137 | + const helper = jsHelper(fakeClient, '', widget.getConfiguration()); |
| 138 | + helper.search = sinon.stub(); |
| 139 | + |
| 140 | + widget.init({ |
| 141 | + helper, |
| 142 | + state: helper.state, |
| 143 | + createURL: () => '#', |
| 144 | + onHistoryChange: () => {}, |
| 145 | + }); |
| 146 | + |
| 147 | + { // first rendering |
| 148 | + expect(helper.getNumericRefinement('price', '>=')).toEqual(undefined); |
| 149 | + expect(helper.getNumericRefinement('price', '<=')).toEqual(undefined); |
| 150 | + const renderOptions = rendering.lastCall.args[0]; |
| 151 | + const {refine} = renderOptions; |
| 152 | + refine([10, 30]); |
| 153 | + expect(helper.getNumericRefinement('price', '>=')).toEqual([10]); |
| 154 | + expect(helper.getNumericRefinement('price', '<=')).toEqual([30]); |
| 155 | + expect(helper.search.callCount).toBe(1); |
| 156 | + } |
| 157 | + |
| 158 | + widget.render({ |
| 159 | + results: new SearchResults(helper.state, [{ |
| 160 | + hits: [{test: 'oneTime'}], |
| 161 | + facets: {price: {10: 1, 20: 1, 30: 1}}, |
| 162 | + facets_stats: { // eslint-disable-line |
| 163 | + price: { |
| 164 | + avg: 20, |
| 165 | + max: 30, |
| 166 | + min: 10, |
| 167 | + sum: 60, |
| 168 | + }, |
| 169 | + }, |
| 170 | + nbHits: 1, |
| 171 | + nbPages: 1, |
| 172 | + page: 0, |
| 173 | + }, {}]), |
| 174 | + state: helper.state, |
| 175 | + helper, |
| 176 | + createURL: () => '#', |
| 177 | + }); |
| 178 | + |
| 179 | + { // Second rendering |
| 180 | + expect(helper.getNumericRefinement('price', '>=')).toEqual([10]); |
| 181 | + expect(helper.getNumericRefinement('price', '<=')).toEqual([30]); |
| 182 | + const renderOptions = rendering.lastCall.args[0]; |
| 183 | + const {refine} = renderOptions; |
| 184 | + refine([23, 27]); |
| 185 | + expect(helper.getNumericRefinement('price', '>=')).toEqual([23]); |
| 186 | + expect(helper.getNumericRefinement('price', '<=')).toEqual([27]); |
| 187 | + expect(helper.search.callCount).toBe(2); |
| 188 | + } |
| 189 | + }); |
| 190 | +}); |
0 commit comments