|
| 1 | +import jsHelper, { |
| 2 | + SearchResults, |
| 3 | + SearchParameters, |
| 4 | +} from 'algoliasearch-helper'; |
| 5 | +import { createSearchClient } from '../../../../test/mock/createSearchClient'; |
| 6 | +import { |
| 7 | + createInitOptions, |
| 8 | + createRenderOptions, |
| 9 | +} from '../../../../test/mock/createWidget'; |
| 10 | +import { createMultiSearchResponse } from '../../../../test/mock/createAPIResponse'; |
| 11 | +import ratingMenu from '../rating-menu'; |
| 12 | + |
| 13 | +function getInitializedWidget() { |
| 14 | + const container = document.createElement('div'); |
| 15 | + const attribute = 'rating'; |
| 16 | + const widget = ratingMenu({ |
| 17 | + container, |
| 18 | + attribute, |
| 19 | + }); |
| 20 | + |
| 21 | + const helper = jsHelper( |
| 22 | + createSearchClient(), |
| 23 | + '', |
| 24 | + widget.getWidgetSearchParameters!(new SearchParameters({}), { |
| 25 | + uiState: {}, |
| 26 | + }) |
| 27 | + ); |
| 28 | + |
| 29 | + widget.init!(createInitOptions({ helper })); |
| 30 | + |
| 31 | + return { widget, container, helper, attribute }; |
| 32 | +} |
| 33 | + |
| 34 | +describe('rendering', () => { |
| 35 | + it('has correct URLs', () => { |
| 36 | + const { widget, container, helper, attribute } = getInitializedWidget(); |
| 37 | + |
| 38 | + widget.render!( |
| 39 | + createRenderOptions({ |
| 40 | + helper, |
| 41 | + results: new SearchResults( |
| 42 | + helper.state, |
| 43 | + createMultiSearchResponse({ |
| 44 | + facets: { |
| 45 | + [attribute]: { |
| 46 | + 0: 5, |
| 47 | + 1: 10, |
| 48 | + 2: 20, |
| 49 | + 3: 50, |
| 50 | + 4: 900, |
| 51 | + 5: 100, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }).results |
| 55 | + ), |
| 56 | + createURL(searchParams) { |
| 57 | + return JSON.stringify(searchParams); |
| 58 | + }, |
| 59 | + }) |
| 60 | + ); |
| 61 | + |
| 62 | + const links = container.querySelectorAll('a'); |
| 63 | + |
| 64 | + expect(links).toHaveLength(4); |
| 65 | + |
| 66 | + expect(JSON.parse(links[0].getAttribute('href')!)).toEqual({ |
| 67 | + facets: [], |
| 68 | + disjunctiveFacets: ['rating'], |
| 69 | + hierarchicalFacets: [], |
| 70 | + facetsRefinements: {}, |
| 71 | + facetsExcludes: {}, |
| 72 | + disjunctiveFacetsRefinements: {}, |
| 73 | + numericRefinements: { rating: { '<=': [5], '>=': [4] } }, |
| 74 | + tagRefinements: [], |
| 75 | + hierarchicalFacetsRefinements: {}, |
| 76 | + index: '', |
| 77 | + }); |
| 78 | + |
| 79 | + expect(JSON.parse(links[1].getAttribute('href')!)).toEqual({ |
| 80 | + facets: [], |
| 81 | + disjunctiveFacets: ['rating'], |
| 82 | + hierarchicalFacets: [], |
| 83 | + facetsRefinements: {}, |
| 84 | + facetsExcludes: {}, |
| 85 | + disjunctiveFacetsRefinements: {}, |
| 86 | + numericRefinements: { rating: { '<=': [5], '>=': [3] } }, |
| 87 | + tagRefinements: [], |
| 88 | + hierarchicalFacetsRefinements: {}, |
| 89 | + index: '', |
| 90 | + }); |
| 91 | + |
| 92 | + expect(JSON.parse(links[2].getAttribute('href')!)).toEqual({ |
| 93 | + facets: [], |
| 94 | + disjunctiveFacets: ['rating'], |
| 95 | + hierarchicalFacets: [], |
| 96 | + facetsRefinements: {}, |
| 97 | + facetsExcludes: {}, |
| 98 | + disjunctiveFacetsRefinements: {}, |
| 99 | + numericRefinements: { rating: { '<=': [5], '>=': [2] } }, |
| 100 | + tagRefinements: [], |
| 101 | + hierarchicalFacetsRefinements: {}, |
| 102 | + index: '', |
| 103 | + }); |
| 104 | + |
| 105 | + expect(JSON.parse(links[3].getAttribute('href')!)).toEqual({ |
| 106 | + facets: [], |
| 107 | + disjunctiveFacets: ['rating'], |
| 108 | + hierarchicalFacets: [], |
| 109 | + facetsRefinements: {}, |
| 110 | + facetsExcludes: {}, |
| 111 | + disjunctiveFacetsRefinements: {}, |
| 112 | + numericRefinements: { rating: { '<=': [5], '>=': [1] } }, |
| 113 | + tagRefinements: [], |
| 114 | + hierarchicalFacetsRefinements: {}, |
| 115 | + index: '', |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments