@@ -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 ( ) => {
@@ -458,14 +467,9 @@ describe.each([
458
467
subscription . unsubscribe ( )
459
468
} )
460
469
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 ( )
470
+ it ( `should load more data via requestSnapshot when creating live query with higher limit` , async ( ) => {
464
471
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
- ] ,
472
+ data : [ ] ,
469
473
} )
470
474
471
475
// Initial sync with limited data
@@ -496,15 +500,45 @@ describe.each([
496
500
expect ( limitedLiveQuery . size ) . toBe ( 2 ) // Only first 2 active users
497
501
expect ( mockRequestSnapshot ) . toHaveBeenCalledTimes ( 1 )
498
502
499
- const callArgs = ( index : number ) => mockRequestSnapshot . mock . calls [ index ] ?. [ 0 ]
503
+ const callArgs = ( index : number ) =>
504
+ mockRequestSnapshot . mock . calls [ index ] ?. [ 0 ]
500
505
expect ( callArgs ( 0 ) ) . toMatchObject ( {
501
- params : { "1" : " true" } ,
502
- where : " active = $1" ,
503
- orderBy : " age NULLS FIRST" ,
506
+ params : { "1" : ` true` } ,
507
+ where : ` active = $1` ,
508
+ orderBy : ` age NULLS FIRST` ,
504
509
limit : 2 ,
505
510
} )
506
511
507
- // Create second live query with higher limit of 5
512
+ // Next call will return a snapshot containing 2 rows
513
+ // Calls after that will return the default empty snapshot
514
+ mockRequestSnapshot . mockResolvedValueOnce ( {
515
+ data : [
516
+ {
517
+ headers : { operation : `insert` } ,
518
+ key : 5 ,
519
+ value : {
520
+ id : 5 ,
521
+ name : `Eve` ,
522
+ age : 30 ,
523
+
524
+ active : true ,
525
+ } ,
526
+ } ,
527
+ {
528
+ headers : { operation : `insert` } ,
529
+ key : 6 ,
530
+ value : {
531
+ id : 6 ,
532
+ name : `Frank` ,
533
+ age : 35 ,
534
+
535
+ active : true ,
536
+ } ,
537
+ } ,
538
+ ] ,
539
+ } )
540
+
541
+ // Create second live query with higher limit of 6
508
542
const expandedLiveQuery = createLiveQueryCollection ( {
509
543
id : `expanded-users-live-query` ,
510
544
startSync : true ,
@@ -525,26 +559,34 @@ describe.each([
525
559
await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) )
526
560
527
561
// Verify that requestSnapshot was called with the correct parameters
528
- expect ( mockRequestSnapshot ) . toHaveBeenCalledTimes ( 3 )
562
+ expect ( mockRequestSnapshot ) . toHaveBeenCalledTimes ( 4 )
529
563
530
564
// Check that first it requested a limit of 6 users
531
565
expect ( callArgs ( 1 ) ) . toMatchObject ( {
532
- params : { "1" : " true" } ,
533
- where : " active = $1" ,
534
- orderBy : " age NULLS FIRST" ,
566
+ params : { "1" : ` true` } ,
567
+ where : ` active = $1` ,
568
+ orderBy : ` age NULLS FIRST` ,
535
569
limit : 6 ,
536
570
} )
537
571
538
572
// After this initial snapshot for the new live query it receives all 3 users from the local collection
539
573
// so it still needs 3 more users to reach the limit of 6 so it requests 3 more to the sync layer
540
574
expect ( callArgs ( 2 ) ) . toMatchObject ( {
541
- params : { "1" : " true" , "2" : "25" } ,
542
- where : " active = $1 AND age > $2" ,
543
- orderBy : " age NULLS FIRST" ,
575
+ params : { "1" : ` true` , "2" : `25` } ,
576
+ where : ` active = $1 AND age > $2` ,
577
+ orderBy : ` age NULLS FIRST` ,
544
578
limit : 3 ,
545
579
} )
546
580
547
- // The sync layer won't provide any more users so the DB is exhausted and it stops (i.e. doesn't request more)
581
+ // The previous snapshot returned 2 more users so it still needs 1 more user to reach the limit of 6
582
+ expect ( callArgs ( 3 ) ) . toMatchObject ( {
583
+ params : { "1" : `true` , "2" : `35` } ,
584
+ where : `active = $1 AND age > $2` ,
585
+ orderBy : `age NULLS FIRST` ,
586
+ limit : 1 ,
587
+ } )
588
+
589
+ // The sync layer won't provide any more users so the DB is exhausted and it stops (i.e. doesn't request more)
548
590
549
591
// The expanded live query should now have more data
550
592
expect ( expandedLiveQuery . status ) . toBe ( `ready` )
0 commit comments