Skip to content

Commit

Permalink
fix(SFFV): empty query triggered a new SFFV (#1875)
Browse files Browse the repository at this point in the history
Before this commit, when typing in a search for facet values searchbox then removing the query, we would still be in a search state. Now we show the facets of the current search.
  • Loading branch information
mthuret authored and vvo committed Jan 18, 2017
1 parent 73ac421 commit 6c8259a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
7 changes: 5 additions & 2 deletions packages/react-instantsearch/src/connectors/connectMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ export default createConnector({
return null;
}

const items = searchForFacetValuesResults && searchForFacetValuesResults[attributeName]
const isFromSearch = Boolean(searchForFacetValuesResults
&& searchForFacetValuesResults[attributeName]
&& searchForFacetValuesResults.query !== '');

const items = isFromSearch
? searchForFacetValuesResults[attributeName]
.map(
v => ({
Expand All @@ -96,7 +100,6 @@ export default createConnector({
isRefined: v.isRefined,
}));

const isFromSearch = Boolean(searchForFacetValuesResults && searchForFacetValuesResults[attributeName]);
const searchForFacetValues = props.searchForFacetValues ? this.searchForFacetValues : undefined;

const sortedItems = !isFromSearch && props.searchForFacetValues ?
Expand Down
26 changes: 26 additions & 0 deletions packages/react-instantsearch/src/connectors/connectMenu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@ describe('connectMenu', () => {
},
]);

props = getProvidedProps({attributeName: 'ok', limitMin: 1}, {}, {results}, {}, {query: 'query', ok: [{
value: 'wat',
count: 10,
highlighted: 'wat',
isRefined: false,
}]});
expect(props.items).toEqual([
{
value: 'wat',
label: 'wat',
isRefined: false,
count: 10,
_highlightResult: {label: {value: 'wat'}},
},
]);

props = getProvidedProps({attributeName: 'ok', limitMin: 1}, {}, {results}, {}, {query: ''});
expect(props.items).toEqual([
{
value: 'wat',
label: 'wat',
isRefined: true,
count: 20,
},
]);

const transformItems = jest.fn(() => ['items']);
props = getProvidedProps(
{attributeName: 'ok', transformItems},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ export default createConnector({
return null;
}

const items = searchForFacetValuesResults && searchForFacetValuesResults[attributeName]
const isFromSearch = Boolean(searchForFacetValuesResults
&& searchForFacetValuesResults[attributeName]
&& searchForFacetValuesResults.query !== '');

const items = isFromSearch
? searchForFacetValuesResults[attributeName]
.map(v => ({
label: v.value,
Expand All @@ -113,8 +117,8 @@ export default createConnector({
count: v.count,
isRefined: v.isRefined,
}));

const transformedItems = props.transformItems ? props.transformItems(items) : items;
const isFromSearch = Boolean(searchForFacetValuesResults && searchForFacetValuesResults[attributeName]);
const searchForFacetValues = props.searchForFacetValues ? this.searchForFacetValues : undefined;

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,32 @@ describe('connectRefinementList', () => {
},
]);

props = getProvidedProps({attributeName: 'ok', limitMin: 1}, {}, {results}, {}, {query: 'query', ok: [{
value: 'wat',
count: 10,
highlighted: 'wat',
isRefined: false,
}]});
expect(props.items).toEqual([
{
value: ['wat'],
label: 'wat',
isRefined: false,
count: 10,
_highlightResult: {label: {value: 'wat'}},
},
]);

props = getProvidedProps({attributeName: 'ok', limitMin: 1}, {}, {results}, {}, {query: ''});
expect(props.items).toEqual([
{
value: ['wat'],
label: 'wat',
isRefined: true,
count: 20,
},
]);

props = getProvidedProps(
{attributeName: 'ok', showMore: true, limitMin: 0, limitMax: 1},
{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export default function createInstantSearchManager({
resultsFacetValues: {
...store.getState().resultsFacetValues,
[nextSearchState.facetName]: content.facetHits,
query: nextSearchState.query,
},
searchingForFacetValues: false,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('createInstantSearchManager', () => {

return Promise.resolve().then(() => {
const store = ism.store.getState();
expect(store.resultsFacetValues).toEqual({facetName: 'results'});
expect(store.resultsFacetValues).toEqual({facetName: 'results', query: 'query'});
expect(store.searchingForFacetValues).toBe(false);
});
});
Expand Down

0 comments on commit 6c8259a

Please sign in to comment.