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

Commit

Permalink
migrate connectQueryRules
Browse files Browse the repository at this point in the history
  • Loading branch information
Haroenv committed Apr 15, 2019
1 parent 75ab56e commit 2915186
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -451,36 +451,39 @@ Consider using \`transformRuleContexts\` to minimize the number of rules sent to
});
});

describe.skip('multi index', () => {
describe('multi index', () => {
const firstIndexName = 'firstIndex';
const secondIndexName = 'secondIndex';
const context = {
context: {
ais: { mainTargetedIndex: firstIndexName },
multiIndexContext: { targetedIndex: secondIndexName },
},

const contextValue: any = { mainTargetedIndex: firstIndexName };
const indexContextValue: any = { targetedIndex: secondIndexName };

const defaultPropsMultiIndex = {
...defaultProps,
contextValue,
indexContextValue,
};
const getProvidedProps = connect.getProvidedProps.bind(context);
const getSearchParameters = connect.getSearchParameters.bind(context);

it('without userData provides the correct props to the component', () => {
const props: QueryRulesProps = {
...defaultProps,
const props: ConnectedProps<QueryRulesProps> = {
...defaultPropsMultiIndex,
};
const searchState = {};
const searchResults = {
results: { [secondIndexName]: { userData: undefined } },
};

expect(getProvidedProps(props, searchState, searchResults)).toEqual({
expect(
connect.getProvidedProps(props, searchState, searchResults)
).toEqual({
items: [],
canRefine: false,
});
});

it('with userData provides the correct props to the component', () => {
const props: QueryRulesProps = {
...defaultProps,
const props: ConnectedProps<QueryRulesProps> = {
...defaultPropsMultiIndex,
};
const searchState = {};
const searchResults = {
Expand All @@ -489,7 +492,9 @@ Consider using \`transformRuleContexts\` to minimize the number of rules sent to
},
};

expect(getProvidedProps(props, searchState, searchResults)).toEqual({
expect(
connect.getProvidedProps(props, searchState, searchResults)
).toEqual({
items: [{ banner: 'image.png' }],
canRefine: true,
});
Expand All @@ -500,8 +505,8 @@ Consider using \`transformRuleContexts\` to minimize the number of rules sent to
const transformItemsSpy = jest.fn(() => [
{ banner: 'image-transformed.png' },
]);
const props: QueryRulesProps = {
...defaultProps,
const props: ConnectedProps<QueryRulesProps> = {
...defaultPropsMultiIndex,
transformItems: transformItemsSpy,
};
const searchState = {};
Expand All @@ -510,7 +515,9 @@ Consider using \`transformRuleContexts\` to minimize the number of rules sent to
[secondIndexName]: { userData: [{ banner: 'image.png' }] },
},
};
expect(getProvidedProps(props, searchState, searchResults)).toEqual({
expect(
connect.getProvidedProps(props, searchState, searchResults)
).toEqual({
items: [{ banner: 'image-transformed.png' }],
canRefine: true,
});
Expand All @@ -523,11 +530,11 @@ Consider using \`transformRuleContexts\` to minimize the number of rules sent to

describe('trackedFilters', () => {
it('does not set ruleContexts without search state and trackedFilters', () => {
const props: QueryRulesProps = {
...defaultProps,
const props: ConnectedProps<QueryRulesProps> = {
...defaultPropsMultiIndex,
};
const searchState = {};
const searchParameters = getSearchParameters(
const searchParameters = connect.getSearchParameters(
new SearchParameters(),
props,
searchState
Expand All @@ -537,8 +544,8 @@ Consider using \`transformRuleContexts\` to minimize the number of rules sent to
});

it('does not set ruleContexts with search state but without tracked filters', () => {
const props: QueryRulesProps = {
...defaultProps,
const props: ConnectedProps<QueryRulesProps> = {
...defaultPropsMultiIndex,
};
const searchState = {
indices: {
Expand All @@ -552,7 +559,7 @@ Consider using \`transformRuleContexts\` to minimize the number of rules sent to
},
},
};
const searchParameters = getSearchParameters(
const searchParameters = connect.getSearchParameters(
new SearchParameters(),
props,
searchState
Expand All @@ -563,8 +570,8 @@ Consider using \`transformRuleContexts\` to minimize the number of rules sent to

it('sets ruleContexts based on range', () => {
const priceSpy = jest.fn(values => values);
const props: QueryRulesProps = {
...defaultProps,
const props: ConnectedProps<QueryRulesProps> = {
...defaultPropsMultiIndex,
trackedFilters: {
price: priceSpy,
},
Expand All @@ -581,7 +588,7 @@ Consider using \`transformRuleContexts\` to minimize the number of rules sent to
},
},
};
const searchParameters = getSearchParameters(
const searchParameters = connect.getSearchParameters(
new SearchParameters(),
props,
searchState
Expand All @@ -599,8 +606,8 @@ Consider using \`transformRuleContexts\` to minimize the number of rules sent to
describe('transformRuleContexts', () => {
it('transform rule contexts before adding them to search parameters', () => {
const priceSpy = jest.fn(values => values);
const props: QueryRulesProps = {
...defaultProps,
const props: ConnectedProps<QueryRulesProps> = {
...defaultPropsMultiIndex,
trackedFilters: {
price: priceSpy,
},
Expand All @@ -619,7 +626,7 @@ Consider using \`transformRuleContexts\` to minimize the number of rules sent to
},
},
};
const searchParameters = getSearchParameters(
const searchParameters = connect.getSearchParameters(
new SearchParameters(),
props,
searchState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ export default createConnector({
_1: any,
searchResults: any
) {
const results = getResults(searchResults, { ais: props.contextValue });
const results = getResults(searchResults, {
ais: props.contextValue,
multiIndexContext: props.indexContextValue,
});

if (results === null) {
return {
Expand Down Expand Up @@ -150,8 +153,16 @@ export default createConnector({
return searchParameters;
}

const indexSearchState = hasMultipleIndices({ ais: props.contextValue })
? searchState.indices[getIndexId({ ais: props.contextValue })]
const indexSearchState = hasMultipleIndices({
ais: props.contextValue,
multiIndexContext: props.indexContextValue,
})
? searchState.indices[
getIndexId({
ais: props.contextValue,
multiIndexContext: props.indexContextValue,
})
]
: searchState;

const newRuleContexts = getRuleContextsFromTrackedFilters({
Expand Down

0 comments on commit 2915186

Please sign in to comment.