Skip to content

Commit f5dfba7

Browse files
author
Alexandre Stanislawski
committed
feat(connector): test connectPriceRanges
1 parent 6f125b7 commit f5dfba7

File tree

2 files changed

+171
-1
lines changed

2 files changed

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

src/connectors/price-ranges/connectPriceRanges.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ const connectPriceRanges = priceRangesRendering => ({
164164
render({results, helper, state, createURL}) {
165165
let facetValues;
166166

167-
if (results.hits.length > 0) {
167+
if (results && results.hits && results.hits.length > 0) {
168168
facetValues = this._extractRefinedRange(helper);
169169

170170
if (facetValues.length === 0) {

0 commit comments

Comments
 (0)