Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(numericMenu): take array into account for empty state #4084

Merged
merged 2 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 65 additions & 6 deletions src/connectors/numeric-menu/__tests__/connectNumericMenu-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,70 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/numeric-men
});
});

it('when the state is cleared, the "no value" value should be refined', () => {
it('when only the refinement is cleared, the "no value" value should be refined', () => {
const rendering = jest.fn();
const makeWidget = connectNumericMenu(rendering);
const listOptions = [
{ label: 'below 10', end: 10 },
{ label: '10 - 20', start: 10, end: 20 },
{ label: 'more than 20', start: 20 },
{ label: '42', start: 42, end: 42 },
{ label: 'void' },
];
const widget = makeWidget({
attribute: 'numerics',
items: listOptions,
});

const helper = jsHelper({});
helper.search = jest.fn();

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

const refine =
rendering.mock.calls[rendering.mock.calls.length - 1][0].refine;
// a user selects a value in the refinement list
refine(encodeValue(listOptions[0].start, listOptions[0].end));

widget.render({
results: new SearchResults(helper.state, [{}]),
state: helper.state,
helper,
createURL: () => '#',
});

// No option should be selected
const expectedResults0 = [...listOptions].map(mapOptionsToItems);
expectedResults0[0].isRefined = true;

const renderingParameters0 =
rendering.mock.calls[rendering.mock.calls.length - 1][0];
expect(renderingParameters0.items).toEqual(expectedResults0);

// Only the current refinement is cleared by a third party
helper.removeNumericRefinement('numerics', '<=', 10);

widget.render({
results: new SearchResults(helper.state, [{}]),
state: helper.state,
helper,
createURL: () => '#',
});

// No option should be selected
const expectedResults1 = [...listOptions].map(mapOptionsToItems);
expectedResults1[4].isRefined = true;

const renderingParameters1 =
rendering.mock.calls[rendering.mock.calls.length - 1][0];
expect(renderingParameters1.items).toEqual(expectedResults1);
});

it('when all the refinements are cleared, the "no value" value should be refined', () => {
const rendering = jest.fn();
const makeWidget = connectNumericMenu(rendering);
const listOptions = [
Expand Down Expand Up @@ -442,11 +505,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/numeric-men
expect(renderingParameters0.items).toEqual(expectedResults0);

// All the refinements are cleared by a third party
helper.setState(
helper.state.setQueryParameter('numericRefinements', {
numerics: {},
})
);
helper.clearRefinements('numerics');

widget.render({
results: new SearchResults(helper.state, [{}]),
Expand Down
5 changes: 4 additions & 1 deletion src/connectors/numeric-menu/connectNumericMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export default function connectNumericMenu(renderFn, unmountFn = noop) {

this._createURL = state => facetValue =>
createURL(refine(state, attribute, items, facetValue));

this._prepareItems = state =>
items.map(({ start, end, label }) => ({
label,
Expand Down Expand Up @@ -254,7 +255,9 @@ function isRefined(state, attribute, option) {
}

if (option.start === undefined && option.end === undefined) {
return Object.keys(currentRefinements).length === 0;
return Object.keys(currentRefinements).every(
operator => (currentRefinements[operator] || []).length === 0
);
}

return undefined;
Expand Down