personal-subscription.test.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. replace: sinon.stub(),
  52. reload: reloadStub,
  53. setHash: sinon.stub(),
  54. toString: sinon.stub(),
  55. })
  56. })
  57. afterEach(function () {
  58. this.locationStub.restore()
  59. })
  60. it('renders the active dash', function () {
  61. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  62. metaTags: [
  63. { name: 'ol-subscription', value: annualActiveSubscription },
  64. ],
  65. })
  66. screen.getByRole('heading', { name: /billing/i })
  67. })
  68. it('renders the canceled dash', function () {
  69. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  70. metaTags: [{ name: 'ol-subscription', value: canceledSubscription }],
  71. })
  72. screen.getByText(
  73. 'Your subscription has been canceled and will terminate on',
  74. { exact: false }
  75. )
  76. screen.getByText(canceledSubscription.payment!.nextPaymentDueAt, {
  77. exact: false,
  78. })
  79. screen.getByText('No further payments will be taken.', { exact: false })
  80. screen.getByRole('button', { name: 'View your invoices' })
  81. screen.getByRole('button', { name: 'Reactivate your subscription' })
  82. })
  83. it('reactivates canceled plan', async function () {
  84. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  85. metaTags: [{ name: 'ol-subscription', value: canceledSubscription }],
  86. })
  87. const reactivateBtn = screen.getByRole<HTMLButtonElement>('button', {
  88. name: 'Reactivate your subscription',
  89. })
  90. // 1st click - fail
  91. fetchMock.postOnce(reactivateSubscriptionUrl, 400)
  92. fireEvent.click(reactivateBtn)
  93. expect(reactivateBtn.disabled).to.be.true
  94. await fetchMock.callHistory.flush(true)
  95. expect(reactivateBtn.disabled).to.be.false
  96. expect(reloadStub).not.to.have.been.called
  97. fetchMock.removeRoutes().clearHistory()
  98. // 2nd click - success
  99. fetchMock.postOnce(reactivateSubscriptionUrl, 200)
  100. fireEvent.click(reactivateBtn)
  101. await fetchMock.callHistory.flush(true)
  102. expect(reloadStub).to.have.been.calledOnce
  103. expect(reactivateBtn.disabled).to.be.true
  104. fetchMock.removeRoutes().clearHistory()
  105. })
  106. it('renders the expired dash', function () {
  107. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  108. metaTags: [
  109. { name: 'ol-subscription', value: pastDueExpiredSubscription },
  110. ],
  111. })
  112. screen.getByText('Your subscription has expired.')
  113. })
  114. it('renders error message when an unknown subscription state', function () {
  115. const withStateDeleted = Object.assign(
  116. {},
  117. JSON.parse(JSON.stringify(annualActiveSubscription))
  118. )
  119. withStateDeleted.payment.state = undefined
  120. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  121. metaTags: [{ name: 'ol-subscription', value: withStateDeleted }],
  122. })
  123. screen.getByText(
  124. 'There is a problem with your subscription. Please contact us for more information.'
  125. )
  126. })
  127. })
  128. describe('past due subscription', function () {
  129. it('renders error alert', function () {
  130. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  131. metaTags: [
  132. { name: 'ol-subscription', value: pastDueExpiredSubscription },
  133. ],
  134. })
  135. screen.getByRole('alert')
  136. screen.getByText(
  137. 'Your account currently has a past due invoice. You will not be able to change your plan until this is resolved.',
  138. { exact: false }
  139. )
  140. const invoiceLinks = screen.getAllByText('View Your Invoices', {
  141. exact: false,
  142. })
  143. expect(invoiceLinks.length).to.equal(2)
  144. })
  145. })
  146. describe('Recurly JS', function () {
  147. const recurlyFailedToLoadText =
  148. '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.'
  149. it('shows an alert and hides "Change plan" option when Recurly did not load', function () {
  150. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  151. metaTags: [
  152. { name: 'ol-subscription', value: annualActiveSubscription },
  153. ],
  154. recurlyNotLoaded: true,
  155. })
  156. screen.getByRole('alert')
  157. screen.getByText(recurlyFailedToLoadText)
  158. expect(screen.queryByText('Change plan')).to.be.null
  159. })
  160. it('should not show an alert and should show "Change plan" option when Recurly did load', function () {
  161. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  162. metaTags: [
  163. { name: 'ol-subscription', value: annualActiveSubscription },
  164. ],
  165. })
  166. expect(screen.queryByRole('alert')).to.be.null
  167. screen.getByText('Change plan')
  168. })
  169. })
  170. it('shows different payment email address section', async function () {
  171. fetchMock.post('/user/subscription/account/email', 200)
  172. const usersEmail = 'foo@example.com'
  173. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  174. metaTags: [
  175. { name: 'ol-subscription', value: annualActiveSubscription },
  176. { name: 'ol-usersEmail', value: usersEmail },
  177. ],
  178. })
  179. const billingText = screen.getByText(
  180. /your billing email address is currently/i
  181. ).textContent
  182. expect(billingText).to.contain(
  183. `Your billing email address is currently ${annualActiveSubscription.payment.accountEmail}.` +
  184. ` If needed you can update your billing address to ${usersEmail}`
  185. )
  186. const submitBtn = screen.getByRole<HTMLButtonElement>('button', {
  187. name: /update/i,
  188. })
  189. expect(submitBtn.disabled).to.be.false
  190. fireEvent.click(submitBtn)
  191. expect(submitBtn.disabled).to.be.true
  192. expect(
  193. screen.getByRole<HTMLButtonElement>('button', { name: /updating/i })
  194. .disabled
  195. ).to.be.true
  196. await waitForElementToBeRemoved(() =>
  197. screen.getByText(/your billing email address is currently/i)
  198. )
  199. within(screen.getByRole('alert')).getByText(
  200. /your billing email address was successfully updated/i
  201. )
  202. expect(screen.queryByRole('button', { name: /update/i })).to.be.null
  203. expect(screen.queryByRole('button', { name: /updating/i })).to.be.null
  204. })
  205. })