Skip to content

Commit

Permalink
test: make it clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
eunjae-lee committed Nov 9, 2020
1 parent 9effefd commit 1d3c977
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/middlewares/__tests__/createInsightsMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,16 @@ describe('insights', () => {
middleware.subscribe();

setTimeout(() => {
libraryLoadedAndProcessQueue();
// When the library is loaded later, it consumes the queue and sends the event.
const { algoliaAnalytics } = libraryLoadedAndProcessQueue();
expect(algoliaAnalytics.viewedObjectIDs).toHaveBeenCalledTimes(1);
expect(algoliaAnalytics.viewedObjectIDs).toHaveBeenCalledWith({
eventName: 'Hits Viewed',
index: '',
objectIDs: ['1', '2'],
});
done();
}, 20);
}, 200);

expect(() => {
instantSearchInstance.sendEventToInsights({
Expand All @@ -132,6 +139,12 @@ describe('insights', () => {
widgetType: 'ais.hits',
});
}).not.toThrow();

// The library is not loaded yet, so it stays in the queue.
expect(insightsClient.queue[insightsClient.queue.length - 1]).toEqual([
'viewedObjectIDs',
{ eventName: 'Hits Viewed', index: '', objectIDs: ['1', '2'] },
]);
});

it('applies clickAnalytics', () => {
Expand Down
3 changes: 3 additions & 0 deletions test/mock/createInsightsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export function createInsightsUmdVersion() {
queue.push = ([methodName, ...args]) => {
_aa(methodName, ...args);
};
return {
algoliaAnalytics: instance,
};
},
};
}

0 comments on commit 1d3c977

Please sign in to comment.