@@ -64,6 +64,21 @@ describe('createAlgoliaInsightsPlugin', () => {
64
64
) ;
65
65
} ) ;
66
66
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
+
67
82
describe ( 'onItemsChange' , ( ) => {
68
83
test ( 'sends a `viewedObjectIDs` event by default' , async ( ) => {
69
84
const insightsClient = jest . fn ( ) ;
@@ -180,7 +195,10 @@ describe('createAlgoliaInsightsPlugin', () => {
180
195
await runAllMicroTasks ( ) ;
181
196
jest . runAllTimers ( ) ;
182
197
183
- expect ( insightsClient ) . not . toHaveBeenCalled ( ) ;
198
+ expect ( insightsClient ) . not . toHaveBeenCalledWith (
199
+ 'viewedObjectIDs' ,
200
+ expect . any ( Object )
201
+ ) ;
184
202
} ) ;
185
203
186
204
test ( 'debounces calls' , async ( ) => {
@@ -271,7 +289,10 @@ describe('createAlgoliaInsightsPlugin', () => {
271
289
await runAllMicroTasks ( ) ;
272
290
jest . runAllTimers ( ) ;
273
291
274
- expect ( insightsClient ) . not . toHaveBeenCalled ( ) ;
292
+ expect ( insightsClient ) . not . toHaveBeenCalledWith (
293
+ 'viewedObjectIDs' ,
294
+ expect . any ( Object )
295
+ ) ;
275
296
} ) ;
276
297
277
298
test ( 'does not send an event when there are no results' , async ( ) => {
@@ -296,7 +317,10 @@ describe('createAlgoliaInsightsPlugin', () => {
296
317
await runAllMicroTasks ( ) ;
297
318
jest . runAllTimers ( ) ;
298
319
299
- expect ( insightsClient ) . not . toHaveBeenCalled ( ) ;
320
+ expect ( insightsClient ) . not . toHaveBeenCalledWith (
321
+ 'viewedObjectIDs' ,
322
+ expect . any ( Object )
323
+ ) ;
300
324
} ) ;
301
325
} ) ;
302
326
@@ -436,7 +460,10 @@ describe('createAlgoliaInsightsPlugin', () => {
436
460
437
461
await runAllMicroTasks ( ) ;
438
462
439
- expect ( insightsClient ) . not . toHaveBeenCalled ( ) ;
463
+ expect ( insightsClient ) . not . toHaveBeenCalledWith (
464
+ 'clickedObjectIDsAfterSearch' ,
465
+ expect . any ( Object )
466
+ ) ;
440
467
} ) ;
441
468
442
469
test ( 'does not send an event with non-Algolia Insights hits' , async ( ) => {
@@ -464,7 +491,10 @@ describe('createAlgoliaInsightsPlugin', () => {
464
491
465
492
await runAllMicroTasks ( ) ;
466
493
467
- expect ( insightsClient ) . not . toHaveBeenCalled ( ) ;
494
+ expect ( insightsClient ) . not . toHaveBeenCalledWith (
495
+ 'viewedObjectIDs' ,
496
+ expect . any ( Object )
497
+ ) ;
468
498
} ) ;
469
499
} ) ;
470
500
@@ -493,6 +523,9 @@ describe('createAlgoliaInsightsPlugin', () => {
493
523
} ,
494
524
} ) ;
495
525
526
+ // The client is always called once with `addAlgoliaAgent` on `subscribe`
527
+ insightsClient . mockClear ( ) ;
528
+
496
529
inputElement . focus ( ) ;
497
530
498
531
await runAllMicroTasks ( ) ;
0 commit comments