Skip to content

Commit

Permalink
fix(range): clear widget state on empty refinements (#4157)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour authored and Haroenv committed Oct 23, 2019
1 parent 44f69a0 commit 23cd112
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
86 changes: 86 additions & 0 deletions src/connectors/range/__tests__/connectRange-test.js
Expand Up @@ -955,6 +955,32 @@ describe('getWidgetState', () => {
expect(actual).toEqual({});
});

test('returns the `uiState` empty with empty refinements', () => {
const render = jest.fn();
const makeWidget = connectRange(render);
const helper = jsHelper({}, 'indexName', {
disjunctiveFacets: ['price'],
numericRefinements: {
price: {
'>=': [],
'<=': [],
},
},
});
const widget = makeWidget({
attribute: 'price',
});

const actual = widget.getWidgetState(
{},
{
searchParameters: helper.state,
}
);

expect(actual).toEqual({});
});

test('returns the `uiState` with a lower refinement', () => {
const render = jest.fn();
const makeWidget = connectRange(render);
Expand Down Expand Up @@ -1043,6 +1069,66 @@ describe('getWidgetState', () => {
});
});

test('returns the `uiState` with an empty upper refinement', () => {
const render = jest.fn();
const makeWidget = connectRange(render);
const helper = jsHelper({}, 'indexName', {
disjunctiveFacets: ['price'],
numericRefinements: {
price: {
'>=': [100],
'<=': [],
},
},
});
const widget = makeWidget({
attribute: 'price',
});

const actual = widget.getWidgetState(
{},
{
searchParameters: helper.state,
}
);

expect(actual).toEqual({
range: {
price: '100:',
},
});
});

test('returns the `uiState` with an empty lower refinement', () => {
const render = jest.fn();
const makeWidget = connectRange(render);
const helper = jsHelper({}, 'indexName', {
disjunctiveFacets: ['price'],
numericRefinements: {
price: {
'>=': [],
'<=': [1000],
},
},
});
const widget = makeWidget({
attribute: 'price',
});

const actual = widget.getWidgetState(
{},
{
searchParameters: helper.state,
}
);

expect(actual).toEqual({
range: {
price: ':1000',
},
});
});

test('returns the `uiState` without namespace overridden', () => {
const render = jest.fn();
const makeWidget = connectRange(render);
Expand Down
6 changes: 3 additions & 3 deletions src/connectors/range/connectRange.js
Expand Up @@ -260,11 +260,11 @@ export default function connectRange(renderFn, unmountFn = noop) {

getWidgetState(uiState, { searchParameters }) {
const {
'>=': min = '',
'<=': max = '',
'>=': min = [],
'<=': max = [],
} = searchParameters.getNumericRefinements(attribute);

if (min === '' && max === '') {
if (min.length === 0 && max.length === 0) {
return uiState;
}

Expand Down
6 changes: 6 additions & 0 deletions src/lib/utils/__tests__/clearRefinements-test.ts
Expand Up @@ -162,6 +162,7 @@ describe('clearRefinements', () => {
expect(
clearRefinements({
helper: algoliasearchHelper({} as Client, '', {
disjunctiveFacets: ['attr'],
numericRefinements: {
attr: {
'=': [42],
Expand All @@ -172,6 +173,7 @@ describe('clearRefinements', () => {
})
).toEqual(
new SearchParameters({
disjunctiveFacets: ['attr'],
numericRefinements: {
attr: {
'=': [],
Expand All @@ -187,6 +189,7 @@ describe('clearRefinements', () => {
expect(
clearRefinements({
helper: algoliasearchHelper({} as Client, '', {
disjunctiveFacets: ['attr'],
numericRefinements: {
attr: {
'=': [],
Expand All @@ -197,6 +200,7 @@ describe('clearRefinements', () => {
})
).toEqual(
new SearchParameters({
disjunctiveFacets: ['attr'],
numericRefinements: {
attr: {
'=': [],
Expand All @@ -212,6 +216,7 @@ describe('clearRefinements', () => {
expect(
clearRefinements({
helper: algoliasearchHelper({} as Client, '', {
disjunctiveFacets: ['attr'],
numericRefinements: {
attr: {
'=': [42],
Expand All @@ -223,6 +228,7 @@ describe('clearRefinements', () => {
})
).toEqual(
new SearchParameters({
disjunctiveFacets: ['attr'],
numericRefinements: {
attr: {
'=': [],
Expand Down
7 changes: 4 additions & 3 deletions src/lib/utils/clearRefinements.ts
Expand Up @@ -19,6 +19,9 @@ function clearRefinements({
let finalState = helper.state.setPage(0);

finalState = attributesToClear.reduce((state, attribute) => {
if (finalState.isNumericRefined(attribute)) {
return state.removeNumericRefinement(attribute);
}
if (finalState.isHierarchicalFacet(attribute)) {
return state.removeHierarchicalFacetRefinement(attribute);
}
Expand All @@ -28,9 +31,7 @@ function clearRefinements({
if (finalState.isConjunctiveFacet(attribute)) {
return state.removeFacetRefinement(attribute);
}
if (finalState.isNumericRefined(attribute)) {
return state.removeNumericRefinement(attribute);
}

return state;
}, finalState);

Expand Down

0 comments on commit 23cd112

Please sign in to comment.