Skip to content

Commit

Permalink
Splitted tests for getWidgetRenderState and getRenderState for ea…
Browse files Browse the repository at this point in the history
…ch use cases
  • Loading branch information
shortcuts committed Nov 6, 2020
1 parent ab81329 commit 176400b
Showing 1 changed file with 92 additions and 49 deletions.
141 changes: 92 additions & 49 deletions src/connectors/stats/__tests__/connectStats-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ import {
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 @@ -40,22 +61,21 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/stats/js/#c
});

describe('getRenderState', () => {
test('returns the render state', () => {
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',
});
helper.search = jest.fn();

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

expect(renderState1.stats).toEqual({
expect(renderState.stats).toEqual({
hitsPerPage: undefined,
nbHits: 0,
nbPages: 0,
Expand All @@ -64,23 +84,23 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/stats/js/#c
query: '',
widgetParams: {},
});
});

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

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

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

expect(renderState2.stats).toEqual({
expect(renderState.stats).toEqual({
hitsPerPage: 20,
nbHits: 0,
nbPages: 0,
Expand All @@ -90,24 +110,58 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/stats/js/#c
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', () => {
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',
});
helper.search = jest.fn();

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

expect(renderState1).toEqual({
expect(renderState).toEqual({
hitsPerPage: undefined,
nbHits: 0,
nbPages: 0,
Expand All @@ -116,22 +170,22 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/stats/js/#c
query: '',
widgetParams: {},
});
});

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

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

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

expect(renderState2).toEqual({
expect(renderState).toEqual({
hitsPerPage: 20,
nbHits: 0,
nbPages: 0,
Expand All @@ -143,36 +197,25 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/stats/js/#c
});

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

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

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',
}),
]);
const [stats, helper] = getInitializedWidget();

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

Expand Down

0 comments on commit 176400b

Please sign in to comment.