@@ -57,25 +57,34 @@ const mockSubscribe = vi.fn()
57
57
const mockRequestSnapshot = vi . fn ( )
58
58
const mockStream = {
59
59
subscribe : mockSubscribe ,
60
- requestSnapshot : ( ...args : any ) => {
61
- mockRequestSnapshot ( ...args )
62
- const results = mockRequestSnapshot . mock . results
63
- const lastResult = results [ results . length - 1 ] ! . value
64
-
65
- const subscribers = mockSubscribe . mock . calls . map ( args => args [ 0 ] )
66
- subscribers . forEach ( subscriber => subscriber ( lastResult . data . map ( ( row : any ) => ( {
67
- type : `insert` ,
60
+ requestSnapshot : async ( ...args : any ) => {
61
+ const result = await mockRequestSnapshot ( ...args )
62
+ const subscribers = mockSubscribe . mock . calls . map ( ( args ) => args [ 0 ] )
63
+ const data = [ ...result . data ]
64
+
65
+ const messages : Array < Message < any > > = data . map ( ( row : any ) => ( {
68
66
value : row . value ,
69
67
key : row . key ,
70
- } ) ) ) )
71
- }
68
+ headers : row . headers ,
69
+ } ) )
70
+
71
+ if ( messages . length > 0 ) {
72
+ // add an up-to-date message
73
+ messages . push ( {
74
+ headers : { control : `up-to-date` } ,
75
+ } )
76
+ }
77
+
78
+ subscribers . forEach ( ( subscriber ) => subscriber ( messages ) )
79
+ return result
80
+ } ,
72
81
}
73
82
74
83
// Mock the requestSnapshot method
75
84
// to return an empty array of data
76
85
// since most tests don't use it
77
86
mockRequestSnapshot . mockResolvedValue ( {
78
- data : [ ]
87
+ data : [ ] ,
79
88
} )
80
89
81
90
vi . mock ( `@electric-sql/client` , async ( ) => {
@@ -360,14 +369,9 @@ describe.each([
360
369
} )
361
370
362
371
if ( autoIndex === `eager` ) {
363
- it . only ( `should load more data via requestSnapshot when creating live query with higher limit` , async ( ) => {
364
- // Reset mocks
365
- vi . clearAllMocks ( )
372
+ it ( `should load more data via requestSnapshot when creating live query with higher limit` , async ( ) => {
366
373
mockRequestSnapshot . mockResolvedValue ( {
367
- data : [
368
- { key :
5 , value :
{ id :
5 , name :
`Eve` , age :
30 , email :
`[email protected] ` , active :
true } } ,
369
- { key :
6 , value :
{ id :
6 , name :
`Frank` , age :
35 , email :
`[email protected] ` , active :
true } } ,
370
- ] ,
374
+ data : [ ] ,
371
375
} )
372
376
373
377
// Initial sync with limited data
@@ -398,15 +402,45 @@ describe.each([
398
402
expect ( limitedLiveQuery . size ) . toBe ( 2 ) // Only first 2 active users
399
403
expect ( mockRequestSnapshot ) . toHaveBeenCalledTimes ( 1 )
400
404
401
- const callArgs = ( index : number ) => mockRequestSnapshot . mock . calls [ index ] ?. [ 0 ]
405
+ const callArgs = ( index : number ) =>
406
+ mockRequestSnapshot . mock . calls [ index ] ?. [ 0 ]
402
407
expect ( callArgs ( 0 ) ) . toMatchObject ( {
403
- params : { "1" : " true" } ,
404
- where : " active = $1" ,
405
- orderBy : " age NULLS FIRST" ,
408
+ params : { "1" : ` true` } ,
409
+ where : ` active = $1` ,
410
+ orderBy : ` age NULLS FIRST` ,
406
411
limit : 2 ,
407
412
} )
408
413
409
- // Create second live query with higher limit of 5
414
+ // Next call will return a snapshot containing 2 rows
415
+ // Calls after that will return the default empty snapshot
416
+ mockRequestSnapshot . mockResolvedValueOnce ( {
417
+ data : [
418
+ {
419
+ headers : { operation : `insert` } ,
420
+ key : 5 ,
421
+ value : {
422
+ id : 5 ,
423
+ name : `Eve` ,
424
+ age : 30 ,
425
+
426
+ active : true ,
427
+ } ,
428
+ } ,
429
+ {
430
+ headers : { operation : `insert` } ,
431
+ key : 6 ,
432
+ value : {
433
+ id : 6 ,
434
+ name : `Frank` ,
435
+ age : 35 ,
436
+
437
+ active : true ,
438
+ } ,
439
+ } ,
440
+ ] ,
441
+ } )
442
+
443
+ // Create second live query with higher limit of 6
410
444
const expandedLiveQuery = createLiveQueryCollection ( {
411
445
id : `expanded-users-live-query` ,
412
446
startSync : true ,
@@ -427,26 +461,34 @@ describe.each([
427
461
await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) )
428
462
429
463
// Verify that requestSnapshot was called with the correct parameters
430
- expect ( mockRequestSnapshot ) . toHaveBeenCalledTimes ( 3 )
464
+ expect ( mockRequestSnapshot ) . toHaveBeenCalledTimes ( 4 )
431
465
432
466
// Check that first it requested a limit of 6 users
433
467
expect ( callArgs ( 1 ) ) . toMatchObject ( {
434
- params : { "1" : " true" } ,
435
- where : " active = $1" ,
436
- orderBy : " age NULLS FIRST" ,
468
+ params : { "1" : ` true` } ,
469
+ where : ` active = $1` ,
470
+ orderBy : ` age NULLS FIRST` ,
437
471
limit : 6 ,
438
472
} )
439
473
440
474
// After this initial snapshot for the new live query it receives all 3 users from the local collection
441
475
// so it still needs 3 more users to reach the limit of 6 so it requests 3 more to the sync layer
442
476
expect ( callArgs ( 2 ) ) . toMatchObject ( {
443
- params : { "1" : " true" , "2" : "25" } ,
444
- where : " active = $1 AND age > $2" ,
445
- orderBy : " age NULLS FIRST" ,
477
+ params : { "1" : ` true` , "2" : `25` } ,
478
+ where : ` active = $1 AND age > $2` ,
479
+ orderBy : ` age NULLS FIRST` ,
446
480
limit : 3 ,
447
481
} )
448
482
449
- // The sync layer won't provide any more users so the DB is exhausted and it stops (i.e. doesn't request more)
483
+ // The previous snapshot returned 2 more users so it still needs 1 more user to reach the limit of 6
484
+ expect ( callArgs ( 3 ) ) . toMatchObject ( {
485
+ params : { "1" : `true` , "2" : `35` } ,
486
+ where : `active = $1 AND age > $2` ,
487
+ orderBy : `age NULLS FIRST` ,
488
+ limit : 1 ,
489
+ } )
490
+
491
+ // The sync layer won't provide any more users so the DB is exhausted and it stops (i.e. doesn't request more)
450
492
451
493
// The expanded live query should now have more data
452
494
expect ( expandedLiveQuery . status ) . toBe ( `ready` )
0 commit comments