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

Commit

Permalink
fix(MultiIndex): revert breaking change if no multiple index (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthuret authored Apr 13, 2017
1 parent bcabf9b commit 44f7de0
Show file tree
Hide file tree
Showing 22 changed files with 143 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getIndex,
refineValue,
getCurrentRefinementValue,
getResults,
} from '../core/indexUtils';

export const getId = props => props.attributes[0];
Expand Down Expand Up @@ -171,12 +172,11 @@ export default createConnector({
getProvidedProps(props, searchState, searchResults) {
const { showMore, limitMin, limitMax } = props;
const id = getId(props);
const { results } = searchResults;
const index = getIndex(this.context);
const results = getResults(searchResults, this.context);

const isFacetPresent = Boolean(results) &&
Boolean(results[index]) &&
Boolean(results[index].getFacetByName(id));
Boolean(results.getFacetByName(id));

if (!isFacetPresent) {
return {
Expand All @@ -191,7 +191,7 @@ export default createConnector({
}

const limit = showMore ? limitMax : limitMin;
const value = results[index].getFacetValues(id, { sortBy });
const value = results.getFacetValues(id, { sortBy });
const items = value.data
? transformValue(value.data, limit, props, searchState, this.context)
: [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ describe('connectHierarchicalMenu', () => {

it('provides the correct props to the component', () => {
const results = {
index: {
getFacetValues: jest.fn(),
getFacetByName: () => true,
},
getFacetValues: jest.fn(),
getFacetByName: () => true,
};

results.index.getFacetValues.mockImplementationOnce(() => ({}));
results.getFacetValues.mockImplementationOnce(() => ({}));
props = getProvidedProps(
{ attributes: ['ok'] },
{ hierarchicalMenu: { ok: 'wat' } },
Expand All @@ -44,8 +42,8 @@ describe('connectHierarchicalMenu', () => {
items: [],
});

results.index.getFacetValues.mockClear();
results.index.getFacetValues.mockImplementation(() => ({
results.getFacetValues.mockClear();
results.getFacetValues.mockImplementation(() => ({
data: [
{
name: 'wat',
Expand Down
7 changes: 3 additions & 4 deletions packages/react-instantsearch/src/connectors/connectHits.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import createConnector from '../core/createConnector';
import { getIndex } from '../core/indexUtils';
import { getIndex, getResults } from '../core/indexUtils';

/**
* connectHits connector provides the logic to create connected
Expand All @@ -18,9 +18,8 @@ export default createConnector({

getProvidedProps(props, searchState, searchResults) {
const index = getIndex(this.context);
const hits = searchResults.results && searchResults.results[index]
? searchResults.results[index].hits
: [];
const results = getResults(searchResults, this.context);
const hits = results ? results.hits : [];

return { hits };
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ describe('connectHits', () => {
it('provides the current hits to the component', () => {
const hits = [{}];
const props = getProvidedProps(null, null, {
results: { index: { hits } },
results: { hits },
});
expect(props).toEqual({ hits });
});

it("doesn't render when no hits are available", () => {
const props = getProvidedProps(null, null, { results: { index: null } });
const props = getProvidedProps(null, null, { results: null });
expect(props).toEqual({ hits: [] });
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getIndex,
getCurrentRefinementValue,
refineValue,
getResults,
} from '../core/indexUtils';

function getId() {
Expand Down Expand Up @@ -42,15 +43,17 @@ export default createConnector({

getProvidedProps(props, searchState, searchResults) {
const index = getIndex(this.context);
if (!searchResults.results || !searchResults.results[index]) {
const results = getResults(searchResults, this.context);

if (!results) {
this._allResults = [];
return {
hits: this._allResults,
hasMore: false,
};
}

const { hits, page, nbPages, hitsPerPage } = searchResults.results[index];
const { hits, page, nbPages, hitsPerPage } = results;

if (page === 0) {
this._allResults = hits;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('connectInfiniteHits', () => {
it('provides the current hits to the component', () => {
const hits = [{}];
const props = getProvidedProps(null, null, {
results: { index: { hits, page: 0, hitsPerPage: 2, nbPages: 3 } },
results: { hits, page: 0, hitsPerPage: 2, nbPages: 3 },
});
expect(props).toEqual({ hits, hasMore: true });
});
Expand All @@ -20,13 +20,16 @@ describe('connectInfiniteHits', () => {
const hits = [{}, {}];
const hits2 = [{}, {}];
const res1 = getProvidedProps(null, null, {
results: { index: { hits, page: 0, hitsPerPage: 2, nbPages: 3 } },
results: { hits, page: 0, hitsPerPage: 2, nbPages: 3 },
});
expect(res1.hits).toEqual(hits);
expect(res1.hasMore).toBe(true);
const res2 = getProvidedProps(null, null, {
results: {
index: { hits: hits2, page: 1, hitsPerPage: 2, nbPages: 3 },
hits: hits2,
page: 1,
hitsPerPage: 2,
nbPages: 3,
},
});
expect(res2.hits).toEqual([...hits, ...hits2]);
Expand All @@ -42,12 +45,10 @@ describe('connectInfiniteHits', () => {
allHits = [...allHits, ...hits];
const res = getProvidedProps(null, null, {
results: {
index: {
hits,
page,
hitsPerPage: hits.length,
nbPages,
},
hits,
page,
hitsPerPage: hits.length,
nbPages,
},
});
expect(res.hits).toEqual(allHits);
Expand All @@ -59,12 +60,10 @@ describe('connectInfiniteHits', () => {
allHits = [...allHits, ...hits];
const res = getProvidedProps(null, null, {
results: {
index: {
hits,
page: nbPages - 1,
hitsPerPage: hits.length,
nbPages,
},
hits,
page: nbPages - 1,
hitsPerPage: hits.length,
nbPages,
},
});
expect(res.hits.length).toEqual(nbPages * 2);
Expand All @@ -77,16 +76,22 @@ describe('connectInfiniteHits', () => {
const hits2 = [{}, {}];
const hits3 = [{}];
getProvidedProps(null, null, {
results: { index: { hits, page: 0, hitsPerPage: 2, nbPages: 3 } },
results: { hits, page: 0, hitsPerPage: 2, nbPages: 3 },
});
getProvidedProps(null, null, {
results: {
index: { hits: hits2, page: 1, hitsPerPage: 2, nbPages: 3 },
hits: hits2,
page: 1,
hitsPerPage: 2,
nbPages: 3,
},
});
const props = getProvidedProps(null, null, {
results: {
index: { hits: hits3, page: 2, hitsPerPage: 2, nbPages: 3 },
hits: hits3,
page: 2,
hitsPerPage: 2,
nbPages: 3,
},
});
expect(props.hits).toEqual([...hits, ...hits2, ...hits3]);
Expand Down
8 changes: 4 additions & 4 deletions packages/react-instantsearch/src/connectors/connectMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getIndex,
refineValue,
getCurrentRefinementValue,
getResults,
} from '../core/indexUtils';

const namespace = 'menu';
Expand Down Expand Up @@ -96,14 +97,13 @@ export default createConnector({
meta,
searchForFacetValuesResults
) {
const { results } = searchResults;
const { attributeName, showMore, limitMin, limitMax } = props;
const limit = showMore ? limitMax : limitMin;
const index = getIndex(this.context);
const results = getResults(searchResults, this.context);

const canRefine = Boolean(results) &&
Boolean(results[index]) &&
Boolean(results[index].getFacetByName(attributeName));
Boolean(results.getFacetByName(attributeName));

const isFromSearch = Boolean(
searchForFacetValuesResults &&
Expand Down Expand Up @@ -148,7 +148,7 @@ export default createConnector({
count: v.count,
isRefined: v.isRefined,
}))
: results[index].getFacetValues(attributeName, { sortBy }).map(v => ({
: results.getFacetValues(attributeName, { sortBy }).map(v => ({
label: v.name,
value: getValue(v.name, props, searchState, this.context),
count: v.count,
Expand Down
28 changes: 11 additions & 17 deletions packages/react-instantsearch/src/connectors/connectMenu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ describe('connectMenu', () => {

it('provides the correct props to the component', () => {
const results = {
index: {
getFacetValues: jest.fn(() => []),
getFacetByName: () => true,
},
getFacetValues: jest.fn(() => []),
getFacetByName: () => true,
};

props = getProvidedProps({ attributeName: 'ok' }, {}, {});
Expand Down Expand Up @@ -85,8 +83,8 @@ describe('connectMenu', () => {
searchForItems: undefined,
});

results.index.getFacetValues.mockClear();
results.index.getFacetValues.mockImplementation(() => [
results.getFacetValues.mockClear();
results.getFacetValues.mockImplementation(() => [
{
name: 'wat',
isRefined: true,
Expand Down Expand Up @@ -210,12 +208,10 @@ describe('connectMenu', () => {

it('if an item is equal to the currentRefinement, its value should be an empty string', () => {
const results = {
index: {
getFacetValues: jest.fn(() => []),
getFacetByName: () => true,
},
getFacetValues: jest.fn(() => []),
getFacetByName: () => true,
};
results.index.getFacetValues.mockImplementation(() => [
results.getFacetValues.mockImplementation(() => [
{
name: 'wat',
isRefined: true,
Expand Down Expand Up @@ -385,13 +381,11 @@ describe('connectMenu', () => {

it('when search for facets values is activated order the item by isRefined first', () => {
const results = {
index: {
getFacetValues: jest.fn(() => []),
getFacetByName: () => true,
},
getFacetValues: jest.fn(() => []),
getFacetByName: () => true,
};
results.index.getFacetValues.mockClear();
results.index.getFacetValues.mockImplementation(() => [
results.getFacetValues.mockClear();
results.getFacetValues.mockImplementation(() => [
{
name: 'wat',
isRefined: false,
Expand Down
12 changes: 7 additions & 5 deletions packages/react-instantsearch/src/connectors/connectMultiRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getIndex,
refineValue,
getCurrentRefinementValue,
getResults,
} from '../core/indexUtils';

import createConnector from '../core/createConnector';
Expand Down Expand Up @@ -125,21 +126,22 @@ export default createConnector({
this.context
);
const index = getIndex(this.context);
const results = getResults(searchResults, this.context);

const items = props.items.map(item => {
const value = stringifyItem(item);
return {
label: item.label,
value,
isRefined: value === currentRefinement,
noRefinement: searchResults.results && searchResults.results[index]
? itemHasRefinement(getId(props), searchResults.results[index], value)
noRefinement: results
? itemHasRefinement(getId(props), results, value)
: false,
};
});

const stats = has(searchResults, `results.${index}`) &&
searchResults.results[index].getFacetByName(attributeName)
? searchResults.results[index].getFacetStats(attributeName)
const stats = results && results.getFacetByName(attributeName)
? results.getFacetStats(attributeName)
: null;
const refinedItem = find(items, item => item.isRefined === true);
if (!items.some(item => item.value === '')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ describe('connectMultiRange', () => {
const cleanUp = connect.cleanUp.bind(context);

const results = {
index: {
getFacetStats: () => ({ min: 0, max: 300 }),
getFacetByName: () => true,
},
getFacetStats: () => ({ min: 0, max: 300 }),
getFacetByName: () => true,
};

it('provides the correct props to the component', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getIndex,
refineValue,
getCurrentRefinementValue,
getResults,
} from '../core/indexUtils';

import createConnector from '../core/createConnector';
Expand Down Expand Up @@ -57,11 +58,13 @@ export default createConnector({

getProvidedProps(props, searchState, searchResults) {
const index = getIndex(this.context);
if (!searchResults.results || !searchResults.results[index]) {
const results = getResults(searchResults, this.context);

if (!results) {
return null;
}

const nbPages = searchResults.results[index].nbPages;
const nbPages = results.nbPages;
return {
nbPages,
currentRefinement: getCurrentRefinement(props, searchState, this.context),
Expand Down
Loading

0 comments on commit 44f7de0

Please sign in to comment.