2023-01-26 11:48:30 -05:00
import { expect } from 'chai'
import { render , screen } from '@testing-library/react'
import PersonalSubscription from '../../../../../../frontend/js/features/subscription/components/dashboard/personal-subscription'
import { SubscriptionDashboardProvider } from '../../../../../../frontend/js/features/subscription/context/subscription-dashboard-context'
import {
annualActiveSubscription ,
canceledSubscription ,
pastDueExpiredSubscription ,
} from '../../fixtures/subscriptions'
describe ( '<PersonalSubscription />' , function ( ) {
beforeEach ( function ( ) {
window . metaAttributesCache = new Map ( )
2023-02-02 04:15:04 -05:00
// @ts-ignore
2023-01-26 12:01:54 -05:00
window . recurly = { }
2023-01-26 11:48:30 -05:00
} )
afterEach ( function ( ) {
window . metaAttributesCache = new Map ( )
2023-02-02 04:15:04 -05:00
// @ts-ignore
2023-01-26 12:01:54 -05:00
delete window . recurly
2023-01-26 11:48:30 -05:00
} )
describe ( 'no subscription' , function ( ) {
it ( 'returns empty container' , function ( ) {
const { container } = render (
< SubscriptionDashboardProvider >
2023-02-07 10:38:04 -05:00
< PersonalSubscription / >
2023-01-26 11:48:30 -05:00
< / SubscriptionDashboardProvider >
)
expect ( container . firstChild ) . to . be . null
} )
} )
describe ( 'subscription states ' , function ( ) {
it ( 'renders the active dash' , function ( ) {
2023-02-07 10:38:04 -05:00
window . metaAttributesCache . set (
'ol-subscription' ,
annualActiveSubscription
)
2023-01-26 11:48:30 -05:00
render (
< SubscriptionDashboardProvider >
2023-02-07 10:38:04 -05:00
< PersonalSubscription / >
2023-01-26 11:48:30 -05:00
< / SubscriptionDashboardProvider >
)
screen . getByText ( 'You are currently subscribed to the' , { exact : false } )
} )
it ( 'renders the canceled dash' , function ( ) {
2023-02-07 10:38:04 -05:00
window . metaAttributesCache . set ( 'ol-subscription' , canceledSubscription )
2023-01-26 11:48:30 -05:00
render (
< SubscriptionDashboardProvider >
2023-02-07 10:38:04 -05:00
< PersonalSubscription / >
2023-01-26 11:48:30 -05:00
< / SubscriptionDashboardProvider >
)
screen . getByText (
'Your subscription has been canceled and will terminate on' ,
{ exact : false }
)
screen . getByText ( canceledSubscription . recurly . nextPaymentDueAt , {
exact : false ,
} )
screen . getByText ( 'No further payments will be taken.' , { exact : false } )
screen . getByText (
'Get the most out of your Overleaf subscription by checking out the list of' ,
{ exact : false }
)
screen . getByRole ( 'link' , { name : 'View Your Invoices' } )
screen . getByRole ( 'button' , { name : 'Reactivate your subscription' } )
} )
it ( 'renders the expired dash' , function ( ) {
2023-02-07 10:38:04 -05:00
window . metaAttributesCache . set (
'ol-subscription' ,
pastDueExpiredSubscription
)
2023-01-26 11:48:30 -05:00
render (
< SubscriptionDashboardProvider >
2023-02-07 10:38:04 -05:00
< PersonalSubscription / >
2023-01-26 11:48:30 -05:00
< / SubscriptionDashboardProvider >
)
screen . getByText ( 'Your subscription has expired.' )
} )
it ( 'renders error message when an unknown subscription state' , function ( ) {
2023-01-26 12:01:54 -05:00
const withStateDeleted = Object . assign (
{ } ,
JSON . parse ( JSON . stringify ( annualActiveSubscription ) )
)
2023-01-26 11:48:30 -05:00
withStateDeleted . recurly . state = undefined
2023-02-07 10:38:04 -05:00
window . metaAttributesCache . set ( 'ol-subscription' , withStateDeleted )
2023-01-26 11:48:30 -05:00
render (
< SubscriptionDashboardProvider >
2023-02-07 10:38:04 -05:00
< PersonalSubscription / >
2023-01-26 11:48:30 -05:00
< / SubscriptionDashboardProvider >
)
screen . getByText (
'There is a problem with your subscription. Please contact us for more information.'
)
} )
} )
describe ( 'past due subscription' , function ( ) {
it ( 'renders error alert' , function ( ) {
2023-02-07 10:38:04 -05:00
window . metaAttributesCache . set (
'ol-subscription' ,
pastDueExpiredSubscription
)
2023-01-26 11:48:30 -05:00
render (
< SubscriptionDashboardProvider >
2023-02-07 10:38:04 -05:00
< PersonalSubscription / >
2023-01-26 11:48:30 -05:00
< / SubscriptionDashboardProvider >
)
screen . getByRole ( 'alert' )
screen . getByText (
'Your account currently has a past due invoice. You will not be able to change your plan until this is resolved.' ,
{ exact : false }
)
const invoiceLinks = screen . getAllByText ( 'View Your Invoices' , {
exact : false ,
} )
expect ( invoiceLinks . length ) . to . equal ( 2 )
} )
} )
2023-01-26 12:01:54 -05:00
describe ( 'Recurly JS' , function ( ) {
const recurlyFailedToLoadText =
'Sorry, there was an error talking to our payment provider. Please try again in a few moments. If you are using any ad or script blocking extensions in your browser, you may need to temporarily disable them.'
it ( 'shows an alert and hides "Change plan" option when Recurly did not load' , function ( ) {
2023-02-02 04:15:04 -05:00
// @ts-ignore
2023-01-26 12:01:54 -05:00
delete window . recurly
2023-02-07 10:38:04 -05:00
window . metaAttributesCache . set (
'ol-subscription' ,
annualActiveSubscription
)
2023-01-26 12:01:54 -05:00
render (
< SubscriptionDashboardProvider >
2023-02-07 10:38:04 -05:00
< PersonalSubscription / >
2023-01-26 12:01:54 -05:00
< / SubscriptionDashboardProvider >
)
screen . getByRole ( 'alert' )
screen . getByText ( recurlyFailedToLoadText )
expect ( screen . queryByText ( 'Change plan' ) ) . to . be . null
} )
it ( 'should not show an alert and should show "Change plan" option when Recurly did load' , function ( ) {
2023-02-07 10:38:04 -05:00
window . metaAttributesCache . set (
'ol-subscription' ,
annualActiveSubscription
)
2023-01-26 12:01:54 -05:00
render (
< SubscriptionDashboardProvider >
2023-02-07 10:38:04 -05:00
< PersonalSubscription / >
2023-01-26 12:01:54 -05:00
< / SubscriptionDashboardProvider >
)
expect ( screen . queryByRole ( 'alert' ) ) . to . be . null
screen . getByText ( 'Change plan' )
} )
} )
2023-01-26 11:48:30 -05:00
} )