Skip to content

Commit

Permalink
refactor(sendEventForHits): remove __escaped more efficiently (#4840)
Browse files Browse the repository at this point in the history
* refactor(sendEventForHits): remove __escaped more efficiently

Mapping over the hits isn't heavy, but it's not required, as .slice also removes the __escaped key on hits

* whooops forgot to install
  • Loading branch information
Haroenv committed Aug 20, 2021
1 parent 932ae3a commit 2332c21
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
32 changes: 32 additions & 0 deletions src/lib/utils/__tests__/createSendEventForHits-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createSendEventForHits,
} from '../createSendEventForHits';
import { deserializePayload } from '../../utils';
import type { EscapedHits } from '../../../types';

const createTestEnvironment = () => {
const instantSearchInstance = createInstantSearch();
Expand Down Expand Up @@ -223,6 +224,37 @@ describe('createSendEventForHits', () => {
custom: 'event',
});
});

it('removes __escaped marker', () => {
const { sendEvent, instantSearchInstance, hits } = createTestEnvironment();

(hits as EscapedHits).__escaped = true;

sendEvent('view', hits);
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledTimes(1);
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
eventType: 'view',
hits: [
{
__position: 0,
__queryID: 'test-query-id',
objectID: 'obj0',
},
{
__position: 1,
__queryID: 'test-query-id',
objectID: 'obj1',
},
],
insightsMethod: 'viewedObjectIDs',
payload: {
eventName: 'Hits Viewed',
index: 'testIndex',
objectIDs: ['obj0', 'obj1'],
},
widgetType: 'ais.testWidget',
});
});
});

describe('createBindEventForHits', () => {
Expand Down
5 changes: 2 additions & 3 deletions src/lib/utils/createSendEventForHits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ const buildPayload: BuildPayload = ({
};

function removeEscapedFromHits(hits: Hits | EscapedHits): Hits {
// this returns without `hits.__escaped`
// and this way it doesn't mutate the original `hits`
return hits.map((hit) => hit);
// remove `hits.__escaped` without mutating
return hits.slice();
}

export function createSendEventForHits({
Expand Down

0 comments on commit 2332c21

Please sign in to comment.