Skip to content

Commit

Permalink
feat(connectPagination): update getWidgetSearchParameters (#4004)
Browse files Browse the repository at this point in the history
* test(connectPagination): remove routing describe

* refactor(connectPagination): sets default value with getWidgetSearchParameters

* refactor(connectPagination): remove equality check
  • Loading branch information
samouss authored and Haroenv committed Oct 23, 2019
1 parent 8373529 commit eed7e77
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 134 deletions.

This file was deleted.

155 changes: 66 additions & 89 deletions src/connectors/pagination/__tests__/connectPagination-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ import algoliasearchHelper, {
import connectPagination from '../connectPagination';

describe('connectPagination', () => {
const getInitializedWidget = () => {
const renderFn = jest.fn();
const makeWidget = connectPagination(renderFn);
const widget = makeWidget({});

const helper = algoliasearchHelper({}, '');
helper.search = jest.fn();

widget.init({
helper,
state: helper.state,
createURL: () => '#',
});

const { refine } = renderFn.mock.calls[0][0];

return [widget, helper, refine];
};

describe('Usage', () => {
it('throws without render function', () => {
expect(() => {
Expand Down Expand Up @@ -361,113 +380,71 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/pagination/
});
});

describe('routing', () => {
const getInitializedWidget = () => {
const renderFn = jest.fn();
const makeWidget = connectPagination(renderFn);
const widget = makeWidget({});
describe('getWidgetState', () => {
test('returns the `uiState` emtpy', () => {
const [widget, helper] = getInitializedWidget();

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

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

const { refine } = renderFn.mock.calls[0][0];
test('returns the `uiState` with a refinement', () => {
const [widget, helper] = getInitializedWidget();

return [widget, helper, refine];
};
helper.setQueryParameter('page', 4);

describe('getWidgetState', () => {
test('should give back the object unmodified if the default value is selected', () => {
const [widget, helper] = getInitializedWidget();
const uiStateBefore = {};
const uiStateAfter = widget.getWidgetState(uiStateBefore, {
const actual = widget.getWidgetState(
{},
{
searchParameters: helper.state,
helper,
});
}
);

expect(uiStateAfter).toBe(uiStateBefore);
expect(actual).toEqual({
page: 5, // page + 1
});
});
});

test('should add an entry equal to the refinement', () => {
const [widget, helper, refine] = getInitializedWidget();
refine(4);
const uiStateBefore = {};
const uiStateAfter = widget.getWidgetState(uiStateBefore, {
searchParameters: helper.state,
helper,
});
describe('getWidgetSearchParameters', () => {
test('returns the `SearchParameters` with the value from `uiState`', () => {
const [widget, helper] = getInitializedWidget();

expect(uiStateAfter).toMatchSnapshot();
});

test('should give back the object unmodified if refinements are already set', () => {
const [widget, helper, refine] = getInitializedWidget();
refine(4);
const uiStateBefore = {
const actual = widget.getWidgetSearchParameters(helper.state, {
uiState: {
page: 5,
};
const uiStateAfter = widget.getWidgetState(uiStateBefore, {
searchParameters: helper.state,
helper,
});

expect(uiStateAfter).toBe(uiStateBefore);
},
});

expect(actual).toEqual(
new SearchParameters({
index: '',
page: 4, // uiState.page - 1
})
);
});

describe('getWidgetSearchParameters', () => {
test('should return the same SP if there are no refinements in the UI state', () => {
const [widget, helper] = getInitializedWidget();
// The user presses back (browser), and the URL contains no parameters
const uiState = {};
// The current state is empty (and page is set to 0 by default)
const searchParametersBefore = SearchParameters.make(helper.state);
const searchParametersAfter = widget.getWidgetSearchParameters(
searchParametersBefore,
{ uiState }
);
// Applying the same values should not return a new object
expect(searchParametersAfter).toBe(searchParametersBefore);
});
test('returns the `SearchParameters` with the default value', () => {
const [widget, helper] = getInitializedWidget();

test('should enforce the default value if no value is in the UI State', () => {
const [widget, helper, refine] = getInitializedWidget();
// The user presses back (browser), and the URL contains no parameters
const uiState = {};
// The current state is set to page 4
refine(4);
const searchParametersBefore = SearchParameters.make(helper.state);
const searchParametersAfter = widget.getWidgetSearchParameters(
searchParametersBefore,
{ uiState }
);
// Applying an empty state, should force back to page 0
expect(searchParametersAfter).toMatchSnapshot();
expect(searchParametersAfter.page).toBeUndefined();
const actual = widget.getWidgetSearchParameters(helper.state, {
uiState: {},
});

test('should add the refinements according to the UI state provided', () => {
const [widget, helper, refine] = getInitializedWidget();
// The user presses back (browser), and the URL contains some parameters
const uiState = {
page: 2,
};
// The current search is set to page 10
refine(10);
const searchParametersBefore = SearchParameters.make(helper.state);
const searchParametersAfter = widget.getWidgetSearchParameters(
searchParametersBefore,
{ uiState }
);
// Applying a state with new parameters should apply them on the search
expect(searchParametersAfter).toMatchSnapshot();
expect(searchParametersAfter.page).toBe(1);
});
expect(actual).toEqual(
new SearchParameters({
index: '',
page: 0,
})
);
});
});
});
10 changes: 3 additions & 7 deletions src/connectors/pagination/connectPagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export default function connectPagination(renderFn, unmountFn = noop) {
getWidgetState(uiState, { searchParameters }) {
const page = searchParameters.page || 0;

if (page === 0 || page + 1 === uiState.page) {
if (!page) {
return uiState;
}

Expand All @@ -180,13 +180,9 @@ export default function connectPagination(renderFn, unmountFn = noop) {
},

getWidgetSearchParameters(searchParameters, { uiState }) {
const uiPage = uiState.page;
const page = uiState.page ? uiState.page - 1 : 0;

if (uiPage) {
return searchParameters.setQueryParameter('page', uiState.page - 1);
}

return searchParameters.setQueryParameter('page', undefined);
return searchParameters.setQueryParameter('page', page);
},
};
};
Expand Down

0 comments on commit eed7e77

Please sign in to comment.