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

feat(facets): apply result from facet ordering #4784

Merged
merged 7 commits into from
Jul 5, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,166 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hierarchica
canToggleShowMore: false,
});
});

describe('facetOrdering', () => {
const resultsViaFacetOrdering = [
{
count: 47,
data: null,
exhaustive: true,
isRefined: false,
label: 'Outdoor',
value: 'Outdoor',
},
{
count: 880,
data: [
{
count: 173,
data: null,
exhaustive: true,
isRefined: false,
label: 'Frames & pictures',
value: 'Decoration > Frames & pictures',
},
{
count: 193,
data: null,
exhaustive: true,
isRefined: false,
label: 'Candle holders & candles',
value: 'Decoration > Candle holders & candles',
},
],
exhaustive: true,
isRefined: true,
label: 'Decoration',
value: 'Decoration',
},
];
const resultsViaSortBy = [
{
count: 880,
data: [
{
count: 193,
data: null,
exhaustive: true,
isRefined: false,
label: 'Candle holders & candles',
value: 'Decoration > Candle holders & candles',
},
{
count: 173,
data: null,
exhaustive: true,
isRefined: false,
label: 'Frames & pictures',
value: 'Decoration > Frames & pictures',
},
],
exhaustive: true,
isRefined: true,
label: 'Decoration',
value: 'Decoration',
},
{
count: 47,
data: null,
exhaustive: true,
isRefined: false,
label: 'Outdoor',
value: 'Outdoor',
},
];

test.each`
facetOrderingInResult | sortBy | expected
${true} | ${undefined} | ${resultsViaFacetOrdering}
${false} | ${undefined} | ${resultsViaSortBy}
${true} | ${['name:asc']} | ${resultsViaSortBy}
${false} | ${['name:asc']} | ${resultsViaSortBy}
`(
'renderingContent present: $facetOrderingInResult, sortBy: $sortBy',
({ facetOrderingInResult, sortBy, expected }) => {
const renderFn = jest.fn();
const unmountFn = jest.fn();
const createHierarchicalMenu = connectHierarchicalMenu(
renderFn,
unmountFn
);
const hierarchicalMenu = createHierarchicalMenu({
attributes: ['category', 'subCategory'],
sortBy,
});
const helper = algoliasearchHelper(
createSearchClient(),
'indexName',
hierarchicalMenu.getWidgetSearchParameters!(
new SearchParameters(),
{
uiState: {
hierarchicalMenu: {
category: ['Decoration'],
},
},
}
)
);

hierarchicalMenu.init!(createInitOptions({ helper }));

const renderingContent = facetOrderingInResult
? {
facetOrdering: {
values: {
category: {
order: ['Outdoor'],
sortRemainingBy: 'alpha' as const,
},
subCategory: {
order: ['Decoration > Frames & pictures'],
sortRemainingBy: 'count' as const,
},
},
},
}
: undefined;

const results = new SearchResults(helper.state, [
createSingleSearchResponse({
renderingContent,
facets: {
category: {
Decoration: 880,
},
subCategory: {
'Decoration > Candle holders & candles': 193,
'Decoration > Frames & pictures': 173,
},
},
}),
createSingleSearchResponse({
facets: {
category: {
Decoration: 880,
Outdoor: 47,
},
},
}),
]);

const renderState = hierarchicalMenu.getWidgetRenderState(
createRenderOptions({
helper,
results,
})
);

expect(renderState.items).toEqual(expected);
}
);
});
});

describe('getWidgetUiState', () => {
Expand Down
12 changes: 6 additions & 6 deletions src/connectors/hierarchical-menu/connectHierarchicalMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const withUsage = createDocumentationMessageGenerator({
connector: true,
});

const DEFAULT_SORT = ['name:asc'];

export type HierarchicalMenuItem = {
/**
* Value of the menu item.
Expand Down Expand Up @@ -79,6 +81,8 @@ export type HierarchicalMenuConnectorParams = {
/**
* How to sort refinements. Possible values: `count|isRefined|name:asc|name:desc`.
* You can also use a sort function that behaves like the standard Javascript [compareFunction](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Syntax).
*
* If a facetOrdering is set in the index settings, it is used when sortBy isn't passed
*/
sortBy?: SortBy<HierarchicalMenuItem>;
/**
Expand Down Expand Up @@ -174,7 +178,7 @@ const connectHierarchicalMenu: HierarchicalMenuConnector = function connectHiera
limit = 10,
showMore = false,
showMoreLimit = 20,
sortBy = ['name:asc'],
sortBy = DEFAULT_SORT,
transformItems = (items => items) as TransformItems<HierarchicalMenuItem>,
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
} = widgetParams || {};

Expand Down Expand Up @@ -273,11 +277,6 @@ const connectHierarchicalMenu: HierarchicalMenuConnector = function connectHiera
);
},

/**
* @param {Object} param0 cleanup arguments
* @param {any} param0.state current search parameters
* @returns {any} next search parameters
*/
dispose({ state }) {
unmountFn();

Expand Down Expand Up @@ -336,6 +335,7 @@ const connectHierarchicalMenu: HierarchicalMenuConnector = function connectHiera
if (results) {
const facetValues = results.getFacetValues(hierarchicalFacetName, {
sortBy,
facetOrdering: sortBy === DEFAULT_SORT,
});
const facetItems =
facetValues && !Array.isArray(facetValues) && facetValues.data
Expand Down
Loading