Skip to content

Commit

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

* test(connectInfiniteHits): mount a default page
  • Loading branch information
samouss authored and Haroenv committed Oct 23, 2019
1 parent 724b83f commit 8c65249
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
59 changes: 42 additions & 17 deletions src/connectors/infinite-hits/__tests__/connectInfiniteHits-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
escapeHTML: true,
});

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

expect(renderFn).toHaveBeenCalledTimes(0);

const helper = algoliasearchHelper({} as Client, '', {});
Expand Down Expand Up @@ -121,17 +116,6 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
);
});

it('sets the default configuration', () => {
const renderFn = (): void => {};
const makeWidget = connectInfiniteHits(renderFn);
const widget = makeWidget({});

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

it('Provides the hits and accumulates results on next page', () => {
const renderFn = jest.fn();
const makeWidget = connectInfiniteHits(renderFn);
Expand Down Expand Up @@ -624,6 +608,47 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
expect((results.hits as any).__escaped).toBe(true);
});

describe('getConfiguration', () => {
it('adds a `page` to the `SearchParameters`', () => {
const renderFn = (): void => {};
const makeWidget = connectInfiniteHits(renderFn);
const widget = makeWidget({});

const nextConfiguration = widget.getConfiguration!();

expect(nextConfiguration.page).toBe(0);
});

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

const nextConfiguration = widget.getConfiguration!();

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

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

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

const nextConfiguration = widget.getConfiguration!();

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

describe('dispose', () => {
it('calls the unmount function', () => {
const helper = algoliasearchHelper({} as Client, '', {});
Expand Down Expand Up @@ -678,7 +703,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
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({} as Client, '', {
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
Expand Down
13 changes: 12 additions & 1 deletion src/connectors/infinite-hits/connectInfiniteHits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,18 @@ const connectInfiniteHits: InfiniteHitsConnector = (

return {
getConfiguration() {
return escapeHTML ? TAG_PLACEHOLDER : {};
const parameters = {
page: 0,
};

if (!escapeHTML) {
return parameters;
}

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

init({ instantSearchInstance, helper }) {
Expand Down
8 changes: 0 additions & 8 deletions src/widgets/infinite-hits/__tests__/infinite-hits-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { render } from 'preact-compat';
import algoliasearchHelper from 'algoliasearch-helper';
import { TAG_PLACEHOLDER } from '../../../lib/escape-highlight';
import infiniteHits from '../infinite-hits';
import { Client } from '../../../types';

Expand Down Expand Up @@ -53,13 +52,6 @@ describe('infiniteHits()', () => {
};
});

it('It does have a specific configuration', () => {
expect(widget.getConfiguration()).toEqual({
highlightPreTag: TAG_PLACEHOLDER.highlightPreTag,
highlightPostTag: TAG_PLACEHOLDER.highlightPostTag,
});
});

it('calls twice render(<Hits props />, container)', () => {
const state = { page: 0 };
widget.render({ results, state });
Expand Down

0 comments on commit 8c65249

Please sign in to comment.