Skip to content

Commit

Permalink
feat(connectAutocomplete): add default value on getConfiguration (#3836)
Browse files Browse the repository at this point in the history
* test(connectAutocomplete): scope getConfiguration into its own describe

* feat(connectAutocomplete): mount a default query
  • Loading branch information
samouss authored and Haroenv committed Oct 23, 2019
1 parent 705b3e6 commit 724b83f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
54 changes: 42 additions & 12 deletions src/connectors/autocomplete/__tests__/connectAutocomplete-test.js
Expand Up @@ -43,17 +43,6 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/autocomplet
expect(renderFn.mock.calls[1][1]).toBeFalsy();
});

it('sets the default configuration', () => {
const renderFn = jest.fn();
const makeWidget = connectAutocomplete(renderFn);
const widget = makeWidget();

expect(widget.getConfiguration()).toEqual({
highlightPreTag: TAG_PLACEHOLDER.highlightPreTag,
highlightPostTag: TAG_PLACEHOLDER.highlightPostTag,
});
});

it('creates DerivedHelper', () => {
const renderFn = jest.fn();
const makeWidget = connectAutocomplete(renderFn);
Expand Down Expand Up @@ -164,6 +153,47 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/autocomplet
expect(rendering.indices[0].hits).toEqual(hits);
});

describe('getConfiguration', () => {
it('adds a `query` to the `SearchParameters`', () => {
const renderFn = () => {};
const makeWidget = connectAutocomplete(renderFn);
const widget = makeWidget();

const nextConfiguation = widget.getConfiguration();

expect(nextConfiguation.query).toBe('');
});

it('adds the TAG_PLACEHOLDER to the `SearchParameters`', () => {
const renderFn = () => {};
const makeWidget = connectAutocomplete(renderFn);
const widget = makeWidget();

const nextConfiguation = widget.getConfiguration();

expect(nextConfiguation.highlightPreTag).toBe(
TAG_PLACEHOLDER.highlightPreTag
);

expect(nextConfiguation.highlightPostTag).toBe(
TAG_PLACEHOLDER.highlightPostTag
);
});

it('does not add the TAG_PLACEHOLDER to the `SearchParameters` with `escapeHTML` disabled', () => {
const renderFn = () => {};
const makeWidget = connectAutocomplete(renderFn);
const widget = makeWidget({
escapeHTML: false,
});

const nextConfiguation = widget.getConfiguration();

expect(nextConfiguation.highlightPreTag).toBeUndefined();
expect(nextConfiguation.highlightPostTag).toBeUndefined();
});
});

describe('dispose', () => {
it('calls the unmount function', () => {
const helper = algoliasearchHelper(fakeClient, 'firstIndex');
Expand Down Expand Up @@ -291,7 +321,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/autocomplet
expect(nextState.highlightPostTag).toBeUndefined();
});

it('does not remove the TAG_PLACEHOLDER from the `SearchParameters` with `escapeHTML`', () => {
it('does not remove the TAG_PLACEHOLDER from the `SearchParameters` with `escapeHTML` disabled', () => {
const helper = algoliasearchHelper(fakeClient, 'firstIndex', {
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
Expand Down
13 changes: 12 additions & 1 deletion src/connectors/autocomplete/connectAutocomplete.js
Expand Up @@ -57,7 +57,18 @@ export default function connectAutocomplete(renderFn, unmountFn = noop) {

return {
getConfiguration() {
return escapeHTML ? TAG_PLACEHOLDER : undefined;
const parameters = {
query: '',
};

if (!escapeHTML) {
return parameters;
}

return {
...parameters,
...TAG_PLACEHOLDER,
};
},

init({ instantSearchInstance, helper }) {
Expand Down

0 comments on commit 724b83f

Please sign in to comment.