|
| 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 connectMenu from '../connectMenu.js'; |
| 10 | + |
| 11 | +const fakeClient = {addAlgoliaAgent: () => {}}; |
| 12 | + |
| 13 | +describe('connectMenu', () => { |
| 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 = connectMenu(rendering); |
| 20 | + const widget = makeWidget({ |
| 21 | + container, |
| 22 | + attributeName: 'myFacet', |
| 23 | + limit: 9, |
| 24 | + }); |
| 25 | + |
| 26 | + const config = widget.getConfiguration({}); |
| 27 | + expect(config).toEqual({ |
| 28 | + hierarchicalFacets: [{ |
| 29 | + name: 'myFacet', |
| 30 | + attributes: ['myFacet'], |
| 31 | + }], |
| 32 | + maxValuesPerFacet: 9, |
| 33 | + }); |
| 34 | + |
| 35 | + // test if widget is not rendered yet at this point |
| 36 | + expect(rendering.callCount).toBe(0); |
| 37 | + |
| 38 | + const helper = jsHelper(fakeClient, '', config); |
| 39 | + helper.search = sinon.stub(); |
| 40 | + |
| 41 | + widget.init({ |
| 42 | + helper, |
| 43 | + state: helper.state, |
| 44 | + createURL: () => '#', |
| 45 | + onHistoryChange: () => {}, |
| 46 | + }); |
| 47 | + |
| 48 | + // test that rendering has been called during init with isFirstRendering = true |
| 49 | + expect(rendering.callCount).toBe(1); |
| 50 | + // test if isFirstRendering is true during init |
| 51 | + expect(rendering.lastCall.args[1]).toBe(true); |
| 52 | + |
| 53 | + const firstRenderingOptions = rendering.lastCall.args[0]; |
| 54 | + expect(firstRenderingOptions.shouldAutoHideContainer).toBe(true); |
| 55 | + expect(firstRenderingOptions.limitMax).toBe(9); |
| 56 | + expect(firstRenderingOptions.limitMin).toBe(9); |
| 57 | + expect(firstRenderingOptions.collapsible).toBe(false); |
| 58 | + expect(firstRenderingOptions.containerNode).toBe(container); |
| 59 | + |
| 60 | + widget.render({ |
| 61 | + results: new SearchResults(helper.state, [{}]), |
| 62 | + state: helper.state, |
| 63 | + helper, |
| 64 | + createURL: () => '#', |
| 65 | + }); |
| 66 | + |
| 67 | + // test that rendering has been called during init with isFirstRendering = false |
| 68 | + expect(rendering.callCount).toBe(2); |
| 69 | + expect(rendering.lastCall.args[1]).toBe(false); |
| 70 | + |
| 71 | + const secondRenderingOptions = rendering.lastCall.args[0]; |
| 72 | + expect(secondRenderingOptions.shouldAutoHideContainer).toBe(true); |
| 73 | + expect(secondRenderingOptions.limitMax).toBe(9); |
| 74 | + expect(secondRenderingOptions.limitMin).toBe(9); |
| 75 | + expect(secondRenderingOptions.collapsible).toBe(false); |
| 76 | + expect(secondRenderingOptions.containerNode).toBe(container); |
| 77 | + }); |
| 78 | + |
| 79 | + it('Provide a function to clear the refinements at each step', () => { |
| 80 | + const container = document.createElement('div'); |
| 81 | + const rendering = sinon.stub(); |
| 82 | + const makeWidget = connectMenu(rendering); |
| 83 | + const widget = makeWidget({ |
| 84 | + container, |
| 85 | + attributeName: 'category', |
| 86 | + }); |
| 87 | + |
| 88 | + const helper = jsHelper({}, '', widget.getConfiguration({})); |
| 89 | + helper.search = sinon.stub(); |
| 90 | + |
| 91 | + helper.toggleRefinement('category', 'value'); |
| 92 | + |
| 93 | + widget.init({ |
| 94 | + helper, |
| 95 | + state: helper.state, |
| 96 | + createURL: () => '#', |
| 97 | + onHistoryChange: () => {}, |
| 98 | + }); |
| 99 | + |
| 100 | + const firstRenderingOptions = rendering.lastCall.args[0]; |
| 101 | + const {toggleRefinement} = firstRenderingOptions; |
| 102 | + toggleRefinement('value'); |
| 103 | + expect(helper.hasRefinements('category')).toBe(false); |
| 104 | + toggleRefinement('value'); |
| 105 | + expect(helper.hasRefinements('category')).toBe(true); |
| 106 | + |
| 107 | + widget.render({ |
| 108 | + results: new SearchResults(helper.state, [{}, {}]), |
| 109 | + state: helper.state, |
| 110 | + helper, |
| 111 | + createURL: () => '#', |
| 112 | + }); |
| 113 | + |
| 114 | + const secondRenderingOptions = rendering.lastCall.args[0]; |
| 115 | + const {toggleRefinement: renderToggleRefinement} = secondRenderingOptions; |
| 116 | + renderToggleRefinement('value'); |
| 117 | + expect(helper.hasRefinements('category')).toBe(false); |
| 118 | + renderToggleRefinement('value'); |
| 119 | + expect(helper.hasRefinements('category')).toBe(true); |
| 120 | + }); |
| 121 | + |
| 122 | + it('provides the correct facet values', () => { |
| 123 | + const container = document.createElement('div'); |
| 124 | + const rendering = sinon.stub(); |
| 125 | + const makeWidget = connectMenu(rendering); |
| 126 | + const widget = makeWidget({ |
| 127 | + container, |
| 128 | + attributeName: 'category', |
| 129 | + }); |
| 130 | + |
| 131 | + const helper = jsHelper({}, '', widget.getConfiguration({})); |
| 132 | + helper.search = sinon.stub(); |
| 133 | + |
| 134 | + helper.toggleRefinement('category', 'Decoration'); |
| 135 | + |
| 136 | + widget.init({ |
| 137 | + helper, |
| 138 | + state: helper.state, |
| 139 | + createURL: () => '#', |
| 140 | + onHistoryChange: () => {}, |
| 141 | + }); |
| 142 | + |
| 143 | + const firstRenderingOptions = rendering.lastCall.args[0]; |
| 144 | + // During the first rendering there are no facet values |
| 145 | + // The function get an empty array so that it doesn't break |
| 146 | + // over null-ish values. |
| 147 | + expect(firstRenderingOptions.facetValues).toEqual([]); |
| 148 | + |
| 149 | + widget.render({ |
| 150 | + results: new SearchResults(helper.state, [{ |
| 151 | + hits: [], |
| 152 | + facets: { |
| 153 | + category: { |
| 154 | + Decoration: 880, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, { |
| 158 | + facets: { |
| 159 | + category: { |
| 160 | + Decoration: 880, |
| 161 | + Outdoor: 47, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }]), |
| 165 | + state: helper.state, |
| 166 | + helper, |
| 167 | + createURL: () => '#', |
| 168 | + }); |
| 169 | + |
| 170 | + const secondRenderingOptions = rendering.lastCall.args[0]; |
| 171 | + expect(secondRenderingOptions.facetValues).toEqual([ |
| 172 | + { |
| 173 | + name: 'Decoration', |
| 174 | + path: 'Decoration', |
| 175 | + count: 880, |
| 176 | + isRefined: true, |
| 177 | + data: null, |
| 178 | + url: '#', |
| 179 | + }, |
| 180 | + { |
| 181 | + name: 'Outdoor', |
| 182 | + path: 'Outdoor', |
| 183 | + count: 47, |
| 184 | + isRefined: false, |
| 185 | + data: null, |
| 186 | + url: '#', |
| 187 | + }, |
| 188 | + ]); |
| 189 | + }); |
| 190 | +}); |
0 commit comments