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(connectRatingMenu): update getWidgetSearchParameters #4008

Merged
merged 4 commits into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all 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

This file was deleted.

287 changes: 202 additions & 85 deletions src/connectors/rating-menu/__tests__/connectRatingMenu-test.js
Original file line number Diff line number Diff line change
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'],
},
})
);
});
});
});
Loading