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

Commit

Permalink
feat(context): migrate base connector [STEP 2]
Browse files Browse the repository at this point in the history
This is enough changes to be able to look at the storybook without errors again (only pagination, hits, searchbox of course, and nothing multi-index)

For full roadmap, see #2178
  • Loading branch information
Haroenv committed Mar 18, 2019
1 parent 50ac800 commit 1656e28
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,61 @@ jest.mock('../../core/createConnector', () => x => x);

describe('connectConfigure', () => {
describe('single index', () => {
const context = { context: { ais: { mainTargetedIndex: 'index' } } };
const getSearchParameters = connect.getSearchParameters.bind(context);
const transitionState = connect.transitionState.bind(context);
const cleanUp = connect.cleanUp.bind(context);
const contextValue = { ais: { mainTargetedIndex: 'index' } };

it('propagates the props to the SearchParameters without children', () => {
const searchParameters = getSearchParameters(
it('propagates the props to the SearchParameters without children & contextValue', () => {
const searchParameters = connect.getSearchParameters(
new SearchParameters(),
{ distinct: 1, whatever: 'please', children: 'whatever' },
{ distinct: 1, whatever: 'please', children: 'whatever', contextValue },
{}
);

expect(searchParameters).toEqual(
expect.objectContaining({
distinct: 1,
whatever: 'please',
})
);
expect(searchParameters).not.toEqual(
expect.objectContaining({
children: 'whatever',
contextValue,
})
);
expect(searchParameters.getQueryParameter('distinct')).toEqual(1);
expect(searchParameters.getQueryParameter('whatever')).toEqual('please');
expect(
searchParameters.getQueryParameter.bind(searchParameters, 'children')
).toThrow();
).toThrowErrorMatchingInlineSnapshot(
`"Parameter 'children' is not an attribute of SearchParameters (http://algolia.github.io/algoliasearch-helper-js/docs/SearchParameters.html)"`
);
expect(
searchParameters.getQueryParameter.bind(
searchParameters,
'contextValue'
)
).toThrowErrorMatchingInlineSnapshot(
`"Parameter 'contextValue' is not an attribute of SearchParameters (http://algolia.github.io/algoliasearch-helper-js/docs/SearchParameters.html)"`
);
});

it('calling transitionState should add configure parameters to the search state', () => {
const providedThis = {};
let searchState = transitionState.call(
providedThis,
{ distinct: 1, whatever: 'please', children: 'whatever' },
let searchState = connect.transitionState(
{
distinct: 1,
whatever: 'please',
children: 'whatever',
contextValue,
},
{},
{}
);
expect(searchState).toEqual({
configure: { distinct: 1, whatever: 'please' },
});

searchState = transitionState.call(
providedThis,
{ whatever: 'other', children: 'whatever' },
searchState = connect.transitionState(
{ whatever: 'other', children: 'whatever', contextValue },
{ configure: { distinct: 1, whatever: 'please' } },
{ configure: { distinct: 1, whatever: 'please' } }
);
Expand All @@ -46,22 +68,32 @@ describe('connectConfigure', () => {
});

it('calling cleanUp should remove configure parameters from the search state', () => {
let searchState = cleanUp(
{ distinct: 1, whatever: 'please', children: 'whatever' },
let searchState = connect.cleanUp(
{
configure: { distinct: 1, whatever: 'please', another: 'parameters' },
distinct: 1,
whatever: 'please',
children: 'whatever',
contextValue,
},
{
configure: {
distinct: 1,
whatever: 'please',
another: 'parameters',
},
}
);
expect(searchState).toEqual({ configure: { another: 'parameters' } });

searchState = cleanUp(
{ distinct: 1, whatever: 'please', children: 'whatever' },
searchState = connect.cleanUp(
{ distinct: 1, whatever: 'please', children: 'whatever', contextValue },
{ configure: { distinct: 1, whatever: 'please' } }
);
expect(searchState).toEqual({ configure: {} });
});
});
describe('multi index', () => {

describe.skip('multi index', () => {
let context = {
context: {
ais: { mainTargetedIndex: 'first' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ const { getSearchParameters } = connect;

describe('connectHits', () => {
describe('single index', () => {
const context = { context: { ais: { mainTargetedIndex: 'index' } } };
const getProvidedProps = connect.getProvidedProps.bind(context);
const contextValue = { mainTargetedIndex: 'index' };

it('provides the current hits to the component', () => {
const hits = [{}];
const props = getProvidedProps(null, null, {
const props = connect.getProvidedProps({ contextValue }, null, {
results: { hits },
});
expect(props).toEqual({ hits });
});

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

Expand All @@ -26,7 +28,8 @@ describe('connectHits', () => {
expect(searchParameters).toEqual({ hitsPerPage: 10 });
});
});
describe('multi index', () => {

describe.skip('multi index', () => {
const context = {
context: {
ais: { mainTargetedIndex: 'first' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@ let params;

describe('connectPagination', () => {
describe('single index', () => {
const context = { context: { ais: { mainTargetedIndex: 'index' } } };
const getProvidedProps = connect.getProvidedProps.bind(context);
const refine = connect.refine.bind(context);
const getSP = connect.getSearchParameters.bind(context);
const getMetadata = connect.getMetadata.bind(context);
const cleanUp = connect.cleanUp.bind(context);
const contextValue = { mainTargetedIndex: 'index' };

it('provides the correct props to the component', () => {
props = getProvidedProps({}, {}, { results: { nbPages: 666, hits: [] } });
props = connect.getProvidedProps(
{ contextValue },
{},
{ results: { nbPages: 666, hits: [] } }
);
expect(props).toEqual({
currentRefinement: 1,
nbPages: 666,
canRefine: true,
});

props = getProvidedProps(
{},
props = connect.getProvidedProps(
{ contextValue },
{ page: 5 },
{ results: { nbPages: 666, hits: [] } }
);
Expand All @@ -34,8 +33,8 @@ describe('connectPagination', () => {
canRefine: true,
});

props = getProvidedProps(
{},
props = connect.getProvidedProps(
{ contextValue },
{ page: '5' },
{ results: { nbPages: 666, hits: [] } }
);
Expand All @@ -45,8 +44,8 @@ describe('connectPagination', () => {
canRefine: true,
});

props = getProvidedProps(
{},
props = connect.getProvidedProps(
{ contextValue },
{ page: '1' },
{ results: { nbPages: 1, hits: [] } }
);
Expand All @@ -58,12 +57,12 @@ describe('connectPagination', () => {
});

it("doesn't render when no results are available", () => {
props = getProvidedProps({}, {}, {});
props = connect.getProvidedProps({ contextValue }, {}, {});
expect(props).toBe(null);
});

it("calling refine updates the widget's search state", () => {
const nextState = refine({}, { otherKey: 'val' }, 'yep');
const nextState = connect.refine({}, { otherKey: 'val' }, 'yep');
expect(nextState).toEqual({
otherKey: 'val',
page: 'yep',
Expand All @@ -72,18 +71,22 @@ describe('connectPagination', () => {

it('refines the page parameter', () => {
const initSP = new SearchParameters();
params = getSP(initSP, {}, { page: 667 });
params = connect.getSearchParameters(
initSP,
{ contextValue },
{ page: 667 }
);
expect(params.page).toBe(666);
});

it('registers its id in metadata', () => {
const metadata = getMetadata({}, {});
const metadata = connect.getMetadata({ contextValue }, {});
expect(metadata).toEqual({ id: 'page' });
});

it('should return the right searchState when clean up', () => {
const newState = cleanUp(
{},
const newState = connect.cleanUp(
{ contextValue },
{
page: { searchState: 'searchState' },
another: { searchState: 'searchState' },
Expand All @@ -92,7 +95,8 @@ describe('connectPagination', () => {
expect(newState).toEqual({ another: { searchState: 'searchState' } });
});
});
describe('multi index', () => {

describe.skip('multi index', () => {
let context = {
context: {
ais: { mainTargetedIndex: 'first' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ let params;

describe('connectSearchBox', () => {
describe('single index', () => {
const context = { context: { ais: { mainTargetedIndex: 'index' } } };
const getProvidedProps = connect.getProvidedProps.bind(context);
const refine = connect.refine.bind(context);
const getSP = connect.getSearchParameters.bind(context);
const cleanUp = connect.cleanUp.bind(context);
const contextValue = { mainTargetedIndex: 'index' };

it('provides the correct props to the component', () => {
props = getProvidedProps({}, {}, {});
props = connect.getProvidedProps({ contextValue }, {}, {});
expect(props).toEqual({ currentRefinement: '' });

props = getProvidedProps({}, { query: 'yep' }, {});
props = connect.getProvidedProps({ contextValue }, { query: 'yep' }, {});
expect(props).toEqual({ currentRefinement: 'yep' });
});

it("calling refine updates the widget's search state", () => {
const nextState = refine({}, { otherKey: 'val' }, 'yep');
const nextState = connect.refine(
{ contextValue },
{ otherKey: 'val' },
'yep'
);
expect(nextState).toEqual({
otherKey: 'val',
page: 1,
Expand All @@ -31,19 +32,29 @@ describe('connectSearchBox', () => {
});

it('supports defaultRefinement', () => {
expect(getProvidedProps({ defaultRefinement: 'yaw' }, {}, {})).toEqual({
expect(
connect.getProvidedProps(
{ defaultRefinement: 'yaw', contextValue },
{},
{}
)
).toEqual({
currentRefinement: 'yaw',
});
});

it('refines the query parameter', () => {
params = getSP(new SearchParameters(), {}, { query: 'bar' });
params = connect.getSearchParameters(
new SearchParameters(),
{ contextValue },
{ query: 'bar' }
);
expect(params.query).toBe('bar');
});

it('should return the right searchState when clean up', () => {
const searchState = cleanUp(
{},
const searchState = connect.cleanUp(
{ contextValue },
{
query: { searchState: 'searchState' },
another: { searchState: 'searchState' },
Expand All @@ -52,7 +63,8 @@ describe('connectSearchBox', () => {
expect(searchState).toEqual({ another: { searchState: 'searchState' } });
});
});
describe('multi index', () => {

describe.skip('multi index', () => {
let context = {
context: {
ais: { mainTargetedIndex: 'first' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { omit, difference, keys } from 'lodash';
import { omit, difference } from 'lodash';
import createConnector from '../core/createConnector';
import {
refineValue,
Expand All @@ -16,37 +16,42 @@ export default createConnector({
return {};
},
getSearchParameters(searchParameters, props) {
const items = omit(props, 'children');
const items = omit(props, 'children', 'contextValue');
return searchParameters.setQueryParameters(items);
},
transitionState(props, prevSearchState, nextSearchState) {
const id = getId();
const items = omit(props, 'children');
const items = omit(props, 'children', 'contextValue');
const nonPresentKeys = this._props
? difference(keys(this._props), keys(props))
? difference(Object.keys(this._props), Object.keys(props))
: [];
this._props = props;
const nextValue = {
[id]: { ...omit(nextSearchState[id], nonPresentKeys), ...items },
};
return refineValue(nextSearchState, nextValue, this.context);
return refineValue(nextSearchState, nextValue, { ais: props.contextValue });
},
cleanUp(props, searchState) {
const id = getId();
const indexId = getIndexId(this.context);
const indexId = getIndexId({ ais: props.contextValue });

const subState =
hasMultipleIndices(this.context) && searchState.indices
hasMultipleIndices(props.contextValue) && searchState.indices
? searchState.indices[indexId]
: searchState;

const configureKeys =
subState && subState[id] ? Object.keys(subState[id]) : [];

const configureState = configureKeys.reduce((acc, item) => {
if (!props[item]) {
acc[item] = subState[id][item];
}
return acc;
}, {});

const nextValue = { [id]: configureState };
return refineValue(searchState, nextValue, this.context);

return refineValue(searchState, nextValue, { ais: props.contextValue });
},
});
Loading

0 comments on commit 1656e28

Please sign in to comment.