diff --git a/src/middlewares/__tests__/createInsightsMiddleware.ts b/src/middlewares/__tests__/createInsightsMiddleware.ts index 82015ef01e..3e8c0f8620 100644 --- a/src/middlewares/__tests__/createInsightsMiddleware.ts +++ b/src/middlewares/__tests__/createInsightsMiddleware.ts @@ -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({ @@ -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', () => { diff --git a/test/mock/createInsightsClient.ts b/test/mock/createInsightsClient.ts index ee52fc30b5..643fe6d8e6 100644 --- a/test/mock/createInsightsClient.ts +++ b/test/mock/createInsightsClient.ts @@ -74,6 +74,9 @@ export function createInsightsUmdVersion() { queue.push = ([methodName, ...args]) => { _aa(methodName, ...args); }; + return { + algoliaAnalytics: instance, + }; }, }; }