Skip to content

Commit c08b3e4

Browse files
committed
feat(connectors.star-rating): provide currentRefinement value
1 parent e94c8c7 commit c08b3e4

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/connectors/star-rating/__tests__/connectStarRating-test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,50 @@ describe('connectStarRating', () => {
168168
expect(helper.search.callCount).toBe(2);
169169
}
170170
});
171+
172+
it('provides the correct `currentRefinement` value', () => {
173+
const rendering = jest.fn();
174+
const makeWidget = connectStarRating(rendering);
175+
176+
const attributeName = 'grade';
177+
const widget = makeWidget({attributeName});
178+
179+
const config = widget.getConfiguration({});
180+
181+
const helper = jsHelper(fakeClient, '', config);
182+
helper.search = jest.fn();
183+
184+
widget.init({
185+
helper,
186+
state: helper.state,
187+
createURL: () => '#',
188+
onHistoryChange: () => {},
189+
});
190+
191+
const [[firstRenderingOptions]] = rendering.mock.calls;
192+
expect(rendering).toBeCalled();
193+
expect(firstRenderingOptions.currentRefinement).toBe(null);
194+
195+
firstRenderingOptions.refine('3');
196+
widget.render({
197+
results: new SearchResults(helper.state, [{
198+
facets: {
199+
[attributeName]: {3: 50, 4: 900, 5: 100},
200+
},
201+
}, {
202+
facets: {
203+
[attributeName]: {0: 5, 1: 10, 2: 20, 3: 50, 4: 900, 5: 100},
204+
},
205+
}]),
206+
state: helper.state,
207+
helper,
208+
createURL: () => '#',
209+
});
210+
211+
const [, [secondRenderingOptions]] = rendering.mock.calls;
212+
expect(secondRenderingOptions.currentRefinement).toEqual({
213+
count: 1050, isRefined: true, name: '3',
214+
stars: [true, true, true, false, false],
215+
});
216+
});
171217
});

src/connectors/star-rating/connectStarRating.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var customStarRating = connectStarRating(function render(params, isFirstRenderin
99
// instantSearchInstance,
1010
// hasNoResults,
1111
// widgetParams,
12+
// currentRefinement,
1213
// }
1314
});
1415
search.addWidget(
@@ -34,6 +35,7 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
3435
* @property {boolean} hasNoResults a boolean that indicates that the last search contains no results
3536
* @property {InstantSearch} instantSearchInstance the instance of instantsearch on which the widget is attached
3637
* @property {Object} widgetParams all original options forwarded to rendering
38+
* @property {Object} currentRefinement the refinement currently applied
3739
*/
3840

3941
/**
@@ -70,6 +72,7 @@ export default function connectStarRating(renderFn) {
7072
refine: this._toggleRefinement,
7173
createURL: this._createURL(helper.state),
7274
widgetParams,
75+
currentRefinement: null,
7376
}, true);
7477
},
7578

@@ -115,6 +118,7 @@ export default function connectStarRating(renderFn) {
115118
refine: this._toggleRefinement,
116119
createURL: this._createURL(state),
117120
widgetParams,
121+
currentRefinement: facetValues.find(({isRefined}) => isRefined),
118122
}, false);
119123
},
120124

0 commit comments

Comments
 (0)