@@ -449,7 +449,7 @@ describe('Class: AppSyncGraphQLResolver', () => {
449449 }
450450 ) ;
451451
452- it ( 'logs a warning and returns early if one of the batch event is not compatible' , async ( ) => {
452+ it ( 'logs a warning and returns early if one of the batch events is not compatible' , async ( ) => {
453453 // Prepare
454454 const app = new AppSyncGraphQLResolver ( { logger : console } ) ;
455455 app . batchResolver ( vi . fn ( ) , {
@@ -477,63 +477,81 @@ describe('Class: AppSyncGraphQLResolver', () => {
477477 expect ( result ) . toBeUndefined ( ) ;
478478 } ) ;
479479
480- it ( 'registers a batch resolver via direct function call and invokes it (aggregate=true)' , async ( ) => {
481- // Prepare
482- const app = new AppSyncGraphQLResolver ( { logger : console } ) ;
483- const handler = vi . fn ( ) . mockResolvedValue ( [
484- { id : '1' , value : 'A' } ,
485- { id : '2' , value : 'B' } ,
486- ] ) ;
487- app . batchResolver ( handler , {
488- fieldName : 'batchGet' ,
489- typeName : 'Query' ,
480+ it . each ( [
481+ {
490482 aggregate : true ,
491- } ) ;
492- const events = [
493- onGraphqlEventFactory ( 'batchGet' , 'Query' , { id : '1' } ) ,
494- onGraphqlEventFactory ( 'batchGet' , 'Query' , { id : '2' } ) ,
495- ] ;
496-
497- // Act
498- const result = await app . resolve ( events , context ) ;
483+ description : 'aggregate=true' ,
484+ setupHandler : ( handler : ReturnType < typeof vi . fn > ) => {
485+ handler . mockResolvedValue ( [
486+ { id : '1' , value : 'A' } ,
487+ { id : '2' , value : 'B' } ,
488+ ] ) ;
489+ } ,
490+ } ,
491+ {
492+ aggregate : false ,
493+ description : 'aggregate=false and throwOnError=true' ,
494+ setupHandler : ( handler : ReturnType < typeof vi . fn > ) => {
495+ handler
496+ . mockResolvedValueOnce ( { id : '1' , value : 'A' } )
497+ . mockResolvedValueOnce ( { id : '2' , value : 'B' } ) ;
498+ } ,
499+ } ,
500+ ] ) (
501+ 'registers a batch resolver via direct function call and invokes it ($description)' ,
502+ async ( { aggregate, setupHandler } ) => {
503+ // Prepare
504+ const app = new AppSyncGraphQLResolver ( { logger : console } ) ;
505+ const handler = vi . fn ( ) ;
506+ setupHandler ( handler ) ;
499507
500- // Assess
501- expect ( handler ) . toHaveBeenCalledTimes ( 1 ) ;
502- expect ( handler ) . toHaveBeenCalledWith ( events , { event : events , context } ) ;
503- expect ( result ) . toEqual ( [
504- { id : '1' , value : 'A' } ,
505- { id : '2' , value : 'B' } ,
506- ] ) ;
507- } ) ;
508+ if ( aggregate ) {
509+ app . batchResolver ( handler , {
510+ fieldName : 'batchGet' ,
511+ typeName : 'Query' ,
512+ aggregate : true ,
513+ } ) ;
514+ } else {
515+ app . batchResolver ( handler , {
516+ fieldName : 'batchGet' ,
517+ typeName : 'Query' ,
518+ aggregate : false ,
519+ throwOnError : true ,
520+ } ) ;
521+ }
508522
509- it ( 'registers a batch resolver via direct function call and invokes it (aggregate=false) and (throwOnError=true)' , async ( ) => {
510- // Prepare
511- const app = new AppSyncGraphQLResolver ( { logger : console } ) ;
512- const handler = vi
513- . fn ( )
514- . mockResolvedValueOnce ( { id : '1' , value : 'A' } )
515- . mockResolvedValueOnce ( { id : '2' , value : 'B' } ) ;
516- app . batchResolver ( handler , {
517- fieldName : 'batchGet' ,
518- typeName : 'Query' ,
519- aggregate : false ,
520- throwOnError : true ,
521- } ) ;
522- const events = [
523- onGraphqlEventFactory ( 'batchGet' , 'Query' , { id : '1' } ) ,
524- onGraphqlEventFactory ( 'batchGet' , 'Query' , { id : '2' } ) ,
525- ] ;
523+ const events = [
524+ onGraphqlEventFactory ( 'batchGet' , 'Query' , { id : '1' } ) ,
525+ onGraphqlEventFactory ( 'batchGet' , 'Query' , { id : '2' } ) ,
526+ ] ;
526527
527- // Act
528- const result = await app . resolve ( events , context ) ;
528+ // Act
529+ const result = await app . resolve ( events , context ) ;
529530
530- // Assess
531- expect ( handler ) . toHaveBeenCalledTimes ( 2 ) ;
532- expect ( result ) . toEqual ( [
533- { id : '1' , value : 'A' } ,
534- { id : '2' , value : 'B' } ,
535- ] ) ;
536- } ) ;
531+ // Assess
532+ if ( aggregate ) {
533+ expect ( handler ) . toHaveBeenCalledTimes ( 1 ) ;
534+ expect ( handler ) . toHaveBeenCalledWith ( events , {
535+ event : events ,
536+ context,
537+ } ) ;
538+ } else {
539+ expect ( handler ) . toHaveBeenCalledTimes ( 2 ) ;
540+ expect ( handler ) . toHaveBeenNthCalledWith ( 1 , events [ 0 ] . arguments , {
541+ event : events [ 0 ] ,
542+ context,
543+ } ) ;
544+ expect ( handler ) . toHaveBeenNthCalledWith ( 2 , events [ 1 ] . arguments , {
545+ event : events [ 1 ] ,
546+ context,
547+ } ) ;
548+ }
549+ expect ( result ) . toEqual ( [
550+ { id : '1' , value : 'A' } ,
551+ { id : '2' , value : 'B' } ,
552+ ] ) ;
553+ }
554+ ) ;
537555
538556 it ( 'returns null for failed records when aggregate=false' , async ( ) => {
539557 // Prepare
0 commit comments