@@ -56,7 +56,51 @@ mock.module('autumn-js', () => ({
5656
5757mock . module ( './routes/basket' , ( ) => ( {
5858 default : {
59- fetch : mock ( ( ) => Promise . resolve ( new Response ( JSON . stringify ( { status : 'success' } ) , { status : 200 } ) ) ) ,
59+ fetch : mock ( ( request : Request ) => {
60+ const url = new URL ( request . url ) ;
61+ const isBatch = url . pathname . includes ( '/batch' ) ;
62+
63+ if ( isBatch ) {
64+ return request . json ( ) . then ( ( body ) => {
65+ const eventCount = Array . isArray ( body ) ? body . length : 1 ;
66+ const results = Array ( eventCount ) . fill ( null ) . map ( ( _ , index ) => {
67+ const event = Array . isArray ( body ) ? body [ index ] : body ;
68+ const eventType = event ?. type || 'track' ;
69+ return { status : 'success' , type : eventType } ;
70+ } ) ;
71+
72+ return Promise . resolve ( new Response ( JSON . stringify ( {
73+ status : 'success' ,
74+ batch : true ,
75+ processed : eventCount ,
76+ results
77+ } ) , { status : 200 } ) ) ;
78+ } ) . catch ( ( ) => {
79+ // Fallback if JSON parsing fails
80+ return Promise . resolve ( new Response ( JSON . stringify ( {
81+ status : 'success' ,
82+ batch : true ,
83+ processed : 1 ,
84+ results : [ { status : 'success' , type : 'track' } ]
85+ } ) , { status : 200 } ) ) ;
86+ } ) ;
87+ }
88+
89+ // Parse the request body to determine the event type
90+ return request . json ( ) . then ( ( body ) => {
91+ const eventType = body . type || 'track' ;
92+ return Promise . resolve ( new Response ( JSON . stringify ( {
93+ status : 'success' ,
94+ type : eventType
95+ } ) , { status : 200 } ) ) ;
96+ } ) . catch ( ( ) => {
97+ // Fallback if JSON parsing fails
98+ return Promise . resolve ( new Response ( JSON . stringify ( {
99+ status : 'success' ,
100+ type : 'track'
101+ } ) , { status : 200 } ) ) ;
102+ } ) ;
103+ } ) ,
60104 } ,
61105} ) ) ;
62106
0 commit comments