Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hitsPerPage): support new routing system #4038

Merged
merged 7 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,3 @@ Array [
},
]
`;

exports[`connectHitsPerPage routing getWidgetSearchParameters should add the refinements according to the UI state provided 1`] = `
SearchParameters {
"disjunctiveFacets": Array [],
"disjunctiveFacetsRefinements": Object {},
"facets": Array [],
"facetsExcludes": Object {},
"facetsRefinements": Object {},
"hierarchicalFacets": Array [],
"hierarchicalFacetsRefinements": Object {},
"hitsPerPage": 10,
"index": "",
"numericRefinements": Object {},
"tagRefinements": Array [],
}
`;

exports[`connectHitsPerPage routing getWidgetState should add an entry equal to the refinement 1`] = `
Object {
"hitsPerPage": 10,
}
`;
190 changes: 96 additions & 94 deletions src/connectors/hits-per-page/__tests__/connectHitsPerPage-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,129 +521,131 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits-per-pa
});
});

it('does not throw without the unmount function', () => {
const rendering = jest.fn();
const makeWidget = connectHitsPerPage(rendering);
const widget = makeWidget({
items: [
{ value: 3, label: '3 items per page', default: true },
{ value: 10, label: '10 items per page' },
],
describe('getWidgetState', () => {
test('returns the `uiState` empty', () => {
const render = jest.fn();
const makeWidget = connectHitsPerPage(render);
const helper = algoliasearchHelper({}, 'indexName');
const widget = makeWidget({
items: [
{ value: 3, label: '3 items per page' },
{ value: 10, label: '10 items per page' },
],
});

const actual = widget.getWidgetState(
{},
{
searchParameters: helper.state,
}
);

expect(actual).toEqual({});
});
const helper = algoliasearchHelper({}, '', {});
expect(() => widget.dispose({ helper, state: helper.state })).not.toThrow();
});

describe('routing', () => {
const getInitializedWidget = () => {
const renderFn = jest.fn();
const makeWidget = connectHitsPerPage(renderFn);
test('returns the `uiState` with a refinement', () => {
const render = jest.fn();
const makeWidget = connectHitsPerPage(render);
const helper = algoliasearchHelper({}, 'indexName', {
hitsPerPage: '22',
francoischalifour marked this conversation as resolved.
Show resolved Hide resolved
});
const widget = makeWidget({
items: [
{ value: 3, label: '3 items per page', default: true },
{ value: 3, label: '3 items per page' },
{ value: 10, label: '10 items per page' },
],
});

const helper = algoliasearchHelper(
const actual = widget.getWidgetState(
{},
'',
widget.getConfiguration(new SearchParameters({}))
{
searchParameters: helper.state,
}
);
helper.search = jest.fn();

widget.init({
helper,
state: helper.state,
createURL: () => '#',
expect(actual).toEqual({
hitsPerPage: 22,
});
});
});

const { refine } = renderFn.mock.calls[0][0];
describe('getWidgetSearchParameters', () => {
test('returns the `SearchParameters` with the initial value', () => {
const render = jest.fn();
const makeWidget = connectHitsPerPage(render);
const helper = algoliasearchHelper({}, 'indexName');
const widget = makeWidget({
items: [
{ value: 3, label: '3 items per page' },
{ value: 10, label: '10 items per page' },
],
});

return [widget, helper, refine];
};
const actual = widget.getWidgetSearchParameters(helper.state, {
uiState: {},
});

describe('getWidgetState', () => {
test('should give back the object unmodified if there are no refinements', () => {
const [widget, helper] = getInitializedWidget();
const uiStateBefore = {};
const uiStateAfter = widget.getWidgetState(uiStateBefore, {
searchParameters: helper.state,
helper,
});
expect(actual.hitsPerPage).toEqual(undefined);
francoischalifour marked this conversation as resolved.
Show resolved Hide resolved
});

expect(uiStateAfter).toBe(uiStateBefore);
test('returns the `SearchParameters` with the default value', () => {
const render = jest.fn();
const makeWidget = connectHitsPerPage(render);
const helper = algoliasearchHelper({}, 'indexName');
const widget = makeWidget({
items: [
{ value: 3, label: '3 items per page', default: true },
{ value: 10, label: '10 items per page' },
],
});

test('should not add an entry when the default value is selected', () => {
const [widget, helper] = getInitializedWidget();
helper.setQueryParameter('hitsPerPage', 3);
const uiStateBefore = {};
const uiStateAfter = widget.getWidgetState(uiStateBefore, {
searchParameters: helper.state,
helper,
});

expect(uiStateAfter).toBe(uiStateBefore);
const actual = widget.getWidgetSearchParameters(helper.state, {
uiState: {},
});

test('should add an entry equal to the refinement', () => {
const [widget, helper] = getInitializedWidget();
helper.setQueryParameter('hitsPerPage', 10);
const uiStateBefore = {};
const uiStateAfter = widget.getWidgetState(uiStateBefore, {
searchParameters: helper.state,
helper,
});
expect(actual.hitsPerPage).toEqual(3);
});

expect(uiStateAfter).toMatchSnapshot();
test('returns the `SearchParameters` with the value from `uiState`', () => {
const render = jest.fn();
const makeWidget = connectHitsPerPage(render);
const helper = algoliasearchHelper({}, 'indexName');
const widget = makeWidget({
items: [
{ value: 3, label: '3 items per page', default: true },
{ value: 10, label: '10 items per page' },
],
});

test('should give back the object unmodified if refinements are already set', () => {
const [widget, helper] = getInitializedWidget();
const uiStateBefore = {
hitsPerPage: 10,
};
helper.setQueryParameter('hitsPerPage', 10);
const uiStateAfter = widget.getWidgetState(uiStateBefore, {
searchParameters: helper.state,
helper,
});

expect(uiStateAfter).toBe(uiStateBefore);
const actual = widget.getWidgetSearchParameters(helper.state, {
uiState: {
hitsPerPage: 22,
},
});

expect(actual.hitsPerPage).toEqual(22);
});

describe('getWidgetSearchParameters', () => {
test('should return the same SP if there are no refinements in the UI state', () => {
const [widget, helper] = getInitializedWidget();
// The URL contains nothing
const uiState = {};
// The current search is empty
const searchParametersBefore = SearchParameters.make(helper.state);
const searchParametersAfter = widget.getWidgetSearchParameters(
searchParametersBefore,
{ uiState }
);
// Applying the empty UI state should not create a new object
expect(searchParametersAfter).toBe(searchParametersBefore);
test('returns the `SearchParameters` with the value from `uiState` without the previous refinement', () => {
const render = jest.fn();
const makeWidget = connectHitsPerPage(render);
const helper = algoliasearchHelper({}, 'indexName', {
hitsPerPage: 22,
});
const widget = makeWidget({
items: [
{ value: 3, label: '3 items per page' },
{ value: 10, label: '10 items per page' },
],
});

test('should add the refinements according to the UI state provided', () => {
const [widget, helper] = getInitializedWidget();
// The URL contains some values for the widget
const uiState = {
hitsPerPage: 10,
};
// Current the search is empty
const searchParametersBefore = SearchParameters.make(helper.state);
const searchParametersAfter = widget.getWidgetSearchParameters(
searchParametersBefore,
{ uiState }
);
// Applying the UI state should add the new configuration
expect(searchParametersAfter).toMatchSnapshot();
const actual = widget.getWidgetSearchParameters(helper.state, {
uiState: {
hitsPerPage: 33,
},
});

expect(actual.hitsPerPage).toEqual(33);
});
});
});
38 changes: 14 additions & 24 deletions src/connectors/hits-per-page/connectHitsPerPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
checkRendering,
warning,
createDocumentationMessageGenerator,
find,
noop,
} from '../../lib/utils';

Expand Down Expand Up @@ -108,25 +107,26 @@ export default function connectHitsPerPage(renderFn, unmountFn = noop) {
);
}

const defaultValues = items.filter(item => item.default);
if (defaultValues.length > 1) {
const defaultItems = items.filter(item => item.default === true);

if (defaultItems.length > 1) {
throw new Error(
withUsage('More than one default value is specified in `items`.')
);
}

const defaultValue = find(userItems, item => item.default === true);
const defaultItem = defaultItems[0];

return {
$$type: 'ais.hitsPerPage',

getConfiguration(state) {
if (!defaultValue) {
if (!defaultItem) {
return state;
}

return state.setQueryParameters({
hitsPerPage: state.hitsPerPage || defaultValue.value,
hitsPerPage: state.hitsPerPage || defaultItem.value,
});
},

Expand Down Expand Up @@ -214,11 +214,8 @@ You may want to add another entry to the \`items\` option with this value.`

getWidgetState(uiState, { searchParameters }) {
const hitsPerPage = searchParameters.hitsPerPage;
if (
(defaultValue && hitsPerPage === defaultValue.value) ||
hitsPerPage === undefined ||
uiState.hitsPerPage === hitsPerPage
) {

if (hitsPerPage === undefined) {
return uiState;
}

Expand All @@ -229,19 +226,12 @@ You may want to add another entry to the \`items\` option with this value.`
},

getWidgetSearchParameters(searchParameters, { uiState }) {
const hitsPerPage = uiState.hitsPerPage;
if (hitsPerPage)
return searchParameters.setQueryParameter(
'hitsPerPage',
uiState.hitsPerPage
);
if (defaultValue) {
return searchParameters.setQueryParameter(
'hitsPerPage',
defaultValue.value
);
}
return searchParameters.setQueryParameter('hitsPerPage', undefined);
const hitsPerPage =
uiState.hitsPerPage || (defaultItem && defaultItem.value);

return searchParameters.setQueryParameters({
hitsPerPage,
samouss marked this conversation as resolved.
Show resolved Hide resolved
});
},
};
};
Expand Down