Skip to content

Commit

Permalink
fix(menu): apply & remove refinement (#4027)
Browse files Browse the repository at this point in the history
IFW-886

This was forgotten in the initial PR of menu

requires algolia/algoliasearch-helper-js#743 for the removal test to pass
  • Loading branch information
Haroenv committed Oct 23, 2019
1 parent 5b6f210 commit 85de2cf
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 16 deletions.
90 changes: 74 additions & 16 deletions src/connectors/menu/__tests__/connectMenu-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,17 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
attribute: 'myFacet',
});

expect(widget.getConfiguration(new SearchParameters({}))).toEqual(
expect(widget.getConfiguration(new SearchParameters())).toEqual(
new SearchParameters({
hierarchicalFacets: [
{
name: 'myFacet',
attributes: ['myFacet'],
},
],
hierarchicalFacetsRefinements: {
myFacet: [],
},
maxValuesPerFacet: 10,
})
);
Expand All @@ -101,14 +104,17 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
limit: 20,
});

expect(widget.getConfiguration(new SearchParameters({}))).toEqual(
expect(widget.getConfiguration(new SearchParameters())).toEqual(
new SearchParameters({
hierarchicalFacets: [
{
name: 'myFacet',
attributes: ['myFacet'],
},
],
hierarchicalFacetsRefinements: {
myFacet: [],
},
maxValuesPerFacet: 20,
})
);
Expand All @@ -123,7 +129,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
limit: 9,
});

const config = widget.getConfiguration(new SearchParameters({}));
const config = widget.getConfiguration(new SearchParameters());
expect(config).toEqual(
new SearchParameters({
hierarchicalFacets: [
Expand All @@ -132,6 +138,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
attributes: ['myFacet'],
},
],
hierarchicalFacetsRefinements: {
myFacet: [],
},
maxValuesPerFacet: 9,
})
);
Expand Down Expand Up @@ -191,7 +200,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
const helper = jsHelper(
{},
'',
widget.getConfiguration(new SearchParameters({}))
widget.getConfiguration(new SearchParameters())
);
helper.search = jest.fn();

Expand Down Expand Up @@ -233,7 +242,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
const helper = jsHelper(
{},
'',
widget.getConfiguration(new SearchParameters({}))
widget.getConfiguration(new SearchParameters())
);
helper.search = jest.fn();

Expand Down Expand Up @@ -352,7 +361,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
const helper = jsHelper(
{},
'',
widget.getConfiguration(new SearchParameters({}))
widget.getConfiguration(new SearchParameters())
);
helper.search = jest.fn();

Expand Down Expand Up @@ -411,7 +420,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
const helper = jsHelper(
{},
'',
widget.getConfiguration(new SearchParameters({}))
widget.getConfiguration(new SearchParameters())
);
expect(() => widget.dispose({ helper, state: helper.state })).not.toThrow();
});
Expand All @@ -424,14 +433,17 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
showMore: true,
});

expect(widget.getConfiguration(new SearchParameters({}))).toEqual(
expect(widget.getConfiguration(new SearchParameters())).toEqual(
new SearchParameters({
hierarchicalFacets: [
{
name: 'myFacet',
attributes: ['myFacet'],
},
],
hierarchicalFacetsRefinements: {
myFacet: [],
},
maxValuesPerFacet: 20,
})
);
Expand All @@ -445,14 +457,17 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
showMoreLimit: 30,
});

expect(widget.getConfiguration(new SearchParameters({}))).toEqual(
expect(widget.getConfiguration(new SearchParameters())).toEqual(
new SearchParameters({
hierarchicalFacets: [
{
name: 'myFacet',
attributes: ['myFacet'],
},
],
hierarchicalFacetsRefinements: {
myFacet: [],
},
maxValuesPerFacet: 30,
})
);
Expand All @@ -468,7 +483,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
});

// When
const config = widget.getConfiguration(new SearchParameters({}));
const config = widget.getConfiguration(new SearchParameters());
const helper = jsHelper({}, '', config);
helper.search = jest.fn();

Expand Down Expand Up @@ -496,7 +511,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
});

// When
const config = widget.getConfiguration(new SearchParameters({}));
const config = widget.getConfiguration(new SearchParameters());
const helper = jsHelper({}, '', config);

helper.search = jest.fn();
Expand Down Expand Up @@ -560,7 +575,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
});

// When
const config = widget.getConfiguration(new SearchParameters({}));
const config = widget.getConfiguration(new SearchParameters());
const helper = jsHelper({}, '', config);

helper.search = jest.fn();
Expand Down Expand Up @@ -613,7 +628,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
const helper = jsHelper(
{},
'',
widget.getConfiguration(new SearchParameters({}))
widget.getConfiguration(new SearchParameters())
);
helper.search = jest.fn();

Expand Down Expand Up @@ -756,7 +771,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
const helper = jsHelper(
{},
indexName,
widget.getConfiguration(new SearchParameters({}))
widget.getConfiguration(new SearchParameters())
);
helper.search = jest.fn();

Expand All @@ -768,6 +783,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
name: 'myFacet',
},
],
hierarchicalFacetsRefinements: {
myFacet: [],
},
maxValuesPerFacet: 20,
index: indexName,
})
Expand Down Expand Up @@ -832,16 +850,56 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
);
});

it('removes unrefined state', () => {
const widget = makeWidget({
attribute: 'myFacet',
limit: 10,
showMore: true,
});
const indexName = 'instant_search';

const helper = jsHelper(
{},
indexName,
widget.getConfiguration(new SearchParameters())
);
helper.search = jest.fn();

expect(helper.state).toEqual(
new SearchParameters({
hierarchicalFacets: [
{
attributes: ['myFacet'],
name: 'myFacet',
},
],
hierarchicalFacetsRefinements: {
myFacet: [],
},
maxValuesPerFacet: 20,
index: indexName,
})
);

const newState = widget.dispose({ state: helper.state });

expect(newState).toEqual(
new SearchParameters({
index: indexName,
})
);
});

it('leaves empty state intact', () => {
const state = new SearchParameters({});
const state = new SearchParameters();
const widget = makeWidget({
attribute: 'myFacet',
limit: 10,
showMore: true,
});
const newState = widget.dispose({ state });

expect(newState).toEqual(new SearchParameters({}));
expect(newState).toEqual(new SearchParameters());
});
});
});
4 changes: 4 additions & 0 deletions src/connectors/menu/connectMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ export default function connectMenu(renderFn, unmountFn = noop) {
attributes: [attribute],
},
],
hierarchicalFacetsRefinements: {
[attribute]:
configuration.hierarchicalFacetsRefinements[attribute] || [],
},
};

const currentMaxValuesPerFacet = configuration.maxValuesPerFacet || 0;
Expand Down

0 comments on commit 85de2cf

Please sign in to comment.