@@ -54,10 +54,30 @@ const sampleUsers: Array<User> = [
54
54
55
55
// Mock the ShapeStream module
56
56
const mockSubscribe = vi . fn ( )
57
+ const mockRequestSnapshot = vi . fn ( )
57
58
const mockStream = {
58
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` ,
68
+ value : row . value ,
69
+ key : row . key ,
70
+ } ) ) ) )
71
+ }
59
72
}
60
73
74
+ // Mock the requestSnapshot method
75
+ // to return an empty array of data
76
+ // since most tests don't use it
77
+ mockRequestSnapshot . mockResolvedValue ( {
78
+ data : [ ]
79
+ } )
80
+
61
81
vi . mock ( `@electric-sql/client` , async ( ) => {
62
82
const actual = await vi . importActual ( `@electric-sql/client` )
63
83
return {
@@ -437,4 +457,98 @@ describe.each([
437
457
// Clean up
438
458
subscription . unsubscribe ( )
439
459
} )
460
+ if ( autoIndex === `eager` ) {
461
+ it . only ( `should load more data via requestSnapshot when creating live query with higher limit` , async ( ) => {
462
+ // Reset mocks
463
+ vi . clearAllMocks ( )
464
+ mockRequestSnapshot . mockResolvedValue ( {
465
+ data : [
466
+ { key :
5 , value :
{ id :
5 , name :
`Eve` , age :
30 , email :
`[email protected] ` , active :
true } } ,
467
+ { key :
6 , value :
{ id :
6 , name :
`Frank` , age :
35 , email :
`[email protected] ` , active :
true } } ,
468
+ ] ,
469
+ } )
470
+
471
+ // Initial sync with limited data
472
+ simulateInitialSync ( )
473
+ expect ( electricCollection . status ) . toBe ( `ready` )
474
+ expect ( electricCollection . size ) . toBe ( 4 )
475
+ expect ( mockRequestSnapshot ) . toHaveBeenCalledTimes ( 0 )
476
+
477
+ // Create first live query with limit of 2
478
+ const limitedLiveQuery = createLiveQueryCollection ( {
479
+ id : `limited-users-live-query` ,
480
+ startSync : true ,
481
+ query : ( q ) =>
482
+ q
483
+ . from ( { user : electricCollection } )
484
+ . where ( ( { user } ) => eq ( user . active , true ) )
485
+ . select ( ( { user } ) => ( {
486
+ id : user . id ,
487
+ name : user . name ,
488
+ active : user . active ,
489
+ age : user . age ,
490
+ } ) )
491
+ . orderBy ( ( { user } ) => user . age , `asc` )
492
+ . limit ( 2 ) ,
493
+ } )
494
+
495
+ expect ( limitedLiveQuery . status ) . toBe ( `ready` )
496
+ expect ( limitedLiveQuery . size ) . toBe ( 2 ) // Only first 2 active users
497
+ expect ( mockRequestSnapshot ) . toHaveBeenCalledTimes ( 1 )
498
+
499
+ const callArgs = ( index : number ) => mockRequestSnapshot . mock . calls [ index ] ?. [ 0 ]
500
+ expect ( callArgs ( 0 ) ) . toMatchObject ( {
501
+ params : { "1" : "true" } ,
502
+ where : "active = $1" ,
503
+ orderBy : "age NULLS FIRST" ,
504
+ limit : 2 ,
505
+ } )
506
+
507
+ // Create second live query with higher limit of 5
508
+ const expandedLiveQuery = createLiveQueryCollection ( {
509
+ id : `expanded-users-live-query` ,
510
+ startSync : true ,
511
+ query : ( q ) =>
512
+ q
513
+ . from ( { user : electricCollection } )
514
+ . where ( ( { user } ) => eq ( user . active , true ) )
515
+ . select ( ( { user } ) => ( {
516
+ id : user . id ,
517
+ name : user . name ,
518
+ active : user . active ,
519
+ } ) )
520
+ . orderBy ( ( { user } ) => user . age , `asc` )
521
+ . limit ( 6 ) ,
522
+ } )
523
+
524
+ // Wait for the live query to process
525
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) )
526
+
527
+ // Verify that requestSnapshot was called with the correct parameters
528
+ expect ( mockRequestSnapshot ) . toHaveBeenCalledTimes ( 3 )
529
+
530
+ // Check that first it requested a limit of 6 users
531
+ expect ( callArgs ( 1 ) ) . toMatchObject ( {
532
+ params : { "1" : "true" } ,
533
+ where : "active = $1" ,
534
+ orderBy : "age NULLS FIRST" ,
535
+ limit : 6 ,
536
+ } )
537
+
538
+ // After this initial snapshot for the new live query it receives all 3 users from the local collection
539
+ // so it still needs 3 more users to reach the limit of 6 so it requests 3 more to the sync layer
540
+ expect ( callArgs ( 2 ) ) . toMatchObject ( {
541
+ params : { "1" : "true" , "2" : "25" } ,
542
+ where : "active = $1 AND age > $2" ,
543
+ orderBy : "age NULLS FIRST" ,
544
+ limit : 3 ,
545
+ } )
546
+
547
+ // The sync layer won't provide any more users so the DB is exhausted and it stops (i.e. doesn't request more)
548
+
549
+ // The expanded live query should now have more data
550
+ expect ( expandedLiveQuery . status ) . toBe ( `ready` )
551
+ expect ( expandedLiveQuery . size ) . toBe ( 5 ) // Alice, Bob, Dave from initial + Eve and Frank from additional data
552
+ } )
553
+ }
440
554
} )
0 commit comments