@@ -554,4 +554,63 @@ describe('middlewares/batch', () => {
554554 ) ;
555555 } ) ;
556556 } ) ;
557+
558+ describe ( 'option `maxRequestsPerBatch`' , ( ) => {
559+ beforeEach ( ( ) => {
560+ fetchMock . restore ( ) ;
561+ } ) ;
562+
563+ it ( 'should split batch requests based on max requests limit' , async ( ) => {
564+ // Set up mocks for single and batch requests
565+ fetchMock . mock ( {
566+ matcher : '/graphql' ,
567+ response : {
568+ status : 200 ,
569+ body : { data : { } } ,
570+ } ,
571+ method : 'POST' ,
572+ } ) ;
573+
574+ fetchMock . mock ( {
575+ matcher : '/graphql/batch' ,
576+ response : {
577+ status : 200 ,
578+ body : [ { data : { } } , { data : { } } , { data : { } } , { data : { } } , { data : { } } ] ,
579+ } ,
580+ method : 'POST' ,
581+ } ) ;
582+
583+ // Create a network layer with maxRequestsPerBatch set to 2
584+ const rnl = new RelayNetworkLayer ( [ batchMiddleware ( { maxRequestsPerBatch : 2 } ) ] ) ;
585+
586+ // Create 5 mock requests
587+ const req1 = mockReq ( 1 ) ;
588+ const req2 = mockReq ( 2 ) ;
589+ const req3 = mockReq ( 3 ) ;
590+ const req4 = mockReq ( 4 ) ;
591+ const req5 = mockReq ( 5 ) ;
592+
593+ // Execute all requests simultaneously
594+ await Promise . all ( [
595+ req1 . execute ( rnl ) ,
596+ req2 . execute ( rnl ) ,
597+ req3 . execute ( rnl ) ,
598+ req4 . execute ( rnl ) ,
599+ req5 . execute ( rnl ) ,
600+ ] ) ;
601+
602+ // Check if the requests were properly split into batches
603+ const batchReqs = fetchMock . calls ( '/graphql/batch' ) ;
604+ expect ( batchReqs ) . toHaveLength ( 3 ) ; // Should create 3 batches (2 + 2 + 1 requests)
605+
606+ // Verify the contents of the batches
607+ const firstBatchBody = JSON . parse ( batchReqs [ 0 ] [ 1 ] . body ) ;
608+ const secondBatchBody = JSON . parse ( batchReqs [ 1 ] [ 1 ] . body ) ;
609+ const thirdBatchBody = JSON . parse ( batchReqs [ 2 ] [ 1 ] . body ) ;
610+
611+ expect ( firstBatchBody ) . toHaveLength ( 2 ) ;
612+ expect ( secondBatchBody ) . toHaveLength ( 2 ) ;
613+ expect ( thirdBatchBody ) . toHaveLength ( 1 ) ;
614+ } ) ;
615+ } ) ;
557616} ) ;
0 commit comments