@@ -60,6 +60,7 @@ import {
6060 ProfileClient ,
6161} from '@fxa/profile/client' ;
6262import {
63+ FreeTrialFactory ,
6364 MockStrapiClientConfigProvider ,
6465 ProductConfigurationManager ,
6566 StrapiClient ,
@@ -383,9 +384,7 @@ describe('CartService', () => {
383384 jest
384385 . spyOn ( subscriptionManager , 'retrieve' )
385386 . mockResolvedValue ( mockSubscription ) ;
386- jest
387- . spyOn ( subscriptionManager , 'cancel' )
388- . mockRejectedValue ( stripeError ) ;
387+ jest . spyOn ( subscriptionManager , 'cancel' ) . mockRejectedValue ( stripeError ) ;
389388 jest
390389 . spyOn ( paymentIntentManager , 'retrieve' )
391390 . mockResolvedValue ( mockPaymentIntent ) ;
@@ -1722,6 +1721,9 @@ describe('CartService', () => {
17221721 jest
17231722 . spyOn ( invoiceManager , 'preview' )
17241723 . mockResolvedValue ( mockLatestInvoicePreview ) ;
1724+ jest
1725+ . spyOn ( checkoutService , 'getFreeTrialEligibility' )
1726+ . mockResolvedValue ( null ) ;
17251727 } ) ;
17261728
17271729 it ( 'returns cart and upcomingInvoicePreview' , async ( ) => {
@@ -1754,6 +1756,7 @@ describe('CartService', () => {
17541756 } ,
17551757 metricsOptedOut : false ,
17561758 hasActiveSubscriptions : true ,
1759+ freeTrialEligibility : null ,
17571760 } ) ;
17581761
17591762 expect ( cartManager . fetchCartById ) . toHaveBeenCalledWith ( mockCart . id ) ;
@@ -1816,6 +1819,7 @@ describe('CartService', () => {
18161819 customerSessionClientSecret : mockCustomerSession . client_secret ,
18171820 } ,
18181821 hasActiveSubscriptions : true ,
1822+ freeTrialEligibility : null ,
18191823 } ) ;
18201824 expect (
18211825 'latestInvoicePreview' in result && result . latestInvoicePreview
@@ -1884,6 +1888,7 @@ describe('CartService', () => {
18841888 customerSessionClientSecret : mockCustomerSession . client_secret ,
18851889 } ,
18861890 hasActiveSubscriptions : true ,
1891+ freeTrialEligibility : null ,
18871892 } ) ;
18881893 expect (
18891894 'latestInvoicePreview' in result && result . latestInvoicePreview
@@ -1934,6 +1939,7 @@ describe('CartService', () => {
19341939 upcomingInvoicePreview : mockInvoicePreview ,
19351940 metricsOptedOut : false ,
19361941 hasActiveSubscriptions : false ,
1942+ freeTrialEligibility : null ,
19371943 } ) ;
19381944
19391945 expect ( cartManager . fetchCartById ) . toHaveBeenCalledWith ( mockCart . id ) ;
@@ -2006,6 +2012,7 @@ describe('CartService', () => {
20062012 unitAmount : mockFromPrice . unit_amount ,
20072013 } ,
20082014 hasActiveSubscriptions : true ,
2015+ freeTrialEligibility : null ,
20092016 } ) ;
20102017
20112018 expect ( cartManager . fetchCartById ) . toHaveBeenCalledWith ( mockCart . id ) ;
@@ -2312,6 +2319,60 @@ describe('CartService', () => {
23122319 ) ;
23132320 } ) ;
23142321 } ) ;
2322+
2323+ it ( 'includes freeTrialEligibility when cart has uid and eligibility is CREATE' , async ( ) => {
2324+ const mockFreeTrial = FreeTrialFactory ( { trialLengthDays : 7 } ) ;
2325+ const mockCart = ResultCartFactory ( {
2326+ uid : faker . string . uuid ( ) ,
2327+ state : CartState . START ,
2328+ stripeSubscriptionId : null ,
2329+ eligibilityStatus : CartEligibilityStatus . CREATE ,
2330+ } ) ;
2331+ const mockInvoicePreview = InvoicePreviewFactory ( ) ;
2332+
2333+ jest . spyOn ( cartManager , 'fetchCartById' ) . mockResolvedValue ( mockCart ) ;
2334+ jest . spyOn ( eligibilityService , 'checkEligibility' ) . mockResolvedValue ( {
2335+ subscriptionEligibilityResult : EligibilityStatus . CREATE ,
2336+ } ) ;
2337+ jest
2338+ . spyOn ( invoiceManager , 'previewUpcoming' )
2339+ . mockResolvedValue ( mockInvoicePreview ) ;
2340+ jest
2341+ . spyOn ( checkoutService , 'getFreeTrialEligibility' )
2342+ . mockResolvedValue ( mockFreeTrial ) ;
2343+
2344+ const result = await cartService . getCart ( mockCart . id ) ;
2345+ expect ( result . freeTrialEligibility ) . toEqual ( mockFreeTrial ) ;
2346+ expect ( checkoutService . getFreeTrialEligibility ) . toHaveBeenCalledWith ( {
2347+ uid : mockCart . uid ,
2348+ offeringConfigId : mockCart . offeringConfigId ,
2349+ countryCode : mockCart . taxAddress ?. countryCode ,
2350+ interval : mockCart . interval ,
2351+ eligibilityStatus : EligibilityStatus . CREATE ,
2352+ } ) ;
2353+ } ) ;
2354+
2355+ it ( 'sets freeTrialEligibility to null when cart has no uid' , async ( ) => {
2356+ const mockCart = ResultCartFactory ( {
2357+ uid : undefined ,
2358+ stripeCustomerId : null ,
2359+ stripeSubscriptionId : null ,
2360+ eligibilityStatus : CartEligibilityStatus . CREATE ,
2361+ } ) ;
2362+ const mockInvoicePreview = InvoicePreviewFactory ( ) ;
2363+
2364+ jest . spyOn ( cartManager , 'fetchCartById' ) . mockResolvedValue ( mockCart ) ;
2365+ jest . spyOn ( eligibilityService , 'checkEligibility' ) . mockResolvedValue ( {
2366+ subscriptionEligibilityResult : EligibilityStatus . CREATE ,
2367+ } ) ;
2368+ jest
2369+ . spyOn ( invoiceManager , 'previewUpcoming' )
2370+ . mockResolvedValue ( mockInvoicePreview ) ;
2371+
2372+ const result = await cartService . getCart ( mockCart . id ) ;
2373+ expect ( result . freeTrialEligibility ) . toBeNull ( ) ;
2374+ expect ( checkoutService . getFreeTrialEligibility ) . not . toHaveBeenCalled ( ) ;
2375+ } ) ;
23152376 } ) ;
23162377
23172378 describe ( 'metricsOptedOut' , ( ) => {
0 commit comments