Skip to content

Commit

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

* refactor(connectRatingMenu): use Math.min vs for loop

* refactor(connectRatingMenu): sets default value with getWidgetSearchParameters
  • Loading branch information
samouss authored and Haroenv committed Oct 23, 2019
1 parent eed7e77 commit d3c96bf
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 151 deletions.

This file was deleted.

287 changes: 202 additions & 85 deletions src/connectors/rating-menu/__tests__/connectRatingMenu-test.js
Expand Up @@ -5,6 +5,33 @@ import jsHelper, {
import connectRatingMenu from '../connectRatingMenu';

describe('connectRatingMenu', () => {
// @TODO: once we've migrate away from `getConfiguration` update
// the function and use it at least for the lifecycle.
// const getInitializedWidget = (config = {}) => {
// const rendering = jest.fn();
// const makeWidget = connectRatingMenu(rendering);

// const attribute = 'grade';
// const widget = makeWidget({
// attribute,
// ...config,
// });

// const initialConfig = widget.getConfiguration(new SearchParameters({}));
// const helper = jsHelper({}, '', initialConfig);
// helper.search = jest.fn();

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

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

// return [widget, helper, refine];
// };

describe('Usage', () => {
it('throws without render function', () => {
expect(() => {
Expand Down Expand Up @@ -370,116 +397,206 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/rating-menu
});
});

describe('routing', () => {
const getInitializedWidget = (config = {}) => {
const rendering = jest.fn();
const makeWidget = connectRatingMenu(rendering);

const attribute = 'grade';
describe('getWidgetState', () => {
test('returns the `uiState` emtpy', () => {
// Uses the function getInitializedWidget once we've removed `getConfiguration`
const render = () => {};
const makeWidget = connectRatingMenu(render);
const helper = jsHelper({}, '');
const widget = makeWidget({
attribute,
...config,
attribute: 'grade',
});

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

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

const { refine } = rendering.mock.calls[0][0];
test('returns the `uiState` with a refinement', () => {
// Uses the function getInitializedWidget once we've removed `getConfiguration`
const render = () => {};
const makeWidget = connectRatingMenu(render);
const helper = jsHelper({}, '', {
disjunctiveFacets: ['grade'],
disjunctiveFacetsRefinements: {
grade: ['2', '3', '4', '5'],
},
});

return [widget, helper, refine];
};
const widget = makeWidget({
attribute: 'grade',
});

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({
ratingMenu: {
grade: 2,
},
});
});

test('should add an entry equal to the refinement', () => {
const [widget, helper, refine] = getInitializedWidget();
refine('3');
const uiStateBefore = {};
const uiStateAfter = widget.getWidgetState(uiStateBefore, {
searchParameters: helper.state,
helper,
});
expect(uiStateAfter).toMatchSnapshot();
test('returns the `uiState` without namespace overridden', () => {
// Uses the function getInitializedWidget once we've removed `getConfiguration`
const render = () => {};
const makeWidget = connectRatingMenu(render);
const helper = jsHelper({}, '', {
disjunctiveFacets: ['grade'],
disjunctiveFacetsRefinements: {
grade: ['2', '3', '4', '5'],
},
});

test('should give back the object unmodified if the value is already in the UI State', () => {
const [widget, helper, refine] = getInitializedWidget();
refine('3');
const uiStateBefore = widget.getWidgetState(
{},
{
searchParameters: helper.state,
helper,
}
);
const uiStateAfter = widget.getWidgetState(uiStateBefore, {
const widget = makeWidget({
attribute: 'grade',
});

const actual = widget.getWidgetState(
{
ratingMenu: {
rating: 4,
},
},
{
searchParameters: helper.state,
helper,
});
expect(uiStateAfter).toBe(uiStateBefore);
}
);

expect(actual).toEqual({
ratingMenu: {
grade: 2,
rating: 4,
},
});
});
});

describe('getWidgetSearchParameters', () => {
test('returns the `SearchParameters` with the default value', () => {
// Uses the function getInitializedWidget once we've removed `getConfiguration`
const render = () => {};
const makeWidget = connectRatingMenu(render);
const helper = jsHelper({}, '');
const widget = makeWidget({
attribute: 'grade',
});

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

expect(actual).toEqual(
new SearchParameters({
index: '',
disjunctiveFacets: ['grade'],
disjunctiveFacetsRefinements: {
grade: [],
},
})
);
});

test('returns the `SearchParameters` without the previous value', () => {
// Uses the function getInitializedWidget once we've removed `getConfiguration`
const render = () => {};
const makeWidget = connectRatingMenu(render);
const helper = jsHelper({}, '', {
disjunctiveFacetsRefinements: {
grade: ['2', '3', '4', '5'],
},
});

const widget = makeWidget({
attribute: 'grade',
});

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

expect(actual).toEqual(
new SearchParameters({
index: '',
disjunctiveFacets: ['grade'],
disjunctiveFacetsRefinements: {
grade: [],
},
})
);
});

describe('getWidgetSearchParameters', () => {
test('should return the same SP if no value is in the UI state', () => {
const [widget, helper] = getInitializedWidget();
const uiState = {};
const searchParametersBefore = SearchParameters.make(helper.state);
const searchParametersAfter = widget.getWidgetSearchParameters(
searchParametersBefore,
{ uiState }
);
expect(searchParametersAfter).toBe(searchParametersBefore);
test('returns the `SearchParameters` with the value from `uiState`', () => {
// Uses the function getInitializedWidget once we've removed `getConfiguration`
const render = () => {};
const makeWidget = connectRatingMenu(render);
const helper = jsHelper({}, '');
const widget = makeWidget({
attribute: 'grade',
});

test('should add the refinements according to the UI state provided', () => {
const [widget, helper] = getInitializedWidget();
const uiState = {
const actual = widget.getWidgetSearchParameters(helper.state, {
uiState: {
ratingMenu: {
grade: '2',
grade: '3',
},
};
const searchParametersBefore = SearchParameters.make(helper.state);
const searchParametersAfter = widget.getWidgetSearchParameters(
searchParametersBefore,
{ uiState }
);
expect(searchParametersAfter).toMatchSnapshot();
},
});

test('should return the same SP if the value is consistent with the UI state', () => {
const [widget, helper, refine] = getInitializedWidget();
refine('2');
const uiState = widget.getWidgetState(
{},
{
searchParameters: helper.state,
helper,
}
);
const searchParametersBefore = SearchParameters.make(helper.state);
const searchParametersAfter = widget.getWidgetSearchParameters(
searchParametersBefore,
{ uiState }
);
expect(searchParametersAfter).toBe(searchParametersBefore);
expect(actual).toEqual(
new SearchParameters({
index: '',
disjunctiveFacets: ['grade'],
disjunctiveFacetsRefinements: {
grade: ['3', '4', '5'],
},
})
);
});

test('returns the `SearchParameters` with the value from `uiState` without the previous refinement', () => {
const render = () => {};
const makeWidget = connectRatingMenu(render);
const helper = jsHelper({}, '', {
disjunctiveFacets: ['grade'],
disjunctiveFacetsRefinements: {
grade: ['1', '2', '3', '4', '5'],
},
});

const widget = makeWidget({
attribute: 'grade',
});

const actual = widget.getWidgetSearchParameters(helper.state, {
uiState: {
ratingMenu: {
grade: '3',
},
},
});

expect(actual).toEqual(
new SearchParameters({
index: '',
disjunctiveFacets: ['grade'],
disjunctiveFacetsRefinements: {
grade: ['3', '4', '5'],
},
})
);
});
});
});

0 comments on commit d3c96bf

Please sign in to comment.