@@ -6,22 +6,15 @@ import {
6
6
import { deserializePayload } from '../../utils' ;
7
7
import type { EscapedHits } from '../../../types' ;
8
8
9
- const createTestEnvironment = ( ) => {
9
+ const createTestEnvironment = ( { nbHits = 2 } : { nbHits ?: number } = { } ) => {
10
10
const instantSearchInstance = createInstantSearch ( ) ;
11
11
const index = 'testIndex' ;
12
12
const widgetType = 'ais.testWidget' ;
13
- const hits = [
14
- {
15
- objectID : 'obj0' ,
16
- __position : 0 ,
17
- __queryID : 'test-query-id' ,
18
- } ,
19
- {
20
- objectID : 'obj1' ,
21
- __position : 1 ,
22
- __queryID : 'test-query-id' ,
23
- } ,
24
- ] ;
13
+ const hits = Array . from ( { length : nbHits } , ( _ , i ) => ( {
14
+ __position : i ,
15
+ __queryID : 'test-query-id' ,
16
+ objectID : `obj${ i } ` ,
17
+ } ) ) ;
25
18
const sendEvent = createSendEventForHits ( {
26
19
instantSearchInstance,
27
20
index,
@@ -163,6 +156,44 @@ describe('createSendEventForHits', () => {
163
156
} ) ;
164
157
} ) ;
165
158
159
+ it ( 'sends view event with more than 20 hits' , ( ) => {
160
+ const { sendEvent, instantSearchInstance, hits } = createTestEnvironment ( {
161
+ nbHits : 21 ,
162
+ } ) ;
163
+ sendEvent ( 'view' , hits ) ;
164
+ expect ( instantSearchInstance . sendEventToInsights ) . toHaveBeenCalledTimes ( 2 ) ;
165
+ expect ( instantSearchInstance . sendEventToInsights ) . toHaveBeenCalledWith ( {
166
+ eventType : 'view' ,
167
+ hits : Array . from ( { length : 20 } , ( _ , i ) => ( {
168
+ __position : i ,
169
+ __queryID : 'test-query-id' ,
170
+ objectID : `obj${ i } ` ,
171
+ } ) ) ,
172
+ insightsMethod : 'viewedObjectIDs' ,
173
+ payload : {
174
+ eventName : 'Hits Viewed' ,
175
+ index : 'testIndex' ,
176
+ objectIDs : Array . from ( { length : 20 } , ( _ , i ) => `obj${ i } ` ) ,
177
+ } ,
178
+ widgetType : 'ais.testWidget' ,
179
+ } ) ;
180
+ expect ( instantSearchInstance . sendEventToInsights ) . toHaveBeenCalledWith ( {
181
+ eventType : 'view' ,
182
+ hits : Array . from ( { length : 1 } , ( _ , i ) => ( {
183
+ __position : 20 + i ,
184
+ __queryID : 'test-query-id' ,
185
+ objectID : `obj${ 20 + i } ` ,
186
+ } ) ) ,
187
+ insightsMethod : 'viewedObjectIDs' ,
188
+ payload : {
189
+ eventName : 'Hits Viewed' ,
190
+ index : 'testIndex' ,
191
+ objectIDs : Array . from ( { length : 1 } , ( _ , i ) => `obj${ 20 + i } ` ) ,
192
+ } ,
193
+ widgetType : 'ais.testWidget' ,
194
+ } ) ;
195
+ } ) ;
196
+
166
197
it ( 'sends click event' , ( ) => {
167
198
const { sendEvent, instantSearchInstance, hits } = createTestEnvironment ( ) ;
168
199
sendEvent ( 'click' , hits [ 0 ] , 'Product Clicked' ) ;
@@ -188,6 +219,52 @@ describe('createSendEventForHits', () => {
188
219
} ) ;
189
220
} ) ;
190
221
222
+ it ( 'sends click event with more than 20 hits' , ( ) => {
223
+ const { sendEvent, instantSearchInstance, hits } = createTestEnvironment ( {
224
+ nbHits : 21 ,
225
+ } ) ;
226
+ sendEvent ( 'click' , hits , 'Product Clicked' ) ;
227
+ expect ( instantSearchInstance . sendEventToInsights ) . toHaveBeenCalledTimes ( 2 ) ;
228
+ expect ( instantSearchInstance . sendEventToInsights ) . toHaveBeenCalledWith ( {
229
+ eventType : 'click' ,
230
+ hits : Array . from ( { length : 20 } , ( _ , i ) => {
231
+ return {
232
+ __position : i ,
233
+ __queryID : 'test-query-id' ,
234
+ objectID : `obj${ i } ` ,
235
+ } ;
236
+ } ) ,
237
+ insightsMethod : 'clickedObjectIDsAfterSearch' ,
238
+ payload : {
239
+ eventName : 'Product Clicked' ,
240
+ index : 'testIndex' ,
241
+ objectIDs : Array . from ( { length : 20 } , ( _ , i ) => `obj${ i } ` ) ,
242
+ positions : Array . from ( { length : 20 } , ( _ , i ) => i ) ,
243
+ queryID : 'test-query-id' ,
244
+ } ,
245
+ widgetType : 'ais.testWidget' ,
246
+ } ) ;
247
+ expect ( instantSearchInstance . sendEventToInsights ) . toHaveBeenCalledWith ( {
248
+ eventType : 'click' ,
249
+ hits : [
250
+ {
251
+ __position : 20 ,
252
+ __queryID : 'test-query-id' ,
253
+ objectID : 'obj20' ,
254
+ } ,
255
+ ] ,
256
+ insightsMethod : 'clickedObjectIDsAfterSearch' ,
257
+ payload : {
258
+ eventName : 'Product Clicked' ,
259
+ index : 'testIndex' ,
260
+ objectIDs : [ 'obj20' ] ,
261
+ positions : [ 20 ] ,
262
+ queryID : 'test-query-id' ,
263
+ } ,
264
+ widgetType : 'ais.testWidget' ,
265
+ } ) ;
266
+ } ) ;
267
+
191
268
it ( 'sends conversion event' , ( ) => {
192
269
const { sendEvent, instantSearchInstance, hits } = createTestEnvironment ( ) ;
193
270
sendEvent ( 'conversion' , hits [ 0 ] , 'Product Ordered' ) ;
@@ -212,6 +289,50 @@ describe('createSendEventForHits', () => {
212
289
} ) ;
213
290
} ) ;
214
291
292
+ it ( 'sends conversion event with more than 20 hits' , ( ) => {
293
+ const { sendEvent, instantSearchInstance, hits } = createTestEnvironment ( {
294
+ nbHits : 21 ,
295
+ } ) ;
296
+ sendEvent ( 'conversion' , hits , 'Product Ordered' ) ;
297
+ expect ( instantSearchInstance . sendEventToInsights ) . toHaveBeenCalledTimes ( 2 ) ;
298
+ expect ( instantSearchInstance . sendEventToInsights ) . toHaveBeenCalledWith ( {
299
+ eventType : 'conversion' ,
300
+ hits : Array . from ( { length : 20 } , ( _ , i ) => {
301
+ return {
302
+ __position : i ,
303
+ __queryID : 'test-query-id' ,
304
+ objectID : `obj${ i } ` ,
305
+ } ;
306
+ } ) ,
307
+ insightsMethod : 'convertedObjectIDsAfterSearch' ,
308
+ payload : {
309
+ eventName : 'Product Ordered' ,
310
+ index : 'testIndex' ,
311
+ objectIDs : Array . from ( { length : 20 } , ( _ , i ) => `obj${ i } ` ) ,
312
+ queryID : 'test-query-id' ,
313
+ } ,
314
+ widgetType : 'ais.testWidget' ,
315
+ } ) ;
316
+ expect ( instantSearchInstance . sendEventToInsights ) . toHaveBeenCalledWith ( {
317
+ eventType : 'conversion' ,
318
+ hits : [
319
+ {
320
+ __position : 20 ,
321
+ __queryID : 'test-query-id' ,
322
+ objectID : 'obj20' ,
323
+ } ,
324
+ ] ,
325
+ insightsMethod : 'convertedObjectIDsAfterSearch' ,
326
+ payload : {
327
+ eventName : 'Product Ordered' ,
328
+ index : 'testIndex' ,
329
+ objectIDs : [ 'obj20' ] ,
330
+ queryID : 'test-query-id' ,
331
+ } ,
332
+ widgetType : 'ais.testWidget' ,
333
+ } ) ;
334
+ } ) ;
335
+
215
336
it ( 'sends custom event' , ( ) => {
216
337
const { sendEvent, instantSearchInstance } = createTestEnvironment ( ) ;
217
338
sendEvent ( {
@@ -268,49 +389,98 @@ describe('createBindEventForHits', () => {
268
389
const parsedPayload = parsePayload (
269
390
bindEvent ( 'click' , hits [ 0 ] , 'Product Clicked' )
270
391
) ;
271
- expect ( parsedPayload ) . toEqual ( {
272
- eventType : 'click' ,
273
- hits : [
274
- {
275
- __position : 0 ,
276
- __queryID : 'test-query-id' ,
277
- objectID : 'obj0' ,
392
+ expect ( parsedPayload ) . toEqual ( [
393
+ {
394
+ eventType : 'click' ,
395
+ hits : [
396
+ {
397
+ __position : 0 ,
398
+ __queryID : 'test-query-id' ,
399
+ objectID : 'obj0' ,
400
+ } ,
401
+ ] ,
402
+ insightsMethod : 'clickedObjectIDsAfterSearch' ,
403
+ payload : {
404
+ eventName : 'Product Clicked' ,
405
+ index : 'testIndex' ,
406
+ objectIDs : [ 'obj0' ] ,
407
+ positions : [ 0 ] ,
408
+ queryID : 'test-query-id' ,
278
409
} ,
279
- ] ,
280
- insightsMethod : 'clickedObjectIDsAfterSearch' ,
281
- payload : {
282
- eventName : 'Product Clicked' ,
283
- index : 'testIndex' ,
284
- objectIDs : [ 'obj0' ] ,
285
- positions : [ 0 ] ,
286
- queryID : 'test-query-id' ,
410
+ widgetType : 'ais.testWidget' ,
287
411
} ,
288
- widgetType : 'ais.testWidget' ,
289
- } ) ;
412
+ ] ) ;
290
413
} ) ;
291
414
292
415
it ( 'returns a payload for conversion event' , ( ) => {
293
416
const { bindEvent, hits } = createTestEnvironment ( ) ;
294
417
const parsedPayload = parsePayload (
295
418
bindEvent ( 'conversion' , hits [ 0 ] , 'Product Ordered' )
296
419
) ;
297
- expect ( parsedPayload ) . toEqual ( {
298
- eventType : 'conversion' ,
299
- hits : [
300
- {
301
- __position : 0 ,
420
+ expect ( parsedPayload ) . toEqual ( [
421
+ {
422
+ eventType : 'conversion' ,
423
+ hits : [
424
+ {
425
+ __position : 0 ,
426
+ __queryID : 'test-query-id' ,
427
+ objectID : 'obj0' ,
428
+ } ,
429
+ ] ,
430
+ insightsMethod : 'convertedObjectIDsAfterSearch' ,
431
+ payload : {
432
+ eventName : 'Product Ordered' ,
433
+ index : 'testIndex' ,
434
+ objectIDs : [ 'obj0' ] ,
435
+ queryID : 'test-query-id' ,
436
+ } ,
437
+ widgetType : 'ais.testWidget' ,
438
+ } ,
439
+ ] ) ;
440
+ } ) ;
441
+
442
+ it ( 'splits a payload for > 20 hits' , ( ) => {
443
+ const { bindEvent, hits } = createTestEnvironment ( { nbHits : 21 } ) ;
444
+ const parsedPayload = parsePayload (
445
+ bindEvent ( 'click' , hits , 'Product Clicked' )
446
+ ) ;
447
+ expect ( parsedPayload ) . toEqual ( [
448
+ {
449
+ eventType : 'click' ,
450
+ hits : Array . from ( { length : 20 } , ( _ , i ) => ( {
451
+ __position : i ,
302
452
__queryID : 'test-query-id' ,
303
- objectID : 'obj0' ,
453
+ objectID : `obj${ i } ` ,
454
+ } ) ) ,
455
+ insightsMethod : 'clickedObjectIDsAfterSearch' ,
456
+ payload : {
457
+ eventName : 'Product Clicked' ,
458
+ index : 'testIndex' ,
459
+ objectIDs : Array . from ( { length : 20 } , ( _ , i ) => `obj${ i } ` ) ,
460
+ positions : Array . from ( { length : 20 } , ( _ , i ) => i ) ,
461
+ queryID : 'test-query-id' ,
304
462
} ,
305
- ] ,
306
- insightsMethod : 'convertedObjectIDsAfterSearch' ,
307
- payload : {
308
- eventName : 'Product Ordered' ,
309
- index : 'testIndex' ,
310
- objectIDs : [ 'obj0' ] ,
311
- queryID : 'test-query-id' ,
463
+ widgetType : 'ais.testWidget' ,
312
464
} ,
313
- widgetType : 'ais.testWidget' ,
314
- } ) ;
465
+ {
466
+ eventType : 'click' ,
467
+ hits : [
468
+ {
469
+ __position : 20 ,
470
+ __queryID : 'test-query-id' ,
471
+ objectID : 'obj20' ,
472
+ } ,
473
+ ] ,
474
+ insightsMethod : 'clickedObjectIDsAfterSearch' ,
475
+ payload : {
476
+ eventName : 'Product Clicked' ,
477
+ index : 'testIndex' ,
478
+ objectIDs : [ 'obj20' ] ,
479
+ positions : [ 20 ] ,
480
+ queryID : 'test-query-id' ,
481
+ } ,
482
+ widgetType : 'ais.testWidget' ,
483
+ } ,
484
+ ] ) ;
315
485
} ) ;
316
486
} ) ;
0 commit comments