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
198 changes: 198 additions & 0 deletions src/connectors/stats/__tests__/connectStats-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
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';

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

const helper = jsHelper(createSearchClient(), 'indexName', {
index: 'indexName',
});

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

return [widget, helper];
};

describe('Usage', () => {
it('throws without render function', () => {
expect(() => {
Expand Down Expand Up @@ -33,6 +60,177 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/stats/js/#c
});
});

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

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

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

test('returns the widget render state with empty results', () => {
const [stats, helper] = getInitializedWidget();

const renderState = stats.getRenderState(
{ stats: {} },
createRenderOptions({
helper,
state: helper.state,
results: new SearchResults(helper.state, [
createSingleSearchResponse(),
]),
})
);

expect(renderState.stats).toEqual({
hitsPerPage: 20,
nbHits: 0,
nbPages: 0,
page: 0,
processingTimeMS: 0,
query: '',
widgetParams: {},
});
});

test('returns the widget render state with results', () => {
const [stats, helper] = getInitializedWidget();

const renderState = stats.getRenderState(
{ stats: {} },
createRenderOptions({
helper,
state: helper.state,
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',
}),
]),
})
);

expect(renderState.stats).toEqual({
hitsPerPage: 3,
nbHits: 5,
nbPages: 2,
page: 0,
processingTimeMS: 0,
query: 'apple',
widgetParams: {},
});
});
});

describe('getWidgetRenderState', () => {
test('returns the widget render state without results', () => {
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',
});

const renderState = stats.getWidgetRenderState(
createInitOptions({ helper })
);

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

test('returns the widget render state with empty results', () => {
const [stats, helper] = getInitializedWidget();

const renderState = stats.getWidgetRenderState(
createRenderOptions({
helper,
state: helper.state,
results: new SearchResults(helper.state, [
createSingleSearchResponse(),
]),
})
);

expect(renderState).toEqual({
hitsPerPage: 20,
nbHits: 0,
nbPages: 0,
page: 0,
processingTimeMS: 0,
query: '',
widgetParams: {},
});
});

test('returns the widget render state with results', () => {
const [stats, helper] = getInitializedWidget();

const renderState = stats.getWidgetRenderState(
createRenderOptions({
helper,
state: helper.state,
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',
}),
]),
})
);

expect(renderState).toEqual({
hitsPerPage: 3,
nbHits: 5,
nbPages: 2,
page: 0,
processingTimeMS: 0,
query: 'apple',
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) {
return {
hitsPerPage: helper.state.hitsPerPage,
nbHits: 0,
nbPages: 0,
page: helper.state.page || 0,
processingTimeMS: -1,
query: helper.state.query || '',
widgetParams,
};
}

return {
hitsPerPage: results.hitsPerPage,
nbHits: results.nbHits,
nbPages: results.nbPages,
page: results.page,
processingTimeMS: results.processingTimeMS,
query: results.query,
widgetParams,
};
},
});
}