Skip to content

Commit

Permalink
fix(insights): remove bindEvent (#6471)
Browse files Browse the repository at this point in the history
bindEvent is no longer handled in the component since #6403 (https://github.com/algolia/instantsearch/pull/6403/files#diff-e7717d55fdbbc94c7dd996f78b6f0f860332545d6fa8f6b843c940798334532cL94)

[FX-3191]

BREAKING CHANGE: the `bindEvent` related functions are non longer usable, use `sendEvent` instead
  • Loading branch information
Haroenv committed Dec 12, 2024
1 parent 97de992 commit c15dd17
Show file tree
Hide file tree
Showing 17 changed files with 33 additions and 530 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
createInitOptions,
createRenderOptions,
} from '../../../test/createWidget';
import { deserializePayload } from '../../lib/utils';

import type {
EscapedHits,
Expand Down Expand Up @@ -509,7 +508,6 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
hits: [],
items: [],
sendEvent: expect.any(Function),
bindEvent: expect.any(Function),
results: undefined,
widgetParams: {},
});
Expand Down Expand Up @@ -541,7 +539,6 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
hits: expectedHits,
items: expectedHits,
sendEvent: renderState1.hits.sendEvent,
bindEvent: renderState1.hits.bindEvent,
results,
widgetParams: {},
});
Expand Down Expand Up @@ -573,7 +570,6 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
items: [],
banner: undefined,
sendEvent: expect.any(Function),
bindEvent: expect.any(Function),
results: undefined,
widgetParams: {},
});
Expand Down Expand Up @@ -611,7 +607,6 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
items: expectedHits,
banner,
sendEvent: renderState1.sendEvent,
bindEvent: renderState2.bindEvent,
results,
widgetParams: {},
});
Expand Down Expand Up @@ -1033,71 +1028,6 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
});
});
});

describe('bindEvent', () => {
it('returns a payload for click event', () => {
const { renderFn, hits } = createRenderedWidget();
const { bindEvent } =
renderFn.mock.calls[renderFn.mock.calls.length - 1][0];
const payload = bindEvent('click', hits[0], 'Product Added');
expect(payload.startsWith('data-insights-event=')).toBe(true);
expect(
deserializePayload(payload.substr('data-insights-event='.length))
).toEqual([
{
eventType: 'click',
hits: [
{
__position: 0,
__queryID: 'test-query-id',
fake: 'data',
objectID: '1',
},
],
insightsMethod: 'clickedObjectIDsAfterSearch',
payload: {
eventName: 'Product Added',
index: '',
objectIDs: ['1'],
positions: [0],
queryID: 'test-query-id',
},
widgetType: 'ais.hits',
},
]);
});

it('returns a payload for conversion event', () => {
const { renderFn, hits } = createRenderedWidget();
const { bindEvent } =
renderFn.mock.calls[renderFn.mock.calls.length - 1][0];
const payload = bindEvent('conversion', hits[1], 'Product Ordered');
expect(payload.startsWith('data-insights-event=')).toBe(true);
expect(
deserializePayload(payload.substr('data-insights-event='.length))
).toEqual([
{
eventType: 'conversion',
hits: [
{
__position: 1,
__queryID: 'test-query-id',
objectID: '2',
sample: 'infos',
},
],
insightsMethod: 'convertedObjectIDsAfterSearch',
payload: {
eventName: 'Product Ordered',
index: '',
objectIDs: ['2'],
queryID: 'test-query-id',
},
widgetType: 'ais.hits',
},
]);
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
createRenderOptions,
} from '../../../test/createWidget';
import { createInfiniteHitsSessionStorageCache } from '../../lib/infiniteHitsCache';
import { deserializePayload } from '../../lib/utils';

import type {
SearchClient,
Expand Down Expand Up @@ -1328,7 +1327,6 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
items: [],
currentPageHits: [],
sendEvent: expect.any(Function),
bindEvent: expect.any(Function),
isFirstPage: true,
isLastPage: true,
banner: undefined,
Expand Down Expand Up @@ -1392,7 +1390,6 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
items: expectedHits,
currentPageHits: expectedCurrentPageHits,
sendEvent: renderState1.infiniteHits.sendEvent,
bindEvent: renderState1.infiniteHits.bindEvent,
isFirstPage: true,
isLastPage: true,
banner: undefined,
Expand Down Expand Up @@ -1423,7 +1420,6 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
items: [],
currentPageHits: [],
sendEvent: expect.any(Function),
bindEvent: expect.any(Function),
isFirstPage: true,
isLastPage: true,
banner: undefined,
Expand Down Expand Up @@ -1500,7 +1496,6 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
items: expectedHits,
currentPageHits: expectedCurrentPageHits,
sendEvent: renderState1.sendEvent,
bindEvent: renderState1.bindEvent,
isFirstPage: true,
isLastPage: true,
banner,
Expand Down Expand Up @@ -1870,71 +1865,6 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
});
});
});

describe('bindEvent', () => {
it('returns a payload for click event', () => {
const { renderFn, hits } = createRenderedWidget();
const { bindEvent } =
renderFn.mock.calls[renderFn.mock.calls.length - 1][0];
const payload = bindEvent('click', hits[0], 'Product Added');
expect(payload.startsWith('data-insights-event=')).toBe(true);
expect(
deserializePayload(payload.substr('data-insights-event='.length))
).toEqual([
{
eventType: 'click',
hits: [
{
__position: 0,
__queryID: 'test-query-id',
fake: 'data',
objectID: '1',
},
],
insightsMethod: 'clickedObjectIDsAfterSearch',
payload: {
eventName: 'Product Added',
index: '',
objectIDs: ['1'],
positions: [0],
queryID: 'test-query-id',
},
widgetType: 'ais.infiniteHits',
},
]);
});

it('returns a payload for conversion event', () => {
const { renderFn, hits } = createRenderedWidget();
const { bindEvent } =
renderFn.mock.calls[renderFn.mock.calls.length - 1][0];
const payload = bindEvent('conversion', hits[1], 'Product Ordered');
expect(payload.startsWith('data-insights-event=')).toBe(true);
expect(
deserializePayload(payload.substr('data-insights-event='.length))
).toEqual([
{
eventType: 'conversion',
hits: [
{
__position: 1,
__queryID: 'test-query-id',
objectID: '2',
sample: 'infos',
},
],
insightsMethod: 'convertedObjectIDsAfterSearch',
payload: {
eventName: 'Product Ordered',
index: '',
objectIDs: ['2'],
queryID: 'test-query-id',
},
widgetType: 'ais.infiniteHits',
},
]);
});
});
});
});
});
18 changes: 0 additions & 18 deletions packages/instantsearch-core/src/connectors/connectHits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
addAbsolutePosition,
addQueryID,
createSendEventForHits,
createBindEventForHits,
} from '../lib/utils';

import type {
Expand All @@ -21,7 +20,6 @@ import type {
Unmounter,
Renderer,
SendEventForHits,
BindEventForHits,
Widget,
} from '../types';
import type { Banner, SearchResults } from 'algoliasearch-helper';
Expand Down Expand Up @@ -57,11 +55,6 @@ export type HitsRenderState<THit extends NonNullable<object> = BaseHit> = {
* Sends an event to the Insights middleware.
*/
sendEvent: SendEventForHits;

/**
* Returns a string for the `data-insights-event` attribute for the Insights middleware
*/
bindEvent: BindEventForHits;
};

export type HitsConnectorParams<THit extends NonNullable<object> = BaseHit> = {
Expand Down Expand Up @@ -107,7 +100,6 @@ export const connectHits = function connectHits<TWidgetParams>(
>,
} = (widgetParams || {}) as HitsConnectorParams<THit>;
let sendEvent: SendEventForHits;
let bindEvent: BindEventForHits;

type HitsWidget = Widget<
HitsWidgetDescription<THit> & {
Expand Down Expand Up @@ -160,22 +152,13 @@ export const connectHits = function connectHits<TWidgetParams>(
});
}

if (!bindEvent) {
bindEvent = createBindEventForHits({
getIndex: () => helper.getIndex(),
widgetType: this.$$type,
instantSearchInstance,
});
}

if (!results) {
return {
hits: [],
items: [],
results: undefined,
banner: undefined,
sendEvent,
bindEvent,
widgetParams,
};
}
Expand Down Expand Up @@ -207,7 +190,6 @@ export const connectHits = function connectHits<TWidgetParams>(
results,
banner,
sendEvent,
bindEvent,
widgetParams,
};
},
Expand Down
14 changes: 0 additions & 14 deletions packages/instantsearch-core/src/connectors/connectInfiniteHits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
addAbsolutePosition,
addQueryID,
createSendEventForHits,
createBindEventForHits,
} from '../lib/utils';

import type {
Expand All @@ -24,7 +23,6 @@ import type {
Unmounter,
UnknownWidgetParams,
SendEventForHits,
BindEventForHits,
Widget,
} from '../types';
import type {
Expand Down Expand Up @@ -117,11 +115,6 @@ export type InfiniteHitsRenderState<
*/
sendEvent: SendEventForHits;

/**
* Returns a string of data-insights-event attribute for insights middleware
*/
bindEvent: BindEventForHits;

/**
* Hits for the current page
*/
Expand Down Expand Up @@ -237,7 +230,6 @@ export const connectInfiniteHits = function connectInfiniteHits<
let showPrevious: () => void;
let showMore: () => void;
let sendEvent: SendEventForHits;
let bindEvent: BindEventForHits;
const getFirstReceivedPage = (
state: SearchParameters,
cachedHits: InfiniteHitsCachedHits<THit>
Expand Down Expand Up @@ -365,11 +357,6 @@ export const connectInfiniteHits = function connectInfiniteHits<
getIndex: () => helper.getIndex(),
widgetType: this.$$type,
});
bindEvent = createBindEventForHits({
getIndex: () => helper.getIndex(),
widgetType: this.$$type,
instantSearchInstance,
});
isFirstPage =
state.page === undefined ||
getFirstReceivedPage(state, cachedHits) === 0;
Expand Down Expand Up @@ -440,7 +427,6 @@ export const connectInfiniteHits = function connectInfiniteHits<
items,
currentPageHits,
sendEvent,
bindEvent,
banner,
results: results || undefined,
showPrevious,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,35 @@ describe('createSendEventForFacet', () => {
expect(() => {
sendEvent('click');
}).toThrowErrorMatchingInlineSnapshot(`
"You need to pass between two and four arguments like:
sendEvent('click', facetValue, eventName?, additionalData?);
"You need to pass between two and four arguments like:
sendEvent('click', facetValue, eventName?, additionalData?);
If you want to send a custom payload, you can pass one object: sendEvent(customPayload);
"
`);
If you want to send a custom payload, you can pass one object: sendEvent(customPayload);"
`);
});

it('throws with unknown eventType', () => {
const { sendEvent } = createTestEnvironment();
expect(() => {
sendEvent('my custom event type');
}).toThrowErrorMatchingInlineSnapshot(`
"You need to pass between two and four arguments like:
sendEvent('click', facetValue, eventName?, additionalData?);
"You need to pass between two and four arguments like:
sendEvent('click', facetValue, eventName?, additionalData?);
If you want to send a custom payload, you can pass one object: sendEvent(customPayload);
"
`);
If you want to send a custom payload, you can pass one object: sendEvent(customPayload);"
`);
});

it('throws when eventType is not click', () => {
const { sendEvent } = createTestEnvironment();
expect(() => {
sendEvent('custom event type');
}).toThrowErrorMatchingInlineSnapshot(`
"You need to pass between two and four arguments like:
sendEvent('click', facetValue, eventName?, additionalData?);
"You need to pass between two and four arguments like:
sendEvent('click', facetValue, eventName?, additionalData?);
If you want to send a custom payload, you can pass one object: sendEvent(customPayload);
"
`);
If you want to send a custom payload, you can pass one object: sendEvent(customPayload);"
`);
});

it('does not send event when a facet is removed', () => {
Expand Down
Loading

0 comments on commit c15dd17

Please sign in to comment.