|
| 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 connectSortBySelector from '../connectSortBySelector.js'; |
| 10 | + |
| 11 | +const fakeClient = {addAlgoliaAgent: () => {}}; |
| 12 | + |
| 13 | +describe.only('connectSortBySelector', () => { |
| 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 = connectSortBySelector(rendering); |
| 20 | + |
| 21 | + const indices = [ |
| 22 | + {label: 'Sort products by relevance', name: 'relevance'}, |
| 23 | + {label: 'Sort products by price', name: 'priceASC'}, |
| 24 | + ]; |
| 25 | + const widget = makeWidget({ |
| 26 | + container, |
| 27 | + indices, |
| 28 | + }); |
| 29 | + |
| 30 | + expect(widget.getConfiguration).toBe(undefined); |
| 31 | + |
| 32 | + const helper = jsHelper(fakeClient, indices[0].name); |
| 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 {containerNode, currentValue, options, shouldAutoHideContainer} = rendering.lastCall.args[0]; |
| 49 | + expect(containerNode).toBe(container); |
| 50 | + expect(currentValue).toBe(helper.state.index); |
| 51 | + expect(options).toEqual([ |
| 52 | + {label: 'Sort products by relevance', value: 'relevance'}, |
| 53 | + {label: 'Sort products by price', value: 'priceASC'}, |
| 54 | + ]); |
| 55 | + expect(shouldAutoHideContainer).toBe(false); |
| 56 | + } |
| 57 | + |
| 58 | + widget.render({ |
| 59 | + results: new SearchResults(helper.state, [{}]), |
| 60 | + state: helper.state, |
| 61 | + helper, |
| 62 | + createURL: () => '#', |
| 63 | + }); |
| 64 | + |
| 65 | + { // Should call the rendering a second time, with isFirstRendering to false |
| 66 | + expect(rendering.callCount).toBe(2); |
| 67 | + const isFirstRendering = rendering.lastCall.args[1]; |
| 68 | + expect(isFirstRendering).toBe(false); |
| 69 | + |
| 70 | + // should provide good values after the first search |
| 71 | + const {containerNode, currentValue, options, shouldAutoHideContainer} = rendering.lastCall.args[0]; |
| 72 | + expect(containerNode).toBe(container); |
| 73 | + expect(currentValue).toBe(helper.state.index); |
| 74 | + expect(options).toEqual([ |
| 75 | + {label: 'Sort products by relevance', value: 'relevance'}, |
| 76 | + {label: 'Sort products by price', value: 'priceASC'}, |
| 77 | + ]); |
| 78 | + expect(shouldAutoHideContainer).toBe(false); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + it('Provides a function to update the index at each step', () => { |
| 83 | + const container = document.createElement('div'); |
| 84 | + const rendering = sinon.stub(); |
| 85 | + const makeWidget = connectSortBySelector(rendering); |
| 86 | + |
| 87 | + const indices = [ |
| 88 | + {label: 'Sort products by relevance', name: 'relevance'}, |
| 89 | + {label: 'Sort products by price', name: 'priceASC'}, |
| 90 | + ]; |
| 91 | + const widget = makeWidget({ |
| 92 | + container, |
| 93 | + indices, |
| 94 | + }); |
| 95 | + |
| 96 | + const helper = jsHelper(fakeClient, indices[0].name); |
| 97 | + helper.search = sinon.stub(); |
| 98 | + |
| 99 | + widget.init({ |
| 100 | + helper, |
| 101 | + state: helper.state, |
| 102 | + createURL: () => '#', |
| 103 | + onHistoryChange: () => {}, |
| 104 | + }); |
| 105 | + |
| 106 | + { // first rendering |
| 107 | + expect(helper.state.index).toBe(indices[0].name); |
| 108 | + const renderOptions = rendering.lastCall.args[0]; |
| 109 | + const {setValue, currentValue} = renderOptions; |
| 110 | + expect(currentValue).toBe(helper.state.index); |
| 111 | + setValue('bip'); |
| 112 | + expect(helper.state.index).toBe('bip'); |
| 113 | + expect(helper.search.callCount).toBe(1); |
| 114 | + } |
| 115 | + |
| 116 | + widget.render({ |
| 117 | + results: new SearchResults(helper.state, [{}]), |
| 118 | + state: helper.state, |
| 119 | + helper, |
| 120 | + createURL: () => '#', |
| 121 | + }); |
| 122 | + |
| 123 | + { // Second rendering |
| 124 | + expect(helper.state.index).toBe('bip'); |
| 125 | + const renderOptions = rendering.lastCall.args[0]; |
| 126 | + const {setValue, currentValue} = renderOptions; |
| 127 | + expect(currentValue).toBe('bip'); |
| 128 | + setValue('bop'); |
| 129 | + expect(helper.state.index).toBe('bop'); |
| 130 | + expect(helper.search.callCount).toBe(2); |
| 131 | + } |
| 132 | + }); |
| 133 | +}); |
0 commit comments