|
| 1 | +describe('Facets', function () { |
| 2 | + var expect = window.expect; |
| 3 | + |
| 4 | + before(function(){ |
| 5 | + fixture.setBase('mock'); |
| 6 | + var searchTest = fixture.load('searchTestResponse.json'); |
| 7 | + |
| 8 | + //setup document to hold search results |
| 9 | + document.body.innerHTML = __html__['index.html']; |
| 10 | + |
| 11 | + //initialize search |
| 12 | + window.searchobj = new window.Unbxd.setSearch(window.config); |
| 13 | + |
| 14 | + //stub search ajax call with mock response |
| 15 | + this.stub = sinon.stub(jQuery, 'ajax').yieldsTo('success', searchTest); |
| 16 | + |
| 17 | + this.spyOnFacetLoad = sinon.spy(window.searchobj.options, 'onFacetLoad'); |
| 18 | + this.spyFacetOnSelect = sinon |
| 19 | + .spy(window.searchobj.options, 'facetOnSelect'); |
| 20 | + this.spyFacetOnDeselect = sinon |
| 21 | + .spy(window.searchobj.options, 'facetOnDeselect'); |
| 22 | + this.spyRemoveFilter = sinon.spy(window.searchobj, 'removeFilter'); |
| 23 | + this.spyClearFilters = sinon.spy(window.searchobj, 'clearFilters'); |
| 24 | + |
| 25 | + }); |
| 26 | + |
| 27 | + after(function(){ |
| 28 | + this.stub.restore(); |
| 29 | + }); |
| 30 | + |
| 31 | + beforeEach(function(){ |
| 32 | + //reset filters applied |
| 33 | + window.searchobj.clearFilters(true); |
| 34 | + window.searchobj.callResults(window.searchobj.paintResultSet); |
| 35 | + }); |
| 36 | + |
| 37 | + it('Should call onFacetLoad on selecting facet', function(){ |
| 38 | + var beforeCallCount = this.spyOnFacetLoad.callCount; |
| 39 | + var afterCallCount; |
| 40 | + jQuery(window.searchobj.options.facetContainerSelector +' label')[0] |
| 41 | + .click(); |
| 42 | + afterCallCount = this.spyOnFacetLoad.callCount; |
| 43 | + expect(beforeCallCount + 1).to.be.equal(afterCallCount); |
| 44 | + }); |
| 45 | + |
| 46 | + it('Should call facetOnSelect with element as args on facet select', |
| 47 | + function(){ |
| 48 | + var facetElement = jQuery(window.searchobj.options |
| 49 | + .facetContainerSelector + ' label').first(); |
| 50 | + //apply facet |
| 51 | + facetElement.click(); |
| 52 | + expect(jQuery(this.spyFacetOnSelect.args[0][0][0]).text()) |
| 53 | + .to.be.equal(jQuery(window.searchobj.options.facetContainerSelector + |
| 54 | + ' label').first().text()); |
| 55 | + } |
| 56 | + ); |
| 57 | + |
| 58 | + it('Should call facetOnDeselect with element as args on deselecting facet', |
| 59 | + function(){ |
| 60 | + var facetElement = jQuery(window.searchobj.options |
| 61 | + .facetContainerSelector +' label').first(); |
| 62 | + //apply facet |
| 63 | + facetElement.click(); |
| 64 | + //remove facet |
| 65 | + facetElement.click(); |
| 66 | + expect(jQuery(this.spyFacetOnDeselect.args[0][0][0]).text()) |
| 67 | + .to.be.equal(jQuery(window.searchobj.options |
| 68 | + .facetContainerSelector +' label').first().text()); |
| 69 | + } |
| 70 | + ); |
| 71 | + |
| 72 | + it('addFilter - Should add filters to search params', function(){ |
| 73 | + var field = 'color_fq'; |
| 74 | + var value = 'Black'; |
| 75 | + window.searchobj.addFilter(field,value); |
| 76 | + expect(window.searchobj.params.filters[field][value]).to.exist; |
| 77 | + }); |
| 78 | + |
| 79 | + it('removeFilter - Should remove filters from search params', function(){ |
| 80 | + var field = 'color_fq'; |
| 81 | + var value = 'Black'; |
| 82 | + window.searchobj.addFilter(field,value); |
| 83 | + window.searchobj.removeFilter(field,value); |
| 84 | + expect(window.searchobj.params.filters[field]).to.not.exist; |
| 85 | + }); |
| 86 | + |
| 87 | + it('clearFilter - Should clear search param filters',function(){ |
| 88 | + var field = 'color_fq'; |
| 89 | + var value = 'Black'; |
| 90 | + window.searchobj.addFilter(field,value); |
| 91 | + window.searchobj.clearFilters(); |
| 92 | + expect(window.searchobj.params.filters).to.be.empty; |
| 93 | + }); |
| 94 | + |
| 95 | + it('clearFilter(including range) -Should clear filters and rangeFilters', |
| 96 | + function(){ |
| 97 | + var field = 'color_fq'; |
| 98 | + var value = 'Black'; |
| 99 | + var rangeField = 'price_fq'; |
| 100 | + var start = '30'; |
| 101 | + var end = '60'; |
| 102 | + |
| 103 | + window.searchobj.addFilter(field,value); |
| 104 | + window.searchobj.addRangeFilter(rangeField,start,end); |
| 105 | + window.searchobj.clearFilters(true); |
| 106 | + expect(window.searchobj.params.filters).to.be.empty; |
| 107 | + expect(window.searchobj.params.ranges).to.be.empty; |
| 108 | + } |
| 109 | + ); |
| 110 | + |
| 111 | + it('addRangeFilter - Should add rangeFilters to search params', |
| 112 | + function(){ |
| 113 | + var rangeField = 'price_fq'; |
| 114 | + var start = '30'; |
| 115 | + var end = '60'; |
| 116 | + window.searchobj.addRangeFilter(rangeField,start,end); |
| 117 | + expect(window.searchobj.params.ranges[rangeField][start + ' TO ' + end]) |
| 118 | + .to.exist; |
| 119 | + } |
| 120 | + ); |
| 121 | + |
| 122 | + it('removeRangeFilter - Should remove rangeFilters from search params', |
| 123 | + function(){ |
| 124 | + var rangeField = 'price_fq'; |
| 125 | + var start = '30'; |
| 126 | + var end = '60'; |
| 127 | + window.searchobj.addRangeFilter(rangeField,start,end); |
| 128 | + window.searchobj.removeRangeFilter(rangeField,start,end); |
| 129 | + expect(window.searchobj.params.ranges[rangeField]).to.not.exist; |
| 130 | + } |
| 131 | + ); |
| 132 | + |
| 133 | + it('clearRangeFilter - Should clear search param rangeFilters', |
| 134 | + function(){ |
| 135 | + var rangeField = 'price_fq'; |
| 136 | + var start = '30'; |
| 137 | + var end = '60'; |
| 138 | + window.searchobj.addRangeFilter(rangeField,start,end); |
| 139 | + window.searchobj.clearRangeFiltes(); |
| 140 | + expect(window.searchobj.params.ranges).to.be.empty; |
| 141 | + } |
| 142 | + ); |
| 143 | + |
| 144 | + it('MultiSelect - Should be able to apply multiple filters on same facet', |
| 145 | + function(){ |
| 146 | + var field = 'color_fq'; |
| 147 | + var value1 = 'Black'; |
| 148 | + var value2 = 'Red'; |
| 149 | + window.searchobj.addFilter(field,value1); |
| 150 | + window.searchobj.addFilter(field,value2); |
| 151 | + expect(window.searchobj.params.filters[field][value1]) |
| 152 | + .to.equal(window.searchobj.params.filters[field][value2]).and.exist; |
| 153 | + } |
| 154 | + ); |
| 155 | + |
| 156 | + it('Should remove applied filter on clicking on removeSelectedFacetSelector', |
| 157 | + function(){ |
| 158 | + var field = 'color_fq'; |
| 159 | + var value = 'Black'; |
| 160 | + |
| 161 | + window.searchobj.addFilter(field,value); |
| 162 | + window.searchobj.callResults(window.searchobj.paintResultSet); |
| 163 | + jQuery(window.searchobj.options.removeSelectedFacetSelector + |
| 164 | + '[unbxdparam_facetvalue="'+value+'"]').click(); |
| 165 | + |
| 166 | + expect(this.spyRemoveFilter.calledWith(field,value)).to.be.true; |
| 167 | + } |
| 168 | + ); |
| 169 | + |
| 170 | + it('Should remove all filters on clicking on clearSelectedFacetsSelector', |
| 171 | + function(){ |
| 172 | + var field = 'color_fq'; |
| 173 | + var value = 'Black'; |
| 174 | + var rangeField = 'price_fq'; |
| 175 | + var start = '30'; |
| 176 | + var end = '60'; |
| 177 | + |
| 178 | + window.searchobj.addFilter(field,value); |
| 179 | + window.searchobj.addRangeFilter(rangeField,start,end); |
| 180 | + window.searchobj.callResults(window.searchobj.paintResultSet); |
| 181 | + jQuery(window.searchobj.options.clearSelectedFacetsSelector).click(); |
| 182 | + |
| 183 | + expect(this.spyClearFilters.calledWith(true)).to.be.true; |
| 184 | + } |
| 185 | + ); |
| 186 | + |
| 187 | +}); |
0 commit comments