personal-subscription.test.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import { expect } from 'chai'
  2. import {
  3. screen,
  4. fireEvent,
  5. waitForElementToBeRemoved,
  6. within,
  7. } from '@testing-library/react'
  8. import PersonalSubscription from '../../../../../../frontend/js/features/subscription/components/dashboard/personal-subscription'
  9. import {
  10. annualActiveSubscription,
  11. canceledSubscription,
  12. customSubscription,
  13. pastDueExpiredSubscription,
  14. } from '../../fixtures/subscriptions'
  15. import {
  16. cleanUpContext,
  17. renderWithSubscriptionDashContext,
  18. } from '../../helpers/render-with-subscription-dash-context'
  19. import { reactivateSubscriptionUrl } from '../../../../../../frontend/js/features/subscription/data/subscription-url'
  20. import fetchMock from 'fetch-mock'
  21. import sinon from 'sinon'
  22. import * as useLocationModule from '../../../../../../frontend/js/shared/hooks/use-location'
  23. describe('<PersonalSubscription />', function () {
  24. afterEach(function () {
  25. cleanUpContext()
  26. })
  27. describe('no subscription', function () {
  28. it('returns empty container', function () {
  29. const { container } = renderWithSubscriptionDashContext(
  30. <PersonalSubscription />
  31. )
  32. expect(container.firstChild).to.be.null
  33. })
  34. })
  35. describe('custom subscription', function () {
  36. it('displays contact support message', function () {
  37. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  38. metaTags: [{ name: 'ol-subscription', value: customSubscription }],
  39. })
  40. screen.getByText('Please', { exact: false })
  41. screen.getByText('contact support', { exact: false })
  42. screen.getByText('to make changes to your plan', { exact: false })
  43. })
  44. })
  45. describe('subscription states ', function () {
  46. let reloadStub: sinon.SinonStub
  47. beforeEach(function () {
  48. reloadStub = sinon.stub()
  49. this.locationStub = sinon.stub(useLocationModule, 'useLocation').returns({
  50. assign: sinon.stub(),
  51. reload: reloadStub,
  52. })
  53. })
  54. afterEach(function () {
  55. this.locationStub.restore()
  56. })
  57. it('renders the active dash', function () {
  58. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  59. metaTags: [
  60. { name: 'ol-subscription', value: annualActiveSubscription },
  61. ],
  62. })
  63. screen.getByText('You are currently subscribed to the', { exact: false })
  64. })
  65. it('renders the canceled dash', function () {
  66. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  67. metaTags: [{ name: 'ol-subscription', value: canceledSubscription }],
  68. })
  69. screen.getByText(
  70. 'Your subscription has been canceled and will terminate on',
  71. { exact: false }
  72. )
  73. screen.getByText(canceledSubscription.recurly!.nextPaymentDueAt, {
  74. exact: false,
  75. })
  76. screen.getByText('No further payments will be taken.', { exact: false })
  77. screen.getByText(
  78. 'Get the most out of your Overleaf subscription by checking out the list of',
  79. { exact: false }
  80. )
  81. screen.getByRole('link', { name: 'View Your Invoices' })
  82. screen.getByRole('button', { name: 'Reactivate your subscription' })
  83. })
  84. it('reactivates canceled plan', async function () {
  85. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  86. metaTags: [{ name: 'ol-subscription', value: canceledSubscription }],
  87. })
  88. const reactivateBtn = screen.getByRole<HTMLButtonElement>('button', {
  89. name: 'Reactivate your subscription',
  90. })
  91. // 1st click - fail
  92. fetchMock.postOnce(reactivateSubscriptionUrl, 400)
  93. fireEvent.click(reactivateBtn)
  94. expect(reactivateBtn.disabled).to.be.true
  95. await fetchMock.flush(true)
  96. expect(reactivateBtn.disabled).to.be.false
  97. expect(reloadStub).not.to.have.been.called
  98. fetchMock.reset()
  99. // 2nd click - success
  100. fetchMock.postOnce(reactivateSubscriptionUrl, 200)
  101. fireEvent.click(reactivateBtn)
  102. await fetchMock.flush(true)
  103. expect(reloadStub).to.have.been.calledOnce
  104. expect(reactivateBtn.disabled).to.be.true
  105. fetchMock.reset()
  106. })
  107. it('renders the expired dash', function () {
  108. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  109. metaTags: [
  110. { name: 'ol-subscription', value: pastDueExpiredSubscription },
  111. ],
  112. })
  113. screen.getByText('Your subscription has expired.')
  114. })
  115. it('renders error message when an unknown subscription state', function () {
  116. const withStateDeleted = Object.assign(
  117. {},
  118. JSON.parse(JSON.stringify(annualActiveSubscription))
  119. )
  120. withStateDeleted.recurly.state = undefined
  121. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  122. metaTags: [{ name: 'ol-subscription', value: withStateDeleted }],
  123. })
  124. screen.getByText(
  125. 'There is a problem with your subscription. Please contact us for more information.'
  126. )
  127. })
  128. })
  129. describe('past due subscription', function () {
  130. it('renders error alert', function () {
  131. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  132. metaTags: [
  133. { name: 'ol-subscription', value: pastDueExpiredSubscription },
  134. ],
  135. })
  136. screen.getByRole('alert')
  137. screen.getByText(
  138. 'Your account currently has a past due invoice. You will not be able to change your plan until this is resolved.',
  139. { exact: false }
  140. )
  141. const invoiceLinks = screen.getAllByText('View Your Invoices', {
  142. exact: false,
  143. })
  144. expect(invoiceLinks.length).to.equal(2)
  145. })
  146. })
  147. describe('Recurly JS', function () {
  148. const recurlyFailedToLoadText =
  149. '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.'
  150. it('shows an alert and hides "Change plan" option when Recurly did not load', function () {
  151. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  152. metaTags: [
  153. { name: 'ol-subscription', value: annualActiveSubscription },
  154. ],
  155. recurlyNotLoaded: true,
  156. })
  157. screen.getByRole('alert')
  158. screen.getByText(recurlyFailedToLoadText)
  159. expect(screen.queryByText('Change plan')).to.be.null
  160. })
  161. it('should not show an alert and should show "Change plan" option when Recurly did load', function () {
  162. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  163. metaTags: [
  164. { name: 'ol-subscription', value: annualActiveSubscription },
  165. ],
  166. })
  167. expect(screen.queryByRole('alert')).to.be.null
  168. screen.getByText('Change plan')
  169. })
  170. })
  171. it('shows different recurly email address section', async function () {
  172. fetchMock.post('/user/subscription/account/email', 200)
  173. const usersEmail = 'foo@example.com'
  174. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  175. metaTags: [
  176. { name: 'ol-subscription', value: annualActiveSubscription },
  177. { name: 'ol-usersEmail', value: usersEmail },
  178. ],
  179. })
  180. const billingText = screen.getByText(
  181. /your billing email address is currently/i
  182. ).textContent
  183. expect(billingText).to.contain(
  184. `Your billing email address is currently ${annualActiveSubscription.recurly.account.email}.` +
  185. ` If needed you can update your billing address to ${usersEmail}`
  186. )
  187. const submitBtn = screen.getByRole<HTMLButtonElement>('button', {
  188. name: /update/i,
  189. })
  190. expect(submitBtn.disabled).to.be.false
  191. fireEvent.click(submitBtn)
  192. expect(submitBtn.disabled).to.be.true
  193. expect(
  194. screen.getByRole<HTMLButtonElement>('button', { name: /updating/i })
  195. .disabled
  196. ).to.be.true
  197. await waitForElementToBeRemoved(() =>
  198. screen.getByText(/your billing email address is currently/i)
  199. )
  200. within(screen.getByRole('alert')).getByText(
  201. /your billing email address was successfully updated/i
  202. )
  203. expect(screen.queryByRole('button', { name: /update/i })).to.be.null
  204. expect(screen.queryByRole('button', { name: /updating/i })).to.be.null
  205. })
  206. })