personal-subscription.test.tsx 7.9 KB

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