personal-subscription.test.tsx 7.8 KB

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