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

refactor(lodash): orderBy #2462

Merged
merged 11 commits into from
Jun 25, 2019
Merged
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,27 +125,27 @@
"bundlesize": [
{
"path": "packages/react-instantsearch/dist/umd/Core.min.js",
"maxSize": "6.50 kB"
"maxSize": "3 kB"
},
{
"path": "packages/react-instantsearch/dist/umd/Connectors.min.js",
"maxSize": "41.80 kB"
"maxSize": "42 kB"
},
{
"path": "packages/react-instantsearch/dist/umd/Dom.min.js",
"maxSize": "52 kB"
},
{
"path": "packages/react-instantsearch-core/dist/umd/ReactInstantSearchCore.min.js",
"maxSize": "42.25 kB"
"maxSize": "42.50 kB"
},
{
"path": "packages/react-instantsearch-dom/dist/umd/ReactInstantSearchDOM.min.js",
"maxSize": "53.25 kB"
},
{
"path": "packages/react-instantsearch-dom-maps/dist/umd/ReactInstantSearchDOMMaps.min.js",
"maxSize": "10.75 kB"
"maxSize": "7.25 kB"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -416,32 +416,78 @@ describe('connectMenu', () => {
});
});

it('when search for facets values is activated order the item by isRefined first', () => {
it('if not searchable: uses a static sortBy order', () => {
const results = {
getFacetValues: jest.fn(() => []),
getFacetValues: jest.fn(() => [
{
name: 'oy',
isRefined: true,
count: 10,
},
{
name: 'wat',
isRefined: false,
count: 20,
},
]),
getFacetByName: () => true,
hits: [],
};
results.getFacetValues.mockClear();
results.getFacetValues.mockImplementation(() => [
{
name: 'wat',
isRefined: false,
count: 20,
},

props = connect.getProvidedProps(
{ attribute: 'ok', contextValue },
{},
{ results }
);

expect(results.getFacetValues).toHaveBeenCalledWith('ok', {
sortBy: ['count:desc', 'name:asc'],
});

expect(props.items).toEqual([
{
name: 'oy',
value: 'oy',
label: 'oy',
isRefined: true,
count: 10,
},
{
value: 'wat',
label: 'wat',
isRefined: false,
count: 20,
},
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
]);
});

it('if searchable: use the default sortBy order', () => {
const results = {
getFacetValues: jest.fn(() => [
{
name: 'oy',
isRefined: true,
count: 10,
},
{
name: 'wat',
isRefined: false,
count: 20,
},
]),
getFacetByName: () => true,
hits: [],
};

props = connect.getProvidedProps(
{ attribute: 'ok', searchable: true, contextValue },
{},
{ results }
);

expect(results.getFacetValues).toHaveBeenCalledWith('ok', {
sortBy: undefined,
});

expect(props.items).toEqual([
{
value: 'oy',
Expand Down
47 changes: 22 additions & 25 deletions packages/react-instantsearch-core/src/connectors/connectMenu.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { orderBy } from 'lodash';
import PropTypes from 'prop-types';
import createConnector from '../core/createConnector';
import {
Expand Down Expand Up @@ -51,7 +50,7 @@ function cleanUp(props, searchState, context) {
return cleanUpValue(searchState, context, `${namespace}.${getId(props)}`);
}

const sortBy = ['count:desc', 'name:asc'];
const defaultSortBy = ['count:desc', 'name:asc'];

/**
* connectMenu connector provides the logic to build a widget that will
Expand Down Expand Up @@ -135,18 +134,24 @@ export default createConnector({
};
}

const items = isFromSearch
? searchForFacetValuesResults[attribute].map(v => ({
label: v.value,
value: getValue(v.value, props, searchState, {
ais: props.contextValue,
multiIndexContext: props.indexContextValue,
}),
_highlightResult: { label: { value: v.highlighted } },
count: v.count,
isRefined: v.isRefined,
}))
: results.getFacetValues(attribute, { sortBy }).map(v => ({
let items;
if (isFromSearch) {
items = searchForFacetValuesResults[attribute].map(v => ({
label: v.value,
value: getValue(v.value, props, searchState, {
ais: props.contextValue,
multiIndexContext: props.indexContextValue,
}),
_highlightResult: { label: { value: v.highlighted } },
count: v.count,
isRefined: v.isRefined,
}));
} else {
items = results
.getFacetValues(attribute, {
sortBy: searchable ? undefined : defaultSortBy,
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
})
.map(v => ({
label: v.name,
value: getValue(v.name, props, searchState, {
ais: props.contextValue,
Expand All @@ -155,19 +160,11 @@ export default createConnector({
count: v.count,
isRefined: v.isRefined,
}));

const sortedItems =
searchable && !isFromSearch
? orderBy(
items,
['isRefined', 'count', 'label'],
['desc', 'desc', 'asc']
)
: items;
}

const transformedItems = props.transformItems
? props.transformItems(sortedItems)
: sortedItems;
? props.transformItems(items)
: items;

return {
items: transformedItems.slice(0, getLimit(props)),
Expand Down
17 changes: 11 additions & 6 deletions packages/react-instantsearch-core/src/core/indexUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ export function getIndexId(context) {
}

export function getResults(searchResults, context) {
if (searchResults.results && !searchResults.results.hits) {
return searchResults.results[getIndexId(context)]
? searchResults.results[getIndexId(context)]
: null;
} else {
return searchResults.results ? searchResults.results : null;
if (searchResults.results) {
if (searchResults.results.hits) {
return searchResults.results;
}

const indexId = getIndexId(context);
if (searchResults.results[indexId]) {
return searchResults.results[indexId];
}
}

return null;
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
}

export function hasMultipleIndices(context) {
Expand Down