Skip to content

Commit

Permalink
feat(hitsPerPage): implement getRenderState and `getWidgetRenderSta…
Browse files Browse the repository at this point in the history
…te` (#4532)
  • Loading branch information
shortcuts authored and Haroenv committed Nov 30, 2020
1 parent c178c2c commit 7ad10ea
Show file tree
Hide file tree
Showing 4 changed files with 348 additions and 41 deletions.
279 changes: 279 additions & 0 deletions src/connectors/hits-per-page/__tests__/connectHitsPerPage-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,285 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits-per-pa
});
});

describe('getRenderState', () => {
test('returns the render state', () => {
const renderFn = jest.fn();
const unmountFn = jest.fn();
const createHitsPerPage = connectHitsPerPage(renderFn, unmountFn);
const hitsPerPage = createHitsPerPage({
items: [
{
label: '4',
value: 4,
default: true,
},
{
label: '8',
value: 8,
},
],
});
const helper = algoliasearchHelper(createSearchClient(), 'indexName', {
index: 'indexName',
});

const renderState1 = hitsPerPage.getRenderState(
{
hitsPerPage: {
items: [
{
label: '4',
value: 4,
isRefined: false,
},
{
label: '8',
value: 8,
isRefined: false,
},
],
createURL: expect.any(Function),
refine: expect.any(Function),
hasNoResults: false,
widgetParams: {
items: [
{
label: '4',
value: 4,
default: true,
},
{
label: '8',
value: 8,
},
],
},
},
},
createInitOptions({ helper })
);

expect(renderState1.hitsPerPage).toEqual({
items: [
{
label: '4',
value: 4,
default: true,
isRefined: false,
},
{
label: '8',
value: 8,
isRefined: false,
},
],
createURL: expect.any(Function),
hasNoResults: true,
refine: expect.any(Function),
widgetParams: {
items: [
{
label: '4',
value: 4,
default: true,
},
{
label: '8',
value: 8,
},
],
},
});

hitsPerPage.init!(createInitOptions({ helper }));

const renderState2 = hitsPerPage.getRenderState(
{
hitsPerPage: {
items: [
{
label: '4',
value: 4,
isRefined: false,
},
{
label: '8',
value: 8,
isRefined: false,
},
],
createURL: () => '',
refine: () => {},
hasNoResults: true,
widgetParams: {
items: [
{
label: '4',
value: 4,
default: true,
},
{
label: '8',
value: 8,
},
],
},
},
},
createRenderOptions({
helper,
state: helper.state,
results: new SearchResults(helper.state, [
createSingleSearchResponse({
hitsPerPage: 4,
}),
]),
})
);

expect(renderState2.hitsPerPage).toEqual({
items: [
{
label: '',
value: '',
isRefined: false,
},
{
label: '4',
value: 4,
default: true,
isRefined: false,
},
{
label: '8',
value: 8,
isRefined: false,
},
],
createURL: expect.any(Function),
hasNoResults: true,
refine: expect.any(Function),
widgetParams: {
items: [
{
label: '4',
value: 4,
default: true,
},
{
label: '8',
value: 8,
},
],
},
});
});
});

describe('getWidgetRenderState', () => {
test('returns the widget render state', () => {
const renderFn = jest.fn();
const unmountFn = jest.fn();
const createHitsPerPage = connectHitsPerPage(renderFn, unmountFn);
const hitsPerPage = createHitsPerPage({
items: [
{ label: '4', value: 4, default: true },
{ label: '8', value: 8 },
],
});
const helper = algoliasearchHelper(createSearchClient(), 'indexName', {
index: 'indexName',
});

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

expect(renderState1).toEqual({
items: [
{
label: '4',
value: 4,
default: true,
isRefined: false,
},
{
label: '8',
value: 8,
isRefined: false,
},
],
refine: expect.any(Function),
createURL: expect.any(Function),
hasNoResults: true,
widgetParams: {
items: [
{
label: '4',
value: 4,
default: true,
},
{
label: '8',
value: 8,
},
],
},
});

hitsPerPage.init!(createInitOptions({ helper }));

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

expect(renderState2).toEqual({
items: [
{
label: '',
value: '',
isRefined: false,
},
{
label: '4',
value: 4,
default: true,
isRefined: false,
},
{
label: '8',
value: 8,
isRefined: false,
},
],
createURL: expect.any(Function),
hasNoResults: true,
refine: expect.any(Function),
widgetParams: {
items: [
{
label: '4',
value: 4,
default: true,
},
{
label: '8',
value: 8,
},
],
},
});
});
});

describe('getWidgetUiState', () => {
test('returns the `uiState` empty', () => {
const render = jest.fn();
Expand Down
Loading

0 comments on commit 7ad10ea

Please sign in to comment.