Skip to content

Commit

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

* refactor(connectSearchBox): sets default value with getWidgetSearchParameters
  • Loading branch information
samouss authored and Haroenv committed Oct 23, 2019
1 parent c77cf66 commit 5c6fcd8
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`connectSearchBox 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 {},
"index": "",
"numericRefinements": Object {},
"query": "some search",
"tagRefinements": Array [],
}
`;

exports[`connectSearchBox routing getWidgetSearchParameters should enforce the default value if no value is the ui state 1`] = `
SearchParameters {
"disjunctiveFacets": Array [],
"disjunctiveFacetsRefinements": Object {},
"facets": Array [],
"facetsExcludes": Object {},
"facetsRefinements": Object {},
"hierarchicalFacets": Array [],
"hierarchicalFacetsRefinements": Object {},
"index": "",
"numericRefinements": Object {},
"tagRefinements": Array [],
}
`;

exports[`connectSearchBox routing getWidgetState should add an entry equal to the refinement 1`] = `
exports[`connectSearchBox getWidgetState should add an entry equal to the refinement 1`] = `
Object {
"query": "some query",
}
Expand Down
172 changes: 89 additions & 83 deletions src/connectors/search-box/__tests__/connectSearchBox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ import algoliasearchHelper, {
import connectSearchBox from '../connectSearchBox';

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

const initialConfig = {};
const helper = algoliasearchHelper({}, '', initialConfig);
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 @@ -369,105 +391,89 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/search-box/
});
});

describe('routing', () => {
const getInitializedWidget = (config = {}) => {
const renderFn = jest.fn();
const makeWidget = connectSearchBox(renderFn);
const widget = makeWidget({
...config,
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, {
searchParameters: helper.state,
helper,
});
expect(uiStateAfter).toBe(uiStateBefore);
});

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

widget.init({
test('should add an entry equal to the refinement', () => {
const [widget, helper, refine] = getInitializedWidget();
refine('some query');
const uiStateBefore = {};
const uiStateAfter = widget.getWidgetState(uiStateBefore, {
searchParameters: helper.state,
helper,
state: helper.state,
createURL: () => '#',
});
expect(uiStateAfter).toMatchSnapshot();
});

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

return [widget, helper, refine];
};

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, {
test('should give back the same instance if the value is alreay in the uiState', () => {
const [widget, helper, refine] = getInitializedWidget();
refine('query');
const uiStateBefore = widget.getWidgetState(
{},
{
searchParameters: helper.state,
helper,
});
expect(uiStateAfter).toBe(uiStateBefore);
}
);
const uiStateAfter = widget.getWidgetState(uiStateBefore, {
searchParameters: helper.state,
helper,
});
expect(uiStateAfter).toBe(uiStateBefore);
});
});

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

test('should give back the same instance if the value is alreay in the uiState', () => {
const [widget, helper, refine] = getInitializedWidget();
refine('query');
const uiStateBefore = widget.getWidgetState(
{},
{
searchParameters: helper.state,
helper,
}
);
const uiStateAfter = widget.getWidgetState(uiStateBefore, {
searchParameters: helper.state,
helper,
});
expect(uiStateAfter).toBe(uiStateBefore);
expect(helper.state).toEqual(
new SearchParameters({
index: '',
})
);

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

expect(actual).toEqual(
new SearchParameters({
index: '',
query: 'Apple',
})
);
});

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 default value', () => {
const [widget, helper] = getInitializedWidget();

test('should add the refinements according to the UI state provided', () => {
const [widget, helper] = getInitializedWidget();
const uiState = {
query: 'some search',
};
const searchParametersBefore = SearchParameters.make(helper.state);
const searchParametersAfter = widget.getWidgetSearchParameters(
searchParametersBefore,
{ uiState }
);
expect(searchParametersAfter).toMatchSnapshot();
});
expect(helper.state).toEqual(
new SearchParameters({
index: '',
})
);

test('should enforce the default value if no value is the ui state', () => {
const [widget, helper, refine] = getInitializedWidget();
refine('previous search');
const uiState = {};
const searchParametersBefore = SearchParameters.make(helper.state);
const searchParametersAfter = widget.getWidgetSearchParameters(
searchParametersBefore,
{ uiState }
);
expect(searchParametersAfter).toMatchSnapshot();
const actual = widget.getWidgetSearchParameters(helper.state, {
uiState: {},
});

expect(actual).toEqual(
new SearchParameters({
index: '',
query: '',
})
);
});
});
});
2 changes: 1 addition & 1 deletion src/connectors/search-box/connectSearchBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export default function connectSearchBox(renderFn, unmountFn = noop) {
},

getWidgetSearchParameters(searchParameters, { uiState }) {
return searchParameters.setQueryParameter('query', uiState.query);
return searchParameters.setQueryParameter('query', uiState.query || '');
},
};
};
Expand Down

0 comments on commit 5c6fcd8

Please sign in to comment.