Skip to content

Commit

Permalink
feat(connectors.star-rating): provide currentRefinement value
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4x committed Mar 27, 2017
1 parent e94c8c7 commit c08b3e4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/connectors/star-rating/__tests__/connectStarRating-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,50 @@ describe('connectStarRating', () => {
expect(helper.search.callCount).toBe(2);
}
});

it('provides the correct `currentRefinement` value', () => {
const rendering = jest.fn();
const makeWidget = connectStarRating(rendering);

const attributeName = 'grade';
const widget = makeWidget({attributeName});

const config = widget.getConfiguration({});

const helper = jsHelper(fakeClient, '', config);
helper.search = jest.fn();

widget.init({
helper,
state: helper.state,
createURL: () => '#',
onHistoryChange: () => {},
});

const [[firstRenderingOptions]] = rendering.mock.calls;
expect(rendering).toBeCalled();
expect(firstRenderingOptions.currentRefinement).toBe(null);

firstRenderingOptions.refine('3');
widget.render({
results: new SearchResults(helper.state, [{
facets: {
[attributeName]: {3: 50, 4: 900, 5: 100},
},
}, {
facets: {
[attributeName]: {0: 5, 1: 10, 2: 20, 3: 50, 4: 900, 5: 100},
},
}]),
state: helper.state,
helper,
createURL: () => '#',
});

const [, [secondRenderingOptions]] = rendering.mock.calls;
expect(secondRenderingOptions.currentRefinement).toEqual({
count: 1050, isRefined: true, name: '3',
stars: [true, true, true, false, false],
});
});
});
4 changes: 4 additions & 0 deletions src/connectors/star-rating/connectStarRating.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var customStarRating = connectStarRating(function render(params, isFirstRenderin
// instantSearchInstance,
// hasNoResults,
// widgetParams,
// currentRefinement,
// }
});
search.addWidget(
Expand All @@ -34,6 +35,7 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
* @property {boolean} hasNoResults a boolean that indicates that the last search contains no results
* @property {InstantSearch} instantSearchInstance the instance of instantsearch on which the widget is attached
* @property {Object} widgetParams all original options forwarded to rendering
* @property {Object} currentRefinement the refinement currently applied
*/

/**
Expand Down Expand Up @@ -70,6 +72,7 @@ export default function connectStarRating(renderFn) {
refine: this._toggleRefinement,
createURL: this._createURL(helper.state),
widgetParams,
currentRefinement: null,
}, true);
},

Expand Down Expand Up @@ -115,6 +118,7 @@ export default function connectStarRating(renderFn) {
refine: this._toggleRefinement,
createURL: this._createURL(state),
widgetParams,
currentRefinement: facetValues.find(({isRefined}) => isRefined),
}, false);
},

Expand Down

0 comments on commit c08b3e4

Please sign in to comment.