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

refactoring: move show more count tests to common suite #5638

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ import {
import type { Hit } from 'instantsearch.js';
import type { SendEventForHits } from 'instantsearch.js/es/lib/utils';

/**
* Converts InstantSearch.js templates into React InstantSearch Hooks translations.
* @param templates InstantSearch.js templates received in `widgetParams`
* @param map Matching between template keys and translation keys
*/
function fromTemplates(
templates: Record<string, unknown>,
map: Record<string, string>
) {
return Object.entries(map).reduce<Record<string, any>>(
(translations, [templateKey, translationKey]) => {
if (templates[templateKey] !== undefined) {
translations[translationKey] = templates[templateKey];
}

return translations;
},
{}
);
}

/**
* prevent rethrowing InstantSearch errors, so tests can be asserted.
* IRL this isn't needed, as the error doesn't stop execution.
Expand All @@ -41,18 +62,32 @@ function GlobalErrorSwallower() {
}

createRefinementListTests(({ instantSearchOptions, widgetParams }) => {
const { templates, ...props } = widgetParams;
const translations =
templates &&
fromTemplates(templates, {
showMoreText: 'showMoreButtonText',
});

render(
<InstantSearch {...instantSearchOptions}>
<RefinementList {...widgetParams} />
<RefinementList {...props} translations={translations} />
<GlobalErrorSwallower />
</InstantSearch>
);
}, act);

createHierarchicalMenuTests(({ instantSearchOptions, widgetParams }) => {
const { templates, ...props } = widgetParams;
const translations =
templates &&
fromTemplates(templates, {
showMoreText: 'showMoreButtonText',
});

render(
<InstantSearch {...instantSearchOptions}>
<HierarchicalMenu {...widgetParams} />
<HierarchicalMenu {...props} translations={translations} />
<GlobalErrorSwallower />
</InstantSearch>
);
Expand All @@ -70,9 +105,16 @@ createBreadcrumbTests(({ instantSearchOptions, widgetParams }) => {
}, act);

createMenuTests(({ instantSearchOptions, widgetParams }) => {
const { templates, ...props } = widgetParams;
const translations =
templates &&
fromTemplates(templates, {
showMoreText: 'showMoreButtonText',
});

render(
<InstantSearch {...instantSearchOptions}>
<Menu {...widgetParams} />
<Menu {...props} translations={translations} />
<GlobalErrorSwallower />
</InstantSearch>
);
Expand Down
116 changes: 76 additions & 40 deletions packages/vue-instantsearch/src/__tests__/common.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ import {
} from '../instantsearch';
jest.unmock('instantsearch.js/es');

/**
* Converts InstantSearch.js templates into Vue InstantSearch slots.
* @param {Record<string, any>} templates InstantSearch.js templates received in `widgetParams`
* @param {Record<string, any>} map Matching between template keys and slots names
* @returns {Record<string, any>} Vue InstantSearch slots
*/
function fromTemplates(templates, map) {
return Object.entries(map).reduce(
(translations, [templateKey, translationKey]) => {
if (templates[templateKey] !== undefined) {
return { ...translations, [translationKey]: templates[templateKey] };
}

return translations;
},
{}
);
}

/**
* prevent rethrowing InstantSearch errors, so tests can be asserted.
* IRL this isn't needed, as the error doesn't stop execution.
Expand All @@ -42,47 +61,57 @@ const GlobalErrorSwallower = {
},
};

createRefinementListTests(
async ({ instantSearchOptions, widgetParams, vueSlots }) => {
mountApp(
{
render: renderCompat((h) =>
h(AisInstantSearch, { props: instantSearchOptions }, [
h(AisRefinementList, {
props: widgetParams,
scopedSlots: vueSlots,
}),
h(GlobalErrorSwallower),
])
),
},
document.body.appendChild(document.createElement('div'))
);
createRefinementListTests(async ({ instantSearchOptions, widgetParams }) => {
const { templates, ...props } = widgetParams;
const scopedSlots =
templates &&
fromTemplates(templates, {
showMoreText: 'showMoreLabel',
});

await nextTick();
}
);
mountApp(
{
render: renderCompat((h) =>
h(AisInstantSearch, { props: instantSearchOptions }, [
h(AisRefinementList, {
props,
scopedSlots,
}),
h(GlobalErrorSwallower),
])
),
},
document.body.appendChild(document.createElement('div'))
);

createHierarchicalMenuTests(
async ({ instantSearchOptions, widgetParams, vueSlots }) => {
mountApp(
{
render: renderCompat((h) =>
h(AisInstantSearch, { props: instantSearchOptions }, [
h(AisHierarchicalMenu, {
props: widgetParams,
scopedSlots: vueSlots,
}),
h(GlobalErrorSwallower),
])
),
},
document.body.appendChild(document.createElement('div'))
);
await nextTick();
});

createHierarchicalMenuTests(async ({ instantSearchOptions, widgetParams }) => {
const { templates, ...props } = widgetParams;
const scopedSlots =
templates &&
fromTemplates(templates, {
showMoreText: 'showMoreLabel',
});

mountApp(
{
render: renderCompat((h) =>
h(AisInstantSearch, { props: instantSearchOptions }, [
h(AisHierarchicalMenu, {
props,
scopedSlots,
}),
h(GlobalErrorSwallower),
])
),
},
document.body.appendChild(document.createElement('div'))
);

await nextTick();
}
);
await nextTick();
});

createBreadcrumbTests(async ({ instantSearchOptions, widgetParams }) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -103,12 +132,19 @@ createBreadcrumbTests(async ({ instantSearchOptions, widgetParams }) => {
await nextTick();
});

createMenuTests(async ({ instantSearchOptions, widgetParams, vueSlots }) => {
createMenuTests(async ({ instantSearchOptions, widgetParams }) => {
const { templates, ...props } = widgetParams;
const scopedSlots =
templates &&
fromTemplates(templates, {
showMoreText: 'showMoreLabel',
});

mountApp(
{
render: renderCompat((h) =>
h(AisInstantSearch, { props: instantSearchOptions }, [
h(AisMenu, { props: widgetParams, scopedSlots: vueSlots }),
h(AisMenu, { props, scopedSlots }),
h(GlobalErrorSwallower),
])
),
Expand Down
19 changes: 0 additions & 19 deletions tests/common/widgets/hierarchical-menu/show-more.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export function createShowMoreTests(setup: HierarchicalMenuSetup, act: Act) {
limit,
showMoreLimit,
showMore: true,
// InstantSearch.js
templates: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is good, but should the source of truth be templates or translations? I guess it's fine either way though, depends where we consider the translation

// @ts-ignore
showMoreText({ isShowingMore, showMoreCount }) {
Expand All @@ -70,24 +69,6 @@ export function createShowMoreTests(setup: HierarchicalMenuSetup, act: Act) {
: 'Show top items';
},
},
// React InstantSearch Hooks
translations: {
// @ts-ignore
showMoreButtonText({ isShowingMore, showMoreCount }) {
return !isShowingMore
? `Show ${showMoreCount} more`
: 'Show top items';
},
},
},
// Vue InstantSearch
vueSlots: {
// @ts-ignore
showMoreLabel({ isShowingMore, showMoreCount }) {
return !isShowingMore
? `Show ${showMoreCount} more`
: 'Show top items';
},
},
};

Expand Down
19 changes: 0 additions & 19 deletions tests/common/widgets/menu/show-more.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export function createShowMoreTests(setup: MenuSetup, act: Act) {
limit,
showMoreLimit,
showMore: true,
// InstantSearch.js
templates: {
// @ts-ignore
showMoreText({ isShowingMore, showMoreCount }) {
Expand All @@ -70,24 +69,6 @@ export function createShowMoreTests(setup: MenuSetup, act: Act) {
: 'Show top items';
},
},
// React InstantSearch Hooks
translations: {
// @ts-ignore
showMoreButtonText({ isShowingMore, showMoreCount }) {
return !isShowingMore
? `Show ${showMoreCount} more`
: 'Show top items';
},
},
},
// Vue InstantSearch
vueSlots: {
// @ts-ignore
showMoreLabel({ isShowingMore, showMoreCount }) {
return !isShowingMore
? `Show ${showMoreCount} more`
: 'Show top items';
},
},
};

Expand Down
19 changes: 0 additions & 19 deletions tests/common/widgets/refinement-list/show-more.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export function createShowMoreTests(setup: RefinementListSetup, act: Act) {
limit,
showMoreLimit,
showMore: true,
// InstantSearch.js
templates: {
// @ts-ignore
showMoreText({ isShowingMore, showMoreCount }) {
Expand All @@ -70,24 +69,6 @@ export function createShowMoreTests(setup: RefinementListSetup, act: Act) {
: 'Show top items';
},
},
// React InstantSearch Hooks
translations: {
// @ts-ignore
showMoreButtonText({ isShowingMore, showMoreCount }) {
return !isShowingMore
? `Show ${showMoreCount} more`
: 'Show top items';
},
},
},
// Vue InstantSearch
vueSlots: {
// @ts-ignore
showMoreLabel({ isShowingMore, showMoreCount }) {
return !isShowingMore
? `Show ${showMoreCount} more`
: 'Show top items';
},
},
};

Expand Down