Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
feat(facetOrdering): add a new option "facetOrdering" to Menu, Refine…
Browse files Browse the repository at this point in the history
…mentList & HierarchicalMenu

If `facetOrdering` is enabled (the default behaviour), before the default sortBy is used, the result from renderingContent.facetOrdering.values is first checked. If that's present, it will be used to sort the items.

You can still change that ordering afterwards with the existing transformItems, so if you are sorting in transformItems, you actually override the sorting done by facet ordering, and won't see the effect. To use facetOrdering, you thus need to remove any sorting done in transformItems.

If there is a facetOrdering present in the index, but you don't want to use it for a certain widget, you need to explicitly pass `facetOrdering: false` to the widget or connector

References:
- [RFC 45](https://github.com/algolia/instantsearch-rfcs/blob/master/accepted/flexible-facet-values.md)
- algolia/instantsearch#4784
- algolia/algoliasearch-helper-js#822
  • Loading branch information
Haroenv committed Jun 25, 2021
1 parent 4da38b5 commit ddaf941
Show file tree
Hide file tree
Showing 24 changed files with 528 additions and 91 deletions.
2 changes: 1 addition & 1 deletion examples/autocomplete/package.json
Expand Up @@ -13,7 +13,7 @@
"react-test-renderer": "16.8.6"
},
"dependencies": {
"algoliasearch": "4.8.5",
"algoliasearch": "4.9.3",
"antd": "3.23.2",
"lodash": "4.17.15",
"prop-types": "15.6.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/default-theme/package.json
Expand Up @@ -3,7 +3,7 @@
"version": "6.11.1",
"private": true,
"dependencies": {
"algoliasearch": "4.8.5",
"algoliasearch": "4.9.3",
"qs": "6.8.0",
"react": "16.8.6",
"react-dom": "16.8.6",
Expand Down
2 changes: 1 addition & 1 deletion examples/e-commerce/package.json
Expand Up @@ -3,7 +3,7 @@
"version": "6.11.1",
"private": true,
"dependencies": {
"algoliasearch": "4.8.5",
"algoliasearch": "4.9.3",
"classnames": "2.2.6",
"qs": "6.8.0",
"react": "16.8.6",
Expand Down
2 changes: 1 addition & 1 deletion examples/geo-search/package.json
Expand Up @@ -13,7 +13,7 @@
"react-test-renderer": "16.8.6"
},
"dependencies": {
"algoliasearch": "4.8.5",
"algoliasearch": "4.9.3",
"instantsearch.css": "7.3.1",
"qs": "6.8.0",
"react": "16.8.6",
Expand Down
2 changes: 1 addition & 1 deletion examples/media/package.json
Expand Up @@ -3,7 +3,7 @@
"version": "6.11.1",
"private": true,
"dependencies": {
"algoliasearch": "4.8.5",
"algoliasearch": "4.9.3",
"qs": "6.8.0",
"react": "16.8.6",
"react-dom": "16.8.6",
Expand Down
2 changes: 1 addition & 1 deletion examples/multi-index/package.json
Expand Up @@ -13,7 +13,7 @@
"react-test-renderer": "16.8.6"
},
"dependencies": {
"algoliasearch": "4.8.5",
"algoliasearch": "4.9.3",
"instantsearch.css": "7.3.1",
"prop-types": "15.6.0",
"react": "16.8.6",
Expand Down
2 changes: 1 addition & 1 deletion examples/next/package.json
Expand Up @@ -18,7 +18,7 @@
"style-loader": "1.0.0"
},
"dependencies": {
"algoliasearch": "4.8.5",
"algoliasearch": "4.9.3",
"next": "9.1.1",
"prop-types": "15.6.2",
"qs": "6.8.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/react-native-query-suggestions/package.json
Expand Up @@ -12,7 +12,7 @@
"test": "jest"
},
"dependencies": {
"algoliasearch": "4.8.5",
"algoliasearch": "4.9.3",
"expo": "37.0.3",
"lodash": "4.17.15",
"prop-types": "15.6.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/react-native/package.json
Expand Up @@ -13,7 +13,7 @@
},
"dependencies": {
"@ptomasroos/react-native-multi-slider": "2.2.2",
"algoliasearch": "4.8.5",
"algoliasearch": "4.9.3",
"expo": "37.0.3",
"lodash": "4.17.15",
"prop-types": "15.6.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/react-router-v3/package.json
Expand Up @@ -13,7 +13,7 @@
"react-test-renderer": "16.8.6"
},
"dependencies": {
"algoliasearch": "4.8.5",
"algoliasearch": "4.9.3",
"instantsearch.css": "7.3.1",
"lodash": "4.17.15",
"prop-types": "15.6.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/react-router/package.json
Expand Up @@ -14,7 +14,7 @@
"react-test-renderer": "16.8.6"
},
"dependencies": {
"algoliasearch": "4.8.5",
"algoliasearch": "4.9.3",
"instantsearch.css": "7.3.1",
"lodash": "4.17.15",
"prop-types": "15.6.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/server-side-rendering/package.json
Expand Up @@ -26,7 +26,7 @@
"webpack-node-externals": "1.7.2"
},
"dependencies": {
"algoliasearch": "4.8.5",
"algoliasearch": "4.9.3",
"express": "4.17.1",
"prop-types": "15.6.0",
"react": "16.8.6",
Expand Down
2 changes: 1 addition & 1 deletion examples/tourism/package.json
Expand Up @@ -3,7 +3,7 @@
"version": "6.11.1",
"private": true,
"dependencies": {
"algoliasearch": "4.8.5",
"algoliasearch": "4.9.3",
"qs": "6.8.0",
"react": "16.8.6",
"react-dom": "16.8.6",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -72,7 +72,7 @@
"@wdio/selenium-standalone-service": "5.16.10",
"@wdio/spec-reporter": "5.16.11",
"@wdio/static-server-service": "5.16.10",
"algoliasearch": "4.8.5",
"algoliasearch": "4.9.3",
"argos-cli": "0.1.3",
"babel-eslint": "10.0.2",
"babel-jest": "24.9.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-instantsearch-core/package.json
Expand Up @@ -37,7 +37,7 @@
},
"dependencies": {
"@babel/runtime": "^7.1.2",
"algoliasearch-helper": "^3.4.3",
"algoliasearch-helper": "^3.5.3",
"prop-types": "^15.6.2",
"react-fast-compare": "^3.0.0"
},
Expand Down
@@ -1,4 +1,4 @@
import { SearchParameters } from 'algoliasearch-helper';
import { SearchResults, SearchParameters } from 'algoliasearch-helper';
import connect from '../connectHierarchicalMenu';

jest.mock('../../core/createConnector', () => x => x);
Expand Down Expand Up @@ -174,6 +174,158 @@ describe('connectHierarchicalMenu', () => {
expect(props.items).toEqual(['items']);
});

it('facetValues results uses facetOrdering by default', () => {
const props = {
...connect.defaultProps,
attributes: ['lvl0', 'lvl1'],
contextValue,
};
const searchState = { hierarchicalMenu: { lvl0: 'wat' } };
const state = connect.getSearchParameters(
new SearchParameters(),
props,
searchState
);
const results = new SearchResults(state, [
{
hits: [],
renderingContent: {
facetOrdering: {
values: {
lvl0: {
order: ['wat'],
},
lvl1: {
order: ['wat > wut'],
},
},
},
},
facets: {
lvl0: {
wat: 20,
oy: 10,
},
lvl1: {
'wat > wot': 15,
'wat > wut': 5,
},
},
},
]);

const providedProps = connect.getProvidedProps(props, searchState, {
results,
});
expect(providedProps.items).toEqual([
{
label: 'wat',
value: undefined,
count: 20,
isRefined: true,
items: [
{
label: 'wut',
value: 'wat > wut',
count: 5,
isRefined: false,
items: null,
},
{
label: 'wot',
value: 'wat > wot',
count: 15,
isRefined: false,
items: null,
},
],
},
{
label: 'oy',
value: 'oy',
count: 10,
isRefined: false,
items: null,
},
]);
});

it('facetValues results does not use facetOrdering if disabled', () => {
const props = {
attributes: ['lvl0', 'lvl1'],
facetOrdering: false,
contextValue,
};
const searchState = { hierarchicalMenu: { lvl0: 'wat' } };
const state = connect.getSearchParameters(
new SearchParameters(),
props,
searchState
);
const results = new SearchResults(state, [
{
hits: [],
renderingContent: {
facetOrdering: {
values: {
lvl0: {
order: ['wat'],
},
lvl1: {
order: ['wat > wut'],
},
},
},
},
facets: {
lvl0: {
wat: 20,
oy: 10,
},
lvl1: {
'wat > wot': 15,
'wat > wut': 5,
},
},
},
]);

const providedProps = connect.getProvidedProps(props, searchState, {
results,
});
expect(providedProps.items).toEqual([
{
label: 'oy',
value: 'oy',
count: 10,
isRefined: false,
items: null,
},
{
label: 'wat',
value: undefined,
count: 20,
isRefined: true,
items: [
{
label: 'wot',
value: 'wat > wot',
count: 15,
isRefined: false,
items: null,
},
{
label: 'wut',
value: 'wat > wut',
count: 5,
isRefined: false,
items: null,
},
],
},
]);
});

it('shows the effect of showMoreLimit when there is no transformItems', () => {
const results = {
getFacetValues: jest.fn(),
Expand Down

0 comments on commit ddaf941

Please sign in to comment.