Skip to content

Commit

Permalink
add getRenderState method
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed Oct 1, 2020
1 parent 21457c1 commit 311654f
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 92 deletions.
66 changes: 62 additions & 4 deletions src/connectors/hits/__tests__/connectHits-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
} from '../../../../test/mock/createWidget';
import { createSearchClient } from '../../../../test/mock/createSearchClient';
import { createSingleSearchResponse } from '../../../../test/mock/createAPIResponse';
import { EscapedHits, HitAttributeHighlightResult } from '../../../types';
import {
EscapedHits,
HitAttributeHighlightResult,
HitsWidget,
} from '../../../types';

jest.mock('../../../lib/utils/hits-absolute-position', () => ({
// The real implementation creates a new array instance, which can cause bugs,
Expand Down Expand Up @@ -444,7 +448,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
expect(((results.hits as unknown) as EscapedHits).__escaped).toBe(true);
});

describe('getWidgetRenderState', () => {
describe('getRenderState', () => {
it('returns the render state', () => {
const renderFn = jest.fn();
const unmountFn = jest.fn();
Expand All @@ -454,7 +458,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
index: 'indexName',
});

const renderState1 = hitsWidget.getWidgetRenderState!(
const renderState1 = hitsWidget.getRenderState!(
{},
createInitOptions({ state: helper.state, helper })
);
Expand All @@ -474,7 +478,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
createSingleSearchResponse({ hits, queryID: 'theQueryID' }),
]);

const renderState2 = hitsWidget.getWidgetRenderState!(
const renderState2 = hitsWidget.getRenderState!(
{},
createRenderOptions({
helper,
Expand All @@ -500,6 +504,60 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
});
});

describe('getWidgetRenderState', () => {
it('returns the widget render state', () => {
const renderFn = jest.fn();
const unmountFn = jest.fn();
const createHits = connectHits(renderFn, unmountFn);
const hitsWidget = createHits({}) as HitsWidget;
const helper = algoliasearchHelper(createSearchClient(), 'indexName', {
index: 'indexName',
});

const renderState1 = hitsWidget.getWidgetRenderState(
createInitOptions({ state: helper.state, helper })
);

expect(renderState1).toEqual({
hits: [],
results: undefined,
widgetParams: {},
});

const hits = [
{ objectID: '1', name: 'name 1' },
{ objectID: '2', name: 'name 2' },
];

const results = new SearchResults(helper.state, [
createSingleSearchResponse({ hits, queryID: 'theQueryID' }),
]);

const renderState2 = hitsWidget.getWidgetRenderState!(
createRenderOptions({
helper,
state: helper.state,
results,
})
);

const expectedHits = [
{ objectID: '1', name: 'name 1', __queryID: 'theQueryID' },
{ objectID: '2', name: 'name 2', __queryID: 'theQueryID' },
];

((expectedHits as unknown) as EscapedHits).__escaped = true;

expect(renderState2).toEqual(
expect.objectContaining({
hits: expectedHits,
results,
widgetParams: {},
})
);
});
});

describe('getWidgetSearchParameters', () => {
it('adds the TAG_PLACEHOLDER to the `SearchParameters`', () => {
const render = () => {};
Expand Down
35 changes: 16 additions & 19 deletions src/connectors/hits/connectHits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ const connectHits: HitsConnector = function connectHits(
init(initOptions) {
renderFn(
{
...this.getWidgetRenderState!(initOptions.renderState, initOptions)
.hits!,
...this.getWidgetRenderState(initOptions),
instantSearchInstance: initOptions.instantSearchInstance,
},
true
Expand All @@ -69,25 +68,26 @@ const connectHits: HitsConnector = function connectHits(
render(renderOptions) {
renderFn(
{
...this.getWidgetRenderState!(
renderOptions.renderState,
renderOptions
).hits!,
...this.getWidgetRenderState(renderOptions),
instantSearchInstance: renderOptions.instantSearchInstance,
},
false
);
},

getWidgetRenderState(renderState, { results }) {
getRenderState(renderState, renderOptions) {
return {
...renderState,
hits: this.getWidgetRenderState(renderOptions),
};
},

getWidgetRenderState({ results }) {
if (!results) {
return {
...renderState,
hits: {
hits: [],
results: undefined,
widgetParams,
},
hits: [],
results: undefined,
widgetParams,
};
}

Expand Down Expand Up @@ -116,12 +116,9 @@ const connectHits: HitsConnector = function connectHits(
>).__escaped = initialEscaped;

return {
...renderState,
hits: {
hits: results.hits,
results,
widgetParams,
},
hits: results.hits,
results,
widgetParams,
};
},

Expand Down
Loading

0 comments on commit 311654f

Please sign in to comment.