@@ -64,6 +64,21 @@ describe('createAlgoliaInsightsPlugin', () => {
6464 ) ;
6565 } ) ;
6666
67+ test ( 'sets a user agent on the Insights client on subscribe' , ( ) => {
68+ const insightsClient = jest . fn ( ) ;
69+ const insightsPlugin = createAlgoliaInsightsPlugin ( { insightsClient } ) ;
70+
71+ expect ( insightsClient ) . not . toHaveBeenCalled ( ) ;
72+
73+ createPlayground ( createAutocomplete , { plugins : [ insightsPlugin ] } ) ;
74+
75+ expect ( insightsClient ) . toHaveBeenCalledTimes ( 1 ) ;
76+ expect ( insightsClient ) . toHaveBeenCalledWith (
77+ 'addAlgoliaAgent' ,
78+ 'insights-plugin'
79+ ) ;
80+ } ) ;
81+
6782 describe ( 'onItemsChange' , ( ) => {
6883 test ( 'sends a `viewedObjectIDs` event by default' , async ( ) => {
6984 const insightsClient = jest . fn ( ) ;
@@ -180,7 +195,10 @@ describe('createAlgoliaInsightsPlugin', () => {
180195 await runAllMicroTasks ( ) ;
181196 jest . runAllTimers ( ) ;
182197
183- expect ( insightsClient ) . not . toHaveBeenCalled ( ) ;
198+ expect ( insightsClient ) . not . toHaveBeenCalledWith (
199+ 'viewedObjectIDs' ,
200+ expect . any ( Object )
201+ ) ;
184202 } ) ;
185203
186204 test ( 'debounces calls' , async ( ) => {
@@ -271,7 +289,10 @@ describe('createAlgoliaInsightsPlugin', () => {
271289 await runAllMicroTasks ( ) ;
272290 jest . runAllTimers ( ) ;
273291
274- expect ( insightsClient ) . not . toHaveBeenCalled ( ) ;
292+ expect ( insightsClient ) . not . toHaveBeenCalledWith (
293+ 'viewedObjectIDs' ,
294+ expect . any ( Object )
295+ ) ;
275296 } ) ;
276297
277298 test ( 'does not send an event when there are no results' , async ( ) => {
@@ -296,7 +317,10 @@ describe('createAlgoliaInsightsPlugin', () => {
296317 await runAllMicroTasks ( ) ;
297318 jest . runAllTimers ( ) ;
298319
299- expect ( insightsClient ) . not . toHaveBeenCalled ( ) ;
320+ expect ( insightsClient ) . not . toHaveBeenCalledWith (
321+ 'viewedObjectIDs' ,
322+ expect . any ( Object )
323+ ) ;
300324 } ) ;
301325 } ) ;
302326
@@ -436,7 +460,10 @@ describe('createAlgoliaInsightsPlugin', () => {
436460
437461 await runAllMicroTasks ( ) ;
438462
439- expect ( insightsClient ) . not . toHaveBeenCalled ( ) ;
463+ expect ( insightsClient ) . not . toHaveBeenCalledWith (
464+ 'clickedObjectIDsAfterSearch' ,
465+ expect . any ( Object )
466+ ) ;
440467 } ) ;
441468
442469 test ( 'does not send an event with non-Algolia Insights hits' , async ( ) => {
@@ -464,7 +491,10 @@ describe('createAlgoliaInsightsPlugin', () => {
464491
465492 await runAllMicroTasks ( ) ;
466493
467- expect ( insightsClient ) . not . toHaveBeenCalled ( ) ;
494+ expect ( insightsClient ) . not . toHaveBeenCalledWith (
495+ 'viewedObjectIDs' ,
496+ expect . any ( Object )
497+ ) ;
468498 } ) ;
469499 } ) ;
470500
@@ -493,6 +523,9 @@ describe('createAlgoliaInsightsPlugin', () => {
493523 } ,
494524 } ) ;
495525
526+ // The client is always called once with `addAlgoliaAgent` on `subscribe`
527+ insightsClient . mockClear ( ) ;
528+
496529 inputElement . focus ( ) ;
497530
498531 await runAllMicroTasks ( ) ;
0 commit comments