Skip to content

Commit

Permalink
feat(hits): add banners to Hits widget for Vue (#6276)
Browse files Browse the repository at this point in the history
* wip(hits): pass banner data to createHitsComponent()

* feat(hits): pass banner slot to banner component

* feat(hits): finalize banner slot and update tests

* revert(hits): restore App.vue

* revert(hits): restore App.vue some more

* test(hits): remove unneeded snapshot

* chore(hits): update bundlesize.config.json

* fix(hits): expose banner in default slot

* fix(hits): do not rely on Preact's Fragment

* chore(hits): deflate bundlesize.config.json

* chore(hits): simplify bannerComponent
  • Loading branch information
taylorcjohnson authored Jul 16, 2024
1 parent 8a0ae01 commit eb95049
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 40 deletions.
2 changes: 1 addition & 1 deletion bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
{
"path": "packages/vue-instantsearch/vue3/umd/index.js",
"maxSize": "68 kB"
"maxSize": "68.25 kB"
},
{
"path": "packages/vue-instantsearch/vue2/cjs/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,7 @@ const testOptions = {
createMenuWidgetTests: undefined,
createPaginationWidgetTests: undefined,
createInfiniteHitsWidgetTests: undefined,
createHitsWidgetTests: {
skippedTests: { 'banner option': true },
},
createHitsWidgetTests: undefined,
createRangeInputWidgetTests: undefined,
createRatingMenuWidgetTests: undefined,
createInstantSearchWidgetTests: undefined,
Expand Down
14 changes: 13 additions & 1 deletion packages/vue-instantsearch/src/components/Hits.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export default {
createSuitMixin({ name: 'Hits' }),
],
props: {
showBanner: {
type: Boolean,
default: true,
},
escapeHTML: {
type: Boolean,
default: true,
Expand All @@ -31,6 +35,7 @@ export default {
computed: {
widgetParams() {
return {
showBanner: this.showBanner,
escapeHTML: this.escapeHTML,
transformItems: this.transformItems,
};
Expand All @@ -43,6 +48,7 @@ export default {

const defaultSlot = getScopedSlot(this, 'default');
const itemSlot = getScopedSlot(this, 'item');
const bannerSlot = getScopedSlot(this, 'banner');

const itemComponent = ({
hit,
Expand Down Expand Up @@ -78,7 +84,7 @@ export default {

// We only want to render the default slot
// if no other slots are defined
if (!itemSlot && defaultSlot) {
if (!itemSlot && !bannerSlot && defaultSlot) {
return h(
'div',
{
Expand All @@ -88,6 +94,7 @@ export default {
},
[
defaultSlot({
banner: this.state.banner,
items: this.state.items,
insights: this.state.insights,
sendEvent: this.state.sendEvent,
Expand All @@ -99,11 +106,16 @@ export default {
return h(createHitsComponent({ createElement: h }), {
hits: this.state.items,
itemComponent,
banner: this.showBanner ? this.state.banner : undefined,
bannerComponent: bannerSlot,
sendEvent: this.state.sendEvent,
classNames: this.classNames && {
root: this.classNames['ais-Hits'],
list: this.classNames['ais-Hits-list'],
item: this.classNames['ais-Hits-item'],
bannerRoot: this.classNames['ais-Hits-banner'],
bannerImage: this.classNames['ais-Hits-banner-image'],
bannerLink: this.classNames['ais-Hits-banner-link'],
},
});
}),
Expand Down
45 changes: 45 additions & 0 deletions packages/vue-instantsearch/src/components/__tests__/Hits.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ const defaultState = {
sendEvent: jest.fn(),
};

it('accepts a showBanner prop', () => {
__setState({
...defaultState,
});

const wrapper = mount(Hits, {
propsData: {
showBanner: true,
},
});

expect(wrapper.vm.widgetParams.showBanner).toBe(true);
});

it('accepts an escapeHTML prop', () => {
__setState({
...defaultState,
Expand All @@ -28,6 +42,37 @@ it('accepts an escapeHTML prop', () => {
expect(wrapper.vm.widgetParams.escapeHTML).toBe(true);
});

it('exposes banner prop to the banner slot', () => {
__setState({
...defaultState,
banner: {
image: {
urls: [{ url: 'https://via.placeholder.com/550x250' }],
},
},
});

const wrapper = mount(
{
components: { Hits },
template: `
<Hits>
<template v-slot:banner="{ banner }">
<img :src=banner.image.urls[0].url />
</template>
</Hits>
`,
},
{
propsData: {
showBanner: true,
},
}
);
const img = wrapper.find('img');
expect(img.attributes('src')).toBe('https://via.placeholder.com/550x250');
});

it('exposes insights prop to the default slot', async () => {
const insights = jest.fn();
__setState({
Expand Down
68 changes: 33 additions & 35 deletions tests/common/widgets/hits/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,44 +172,43 @@ export function createOptionsTests(
});
});

skippableDescribe('banner option', skippedTests, () => {
test('renders default banner element with banner widget renderingContent', async () => {
const searchClient = createMockedSearchClient({
renderingContent: {
// @TODO: remove once algoliasearch js client has been updated
// @ts-expect-error
widgets: {
banners: [
{
image: {
urls: [{ url: 'https://via.placeholder.com/550x250' }],
},
link: {
url: 'https://www.algolia.com',
},
test('renders default banner element with banner widget renderingContent', async () => {
const searchClient = createMockedSearchClient({
renderingContent: {
// @TODO: remove once algoliasearch js client has been updated
// @ts-expect-error
widgets: {
banners: [
{
image: {
urls: [{ url: 'https://via.placeholder.com/550x250' }],
},
],
},
link: {
url: 'https://www.algolia.com',
},
},
],
},
});
},
});

await setup({
instantSearchOptions: {
indexName: 'indexName',
searchClient,
},
widgetParams: {},
});
await setup({
instantSearchOptions: {
indexName: 'indexName',
searchClient,
},
widgetParams: {},
});

await act(async () => {
await wait(0);
});
await act(async () => {
await wait(0);
});

expect(
document.querySelector('#hits-with-defaults .ais-Hits')
).toMatchNormalizedInlineSnapshot(
normalizeSnapshot,
`
expect(
document.querySelector('#hits-with-defaults .ais-Hits')
).toMatchNormalizedInlineSnapshot(
normalizeSnapshot,
`
<div
class="ais-Hits"
>
Expand Down Expand Up @@ -242,8 +241,7 @@ export function createOptionsTests(
</ol>
</div>
`
);
});
);
});
});
}
Expand Down

0 comments on commit eb95049

Please sign in to comment.