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(stats): implement getRenderState and getWidgetRenderState #4565

Merged
merged 8 commits into from
Nov 6, 2020
120 changes: 120 additions & 0 deletions src/connectors/stats/__tests__/connectStats-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import jsHelper from 'algoliasearch-helper';
const SearchResults = jsHelper.SearchResults;
import { createSearchClient } from '../../../../test/mock/createSearchClient';
import { createSingleSearchResponse } from '../../../../test/mock/createAPIResponse';
import {
createInitOptions,
createRenderOptions,
} from '../../../../test/mock/createWidget';

import connectStats from '../connectStats';

Expand Down Expand Up @@ -33,6 +39,120 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/stats/js/#c
});
});

describe('getRenderState', () => {
test('returns the render state', () => {
const renderFn = jest.fn();
const unmountFn = jest.fn();
const createStats = connectStats(renderFn, unmountFn);
const stats = createStats();
const helper = jsHelper(createSearchClient(), 'indexName', {
index: 'indexName',
});
helper.search = jest.fn();

const renderState1 = stats.getRenderState(
{ stats: {} },
createInitOptions({ helper })
);

expect(renderState1.stats).toEqual({
hitsPerPage: undefined,
nbHits: 0,
nbPages: 0,
page: 0,
processingTimeMS: -1,
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
query: '',
widgetParams: {},
});

stats.init(createInitOptions({ helper, state: helper.state }));

const results = new SearchResults(helper.state, [
createSingleSearchResponse(),
]);

const renderState2 = stats.getRenderState(
{ stats: {} },
createRenderOptions({
helper,
state: helper.state,
results,
})
);

expect(renderState2.stats).toEqual({
hitsPerPage: results.hitsPerPage,
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
nbHits: results.nbHits,
nbPages: results.nbPages,
page: results.page,
processingTimeMS: results.processingTimeMS,
query: results.query,
widgetParams: {},
});
});
});

describe('getWidgetRenderState', () => {
test('returns the widget render state', () => {
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
const renderFn = jest.fn();
const unmountFn = jest.fn();
const createStats = connectStats(renderFn, unmountFn);
const stats = createStats();
const helper = jsHelper(createSearchClient(), 'indexName', {
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
index: 'indexName',
});
helper.search = jest.fn();

const renderState1 = stats.getWidgetRenderState(
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
createInitOptions({ helper })
);

expect(renderState1).toEqual({
hitsPerPage: undefined,
nbHits: 0,
nbPages: 0,
page: 0,
processingTimeMS: -1,
query: '',
widgetParams: {},
});

stats.init(createInitOptions({ helper, state: helper.state }));
shortcuts marked this conversation as resolved.
Show resolved Hide resolved

const results = new SearchResults(helper.state, [
createSingleSearchResponse({
hits: [
{ brand: 'samsung', objectID: '1' },
{ brand: 'apple', objectID: '2' },
{ brand: 'sony', objectID: '3' },
{ brand: 'benq', objectID: '4' },
{ brand: 'dyson', objectID: '5' },
],
hitsPerPage: 3,
query: 'apple',
}),
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
]);

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

expect(renderState2).toEqual({
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
hitsPerPage: results.hitsPerPage,
nbHits: results.nbHits,
nbPages: results.nbPages,
page: results.page,
processingTimeMS: results.processingTimeMS,
query: results.query,
widgetParams: {},
});
});
});

it('Renders during init and render', () => {
// test that the dummyRendering is called with the isFirstRendering
// flag set accordingly
Expand Down
55 changes: 39 additions & 16 deletions src/connectors/stats/connectStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,25 @@ export default function connectStats(renderFn, unmountFn = noop) {
return (widgetParams = {}) => ({
$$type: 'ais.stats',

init({ helper, instantSearchInstance }) {
init(initOptions) {
const { instantSearchInstance } = initOptions;

renderFn(
{
...this.getWidgetRenderState(initOptions),
instantSearchInstance,
hitsPerPage: helper.state.hitsPerPage,
nbHits: 0,
nbPages: 0,
page: helper.state.page || 0,
processingTimeMS: -1,
query: helper.state.query || '',
widgetParams,
},
true
);
},

render({ results, instantSearchInstance }) {
render(renderOptions) {
const { instantSearchInstance } = renderOptions;

renderFn(
{
...this.getWidgetRenderState(renderOptions),
instantSearchInstance,
hitsPerPage: results.hitsPerPage,
nbHits: results.nbHits,
nbPages: results.nbPages,
page: results.page,
processingTimeMS: results.processingTimeMS,
query: results.query,
widgetParams,
},
false
);
Expand All @@ -88,5 +80,36 @@ export default function connectStats(renderFn, unmountFn = noop) {
dispose() {
unmountFn();
},

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

getWidgetRenderState({ results, helper }) {
if (results) {
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
return {
hitsPerPage: results.hitsPerPage,
nbHits: results.nbHits,
nbPages: results.nbPages,
page: results.page,
processingTimeMS: results.processingTimeMS,
query: results.query,
widgetParams,
};
} else {
return {
hitsPerPage: helper.state.hitsPerPage,
nbHits: 0,
nbPages: 0,
page: helper.state.page || 0,
processingTimeMS: -1,
query: helper.state.query || '',
widgetParams,
};
}
},
});
}