@@ -1326,6 +1326,135 @@ describe('insights', () => {
1326
1326
} ) . toWarnDev ( ) ;
1327
1327
expect ( insightsClient ) . toHaveBeenCalledTimes ( numberOfCalls ) ; // still the same
1328
1328
} ) ;
1329
+
1330
+ it ( 'does not send view events that were previously sent for the current query' , ( ) => {
1331
+ const { insightsClient, instantSearchInstance } = createTestEnvironment ( ) ;
1332
+
1333
+ instantSearchInstance . use (
1334
+ createInsightsMiddleware ( {
1335
+ insightsClient,
1336
+ } )
1337
+ ) ;
1338
+
1339
+ insightsClient ( 'setUserToken' , 'token' ) ;
1340
+
1341
+ instantSearchInstance . sendEventToInsights ( {
1342
+ insightsMethod : 'viewedObjectIDs' ,
1343
+ widgetType : 'ais.customWidget' ,
1344
+ eventType : 'view' ,
1345
+ payload : {
1346
+ index : 'my-index' ,
1347
+ eventName : 'My Hits Viewed' ,
1348
+ objectIDs : [ 'obj1' ] ,
1349
+ } ,
1350
+ } ) ;
1351
+
1352
+ instantSearchInstance . sendEventToInsights ( {
1353
+ insightsMethod : 'viewedObjectIDs' ,
1354
+ widgetType : 'ais.customWidget' ,
1355
+ eventType : 'view' ,
1356
+ payload : {
1357
+ index : 'my-index' ,
1358
+ eventName : 'My Hits Viewed' ,
1359
+ objectIDs : [ 'obj1' ] ,
1360
+ } ,
1361
+ } ) ;
1362
+
1363
+ expect (
1364
+ insightsClient . mock . calls . filter (
1365
+ ( call ) => call [ 0 ] === 'viewedObjectIDs'
1366
+ )
1367
+ ) . toHaveLength ( 1 ) ;
1368
+ } ) ;
1369
+
1370
+ it ( 'clears saved view events when the query changes' , ( ) => {
1371
+ const { insightsClient, instantSearchInstance } = createTestEnvironment ( ) ;
1372
+
1373
+ instantSearchInstance . use (
1374
+ createInsightsMiddleware ( {
1375
+ insightsClient,
1376
+ } )
1377
+ ) ;
1378
+
1379
+ insightsClient ( 'setUserToken' , 'token' ) ;
1380
+
1381
+ instantSearchInstance . sendEventToInsights ( {
1382
+ insightsMethod : 'viewedObjectIDs' ,
1383
+ widgetType : 'ais.customWidget' ,
1384
+ eventType : 'view' ,
1385
+ payload : {
1386
+ index : 'my-index' ,
1387
+ eventName : 'My Hits Viewed' ,
1388
+ objectIDs : [ 'obj1' ] ,
1389
+ } ,
1390
+ } ) ;
1391
+
1392
+ instantSearchInstance . mainHelper ! . derivedHelpers [ 0 ] . emit ( 'result' , {
1393
+ results : { queryId : '2' } ,
1394
+ } ) ;
1395
+
1396
+ instantSearchInstance . sendEventToInsights ( {
1397
+ insightsMethod : 'viewedObjectIDs' ,
1398
+ widgetType : 'ais.customWidget' ,
1399
+ eventType : 'view' ,
1400
+ payload : {
1401
+ index : 'my-index' ,
1402
+ eventName : 'My Hits Viewed' ,
1403
+ objectIDs : [ 'obj1' ] ,
1404
+ } ,
1405
+ } ) ;
1406
+
1407
+ expect (
1408
+ insightsClient . mock . calls . filter (
1409
+ ( call ) => call [ 0 ] === 'viewedObjectIDs'
1410
+ )
1411
+ ) . toHaveLength ( 2 ) ;
1412
+ } ) ;
1413
+
1414
+ it ( "only sends view events that haven't been sent yet for current query" , ( ) => {
1415
+ const { insightsClient, instantSearchInstance } = createTestEnvironment ( ) ;
1416
+
1417
+ instantSearchInstance . use (
1418
+ createInsightsMiddleware ( {
1419
+ insightsClient,
1420
+ } )
1421
+ ) ;
1422
+
1423
+ insightsClient ( 'setUserToken' , 'token' ) ;
1424
+
1425
+ instantSearchInstance . sendEventToInsights ( {
1426
+ insightsMethod : 'viewedObjectIDs' ,
1427
+ widgetType : 'ais.customWidget' ,
1428
+ eventType : 'view' ,
1429
+ payload : {
1430
+ index : 'my-index' ,
1431
+ eventName : 'My Hits Viewed' ,
1432
+ objectIDs : [ 'obj1' ] ,
1433
+ } ,
1434
+ } ) ;
1435
+
1436
+ instantSearchInstance . sendEventToInsights ( {
1437
+ insightsMethod : 'viewedObjectIDs' ,
1438
+ widgetType : 'ais.customWidget' ,
1439
+ eventType : 'view' ,
1440
+ payload : {
1441
+ index : 'my-index' ,
1442
+ eventName : 'My Hits Viewed' ,
1443
+ objectIDs : [ 'obj1' , 'obj2' ] ,
1444
+ } ,
1445
+ } ) ;
1446
+
1447
+ expect (
1448
+ insightsClient . mock . calls . filter (
1449
+ ( call ) => call [ 0 ] === 'viewedObjectIDs'
1450
+ )
1451
+ ) . toHaveLength ( 2 ) ;
1452
+ expect ( insightsClient ) . toHaveBeenLastCalledWith (
1453
+ 'viewedObjectIDs' ,
1454
+ expect . objectContaining ( { objectIDs : [ 'obj2' ] } ) ,
1455
+ expect . any ( Object )
1456
+ ) ;
1457
+ } ) ;
1329
1458
} ) ;
1330
1459
1331
1460
test ( "does not write to the URL on load when there's an existing anonymous cookie" , async ( ) => {
0 commit comments