Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

feat(insights): add insights support to Hits widget #665

Merged
merged 5 commits into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 7 additions & 3 deletions src/components/Hits.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
v-if="state"
:class="suit()"
>
<slot :items="items">
<slot
:items="items"
:insights="state.insights"
samouss marked this conversation as resolved.
Show resolved Hide resolved
>
<ol :class="suit('list')">
<li
v-for="(item, itemIndex) in items"
Expand All @@ -14,6 +17,7 @@
name="item"
:item="item"
:index="itemIndex"
:insights="state.insights"
>objectID: {{ item.objectID }}, index: {{ itemIndex }}</slot>
</li>
</ol>
Expand All @@ -22,14 +26,14 @@
</template>

<script>
import { connectHits } from 'instantsearch.js/es/connectors';
import { connectHitsWithInsights } from 'instantsearch.js/es/connectors';
import { createWidgetMixin } from '../mixins/widget';
import { createSuitMixin } from '../mixins/suit';

export default {
name: 'AisHits',
mixins: [
createWidgetMixin({ connector: connectHits }),
createWidgetMixin({ connector: connectHitsWithInsights }),
createSuitMixin({ name: 'Hits' }),
],
props: {
Expand Down
53 changes: 53 additions & 0 deletions src/components/__tests__/Hits.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,56 @@ it('renders correctly', () => {

expect(wrapper.html()).toMatchSnapshot();
});

it('exposes insights prop to the default slot', () => {
const insights = jest.fn();
__setState({
...defaultState,
insights,
});

const wrapper = mount(Hits, {
scopedSlots: {
default: `
<ul slot-scope="{ items, insights }">
<li v-for="(item, itemIndex) in items" >

<button :id="'add-to-cart-' + item.objectID" @click="insights('clickedObjectIDsAfterSearch', {eventName: 'Add to cart', objectIDs: [item.objectID]})">
Add to cart
</button>
</li>
</ul>
`,
},
});
wrapper.find('#add-to-cart-two').trigger('click');
expect(insights).toHaveBeenCalledWith('clickedObjectIDsAfterSearch', {
eventName: 'Add to cart',
objectIDs: ['two'],
});
});

it('exposes insights prop to the item slot', () => {
const insights = jest.fn();
__setState({
...defaultState,
insights,
});

const wrapper = mount(Hits, {
scopedSlots: {
item: `
<div slot-scope="{ item, insights }">
<button :id="'add-to-cart-' + item.objectID" @click="insights('clickedObjectIDsAfterSearch', {eventName: 'Add to cart', objectIDs: [item.objectID]})">
Add to cart
</button>
</div>
`,
},
});
wrapper.find('#add-to-cart-two').trigger('click');
expect(insights).toHaveBeenCalledWith('clickedObjectIDsAfterSearch', {
eventName: 'Add to cart',
objectIDs: ['two'],
});
});
40 changes: 40 additions & 0 deletions stories/Hits.stories.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { storiesOf } from '@storybook/vue';
import { previewWrapper } from './utils';
import { action } from '@storybook/addon-actions';

storiesOf('ais-hits', module)
.addDecorator(previewWrapper())
Expand Down Expand Up @@ -58,3 +59,42 @@ storiesOf('ais-hits', module)
</ais-panel>
`,
}));

storiesOf('ais-hits', module)
.addDecorator(
previewWrapper({
insightsClient: (method, payload) =>
action(`[InsightsClient] sent ${method} with payload`)(payload),
})
)
.add('with insights default slot', () => ({
template: `
<div>
<ais-configure :clickAnalytics="true" />
<ais-hits>
<div slot-scope="{ items, insights }">
<div
v-for="item in items"
:key="item.objectID"
>
custom objectID: {{item.objectID}}
<button @click="insights('clickedObjectIDsAfterSearch', { eventName: 'Add to cart', objectIDs: [item.objectID] })">Add to cart</button>
</div>
</div>
</ais-hits>
</div>
`,
}))
.add('with insights with item slot', () => ({
template: `
<div>
<ais-configure :clickAnalytics="true" />
<ais-hits>
<div slot="item" slot-scope="{ item, insights }">
custom objectID: {{item.objectID}}
<button @click="insights('clickedObjectIDsAfterSearch', { eventName: 'Add to cart', objectIDs: [item.objectID] })">Add to cart</button>
</div>
</ais-hits>
</div>
`,
}));