@@ -334,6 +334,90 @@ describe('createAlgoliaInsightsPlugin', () => {
334
334
} ) ;
335
335
} ) ;
336
336
337
+ test ( 'sends `viewedObjectIDs` events with additional parameters if client supports it' , async ( ) => {
338
+ const insightsClient = jest . fn ( ) ;
339
+ // @ts -ignore
340
+ insightsClient . version = '2.4.0' ;
341
+ const insightsPlugin = createAlgoliaInsightsPlugin ( { insightsClient } ) ;
342
+
343
+ const { inputElement } = createPlayground ( createAutocomplete , {
344
+ plugins : [ insightsPlugin ] ,
345
+ defaultActiveItemId : 0 ,
346
+ openOnFocus : true ,
347
+ getSources ( ) {
348
+ return [
349
+ createSource ( {
350
+ sourceId : 'testSource1' ,
351
+ getItems : ( ) => [
352
+ {
353
+ label : '1' ,
354
+ objectID : '1' ,
355
+ __autocomplete_algoliaCredentials : {
356
+ appId : 'algoliaAppId1' ,
357
+ apiKey : 'algoliaApiKey1' ,
358
+ } ,
359
+ __autocomplete_indexName : 'index1' ,
360
+ __autocomplete_queryID : 'queryID1' ,
361
+ } ,
362
+ ] ,
363
+ } ) ,
364
+ createSource ( {
365
+ sourceId : 'testSource2' ,
366
+ getItems : ( ) => [
367
+ {
368
+ label : '2' ,
369
+ objectID : '2' ,
370
+ __autocomplete_algoliaCredentials : {
371
+ appId : 'algoliaAppId2' ,
372
+ apiKey : 'algoliaApiKey2' ,
373
+ } ,
374
+ __autocomplete_indexName : 'index2' ,
375
+ __autocomplete_queryID : 'queryID2' ,
376
+ } ,
377
+ ] ,
378
+ } ) ,
379
+ ] ;
380
+ } ,
381
+ } ) ;
382
+
383
+ insightsClient . mockClear ( ) ;
384
+
385
+ inputElement . focus ( ) ;
386
+
387
+ await runAllMicroTasks ( ) ;
388
+ jest . runAllTimers ( ) ;
389
+
390
+ expect ( insightsClient ) . toHaveBeenCalledTimes ( 2 ) ;
391
+ expect ( insightsClient ) . toHaveBeenNthCalledWith (
392
+ 1 ,
393
+ 'viewedObjectIDs' ,
394
+ expect . objectContaining ( {
395
+ index : 'index1' ,
396
+ objectIDs : [ '1' ] ,
397
+ } ) ,
398
+ {
399
+ headers : {
400
+ 'X-Algolia-Application-Id' : 'algoliaAppId1' ,
401
+ 'X-Algolia-API-Key' : 'algoliaApiKey1' ,
402
+ } ,
403
+ }
404
+ ) ;
405
+ expect ( insightsClient ) . toHaveBeenNthCalledWith (
406
+ 2 ,
407
+ 'viewedObjectIDs' ,
408
+ expect . objectContaining ( {
409
+ index : 'index2' ,
410
+ objectIDs : [ '2' ] ,
411
+ } ) ,
412
+ {
413
+ headers : {
414
+ 'X-Algolia-Application-Id' : 'algoliaAppId2' ,
415
+ 'X-Algolia-API-Key' : 'algoliaApiKey2' ,
416
+ } ,
417
+ }
418
+ ) ;
419
+ } ) ;
420
+
337
421
test ( 'sends as many `viewedObjectIDs` events as there are compatible sources' , async ( ) => {
338
422
const insightsClient = jest . fn ( ) ;
339
423
const insightsPlugin = createAlgoliaInsightsPlugin ( { insightsClient } ) ;
@@ -649,6 +733,58 @@ describe('createAlgoliaInsightsPlugin', () => {
649
733
) ;
650
734
} ) ;
651
735
736
+ test ( 'sends a `clickedObjectIDsAfterSearch` event with additional parameters if client supports it' , async ( ) => {
737
+ const insightsClient = jest . fn ( ) ;
738
+ // @ts -ignore
739
+ insightsClient . version = '2.4.0' ;
740
+ const insightsPlugin = createAlgoliaInsightsPlugin ( { insightsClient } ) ;
741
+
742
+ const { inputElement } = createPlayground ( createAutocomplete , {
743
+ plugins : [ insightsPlugin ] ,
744
+ defaultActiveItemId : 0 ,
745
+ openOnFocus : true ,
746
+ getSources ( ) {
747
+ return [
748
+ createSource ( {
749
+ getItems : ( ) => [
750
+ {
751
+ label : '1' ,
752
+ objectID : '1' ,
753
+ __autocomplete_algoliaCredentials : {
754
+ appId : 'algoliaAppId' ,
755
+ apiKey : 'algoliaApiKey' ,
756
+ } ,
757
+ __autocomplete_indexName : 'index1' ,
758
+ __autocomplete_queryID : 'queryID1' ,
759
+ } ,
760
+ ] ,
761
+ } ) ,
762
+ ] ;
763
+ } ,
764
+ } ) ;
765
+
766
+ inputElement . focus ( ) ;
767
+
768
+ await runAllMicroTasks ( ) ;
769
+
770
+ userEvent . type ( inputElement , '{enter}' ) ;
771
+
772
+ await runAllMicroTasks ( ) ;
773
+
774
+ expect ( insightsClient ) . toHaveBeenCalledWith (
775
+ 'clickedObjectIDsAfterSearch' ,
776
+ expect . objectContaining ( {
777
+ objectIDs : [ '1' ] ,
778
+ } ) ,
779
+ {
780
+ headers : {
781
+ 'X-Algolia-Application-Id' : 'algoliaAppId' ,
782
+ 'X-Algolia-API-Key' : 'algoliaApiKey' ,
783
+ } ,
784
+ }
785
+ ) ;
786
+ } ) ;
787
+
652
788
test ( 'sends a custom event' , async ( ) => {
653
789
const insightsClient = jest . fn ( ) ;
654
790
const insightsPlugin = createAlgoliaInsightsPlugin ( {
@@ -854,7 +990,14 @@ describe('createAlgoliaInsightsPlugin', () => {
854
990
expect ( track ) . toHaveBeenCalledWith ( 'Product Browsed from Autocomplete' , {
855
991
eventName : 'Item Active' ,
856
992
index : 'index1' ,
857
- objectIDs : [ '1' ] ,
993
+ items : [
994
+ expect . objectContaining ( {
995
+ label : '1' ,
996
+ objectID : '1' ,
997
+ __autocomplete_indexName : 'index1' ,
998
+ __autocomplete_queryID : 'queryID1' ,
999
+ } ) ,
1000
+ ] ,
858
1001
positions : [ 0 ] ,
859
1002
queryID : 'queryID1' ,
860
1003
algoliaSource : [ 'autocomplete' ] ,
0 commit comments