@@ -350,6 +350,49 @@ describe('CartService', () => {
350350 ) ;
351351 } ) ;
352352
353+ it ( 'gracefully handles subscription not found during cancel' , async ( ) => {
354+ const mockCustomer = StripeResponseFactory ( StripeCustomerFactory ( ) ) ;
355+ const mockSubscription = StripeResponseFactory (
356+ StripeSubscriptionFactory ( {
357+ customer : mockCustomer . id ,
358+ latest_invoice : null ,
359+ } )
360+ ) ;
361+ const mockCart = ResultCartFactory ( {
362+ state : CartState . PROCESSING ,
363+ stripeSubscriptionId : mockSubscription . id ,
364+ stripeCustomerId : mockCustomer . id ,
365+ eligibilityStatus : CartEligibilityStatus . CREATE ,
366+ } ) ;
367+
368+ const stripeError = new Stripe . errors . StripeInvalidRequestError ( {
369+ type : 'invalid_request_error' ,
370+ message : `No such subscription: '${ mockSubscription . id } '` ,
371+ code : 'resource_missing' ,
372+ } ) ;
373+
374+ jest
375+ . spyOn ( cartManager , 'fetchCartById' )
376+ . mockRejectedValueOnce ( new Error ( 'test' ) )
377+ . mockResolvedValue ( mockCart ) ;
378+ jest . spyOn ( cartManager , 'finishErrorCart' ) . mockResolvedValue ( ) ;
379+ jest
380+ . spyOn ( subscriptionManager , 'retrieve' )
381+ . mockResolvedValue ( mockSubscription ) ;
382+ jest
383+ . spyOn ( subscriptionManager , 'cancel' )
384+ . mockRejectedValue ( stripeError ) ;
385+ jest
386+ . spyOn ( paymentIntentManager , 'retrieve' )
387+ . mockResolvedValue ( mockPaymentIntent ) ;
388+
389+ await expect (
390+ cartService . finalizeProcessingCart ( mockCart . id )
391+ ) . rejects . toThrow ( Error ) ;
392+
393+ expect ( subscriptionManager . cancel ) . toHaveBeenCalled ( ) ;
394+ } ) ;
395+
353396 it ( 'cancels a created subscription with async local storage' , async ( ) => {
354397 const mockCustomer = StripeResponseFactory ( StripeCustomerFactory ( ) ) ;
355398 const mockSubscription = StripeResponseFactory (
0 commit comments