personal-subscription.test.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. })
  54. })
  55. afterEach(function () {
  56. this.locationStub.restore()
  57. })
  58. it('renders the active dash', function () {
  59. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  60. metaTags: [
  61. { name: 'ol-subscription', value: annualActiveSubscription },
  62. ],
  63. })
  64. screen.getByText('You are currently subscribed to the', { exact: false })
  65. })
  66. it('renders the canceled dash', function () {
  67. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  68. metaTags: [{ name: 'ol-subscription', value: canceledSubscription }],
  69. })
  70. screen.getByText(
  71. 'Your subscription has been canceled and will terminate on',
  72. { exact: false }
  73. )
  74. screen.getByText(canceledSubscription.recurly!.nextPaymentDueAt, {
  75. exact: false,
  76. })
  77. screen.getByText('No further payments will be taken.', { exact: false })
  78. screen.getByRole('link', { name: 'View Your Invoices' })
  79. screen.getByRole('button', { name: 'Reactivate your subscription' })
  80. })
  81. it('reactivates canceled plan', async function () {
  82. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  83. metaTags: [{ name: 'ol-subscription', value: canceledSubscription }],
  84. })
  85. const reactivateBtn = screen.getByRole<HTMLButtonElement>('button', {
  86. name: 'Reactivate your subscription',
  87. })
  88. // 1st click - fail
  89. fetchMock.postOnce(reactivateSubscriptionUrl, 400)
  90. fireEvent.click(reactivateBtn)
  91. expect(reactivateBtn.disabled).to.be.true
  92. await fetchMock.flush(true)
  93. expect(reactivateBtn.disabled).to.be.false
  94. expect(reloadStub).not.to.have.been.called
  95. fetchMock.reset()
  96. // 2nd click - success
  97. fetchMock.postOnce(reactivateSubscriptionUrl, 200)
  98. fireEvent.click(reactivateBtn)
  99. await fetchMock.flush(true)
  100. expect(reloadStub).to.have.been.calledOnce
  101. expect(reactivateBtn.disabled).to.be.true
  102. fetchMock.reset()
  103. })
  104. it('renders the expired dash', function () {
  105. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  106. metaTags: [
  107. { name: 'ol-subscription', value: pastDueExpiredSubscription },
  108. ],
  109. })
  110. screen.getByText('Your subscription has expired.')
  111. })
  112. it('renders error message when an unknown subscription state', function () {
  113. const withStateDeleted = Object.assign(
  114. {},
  115. JSON.parse(JSON.stringify(annualActiveSubscription))
  116. )
  117. withStateDeleted.recurly.state = undefined
  118. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  119. metaTags: [{ name: 'ol-subscription', value: withStateDeleted }],
  120. })
  121. screen.getByText(
  122. 'There is a problem with your subscription. Please contact us for more information.'
  123. )
  124. })
  125. })
  126. describe('past due subscription', function () {
  127. it('renders error alert', function () {
  128. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  129. metaTags: [
  130. { name: 'ol-subscription', value: pastDueExpiredSubscription },
  131. ],
  132. })
  133. screen.getByRole('alert')
  134. screen.getByText(
  135. 'Your account currently has a past due invoice. You will not be able to change your plan until this is resolved.',
  136. { exact: false }
  137. )
  138. const invoiceLinks = screen.getAllByText('View Your Invoices', {
  139. exact: false,
  140. })
  141. expect(invoiceLinks.length).to.equal(2)
  142. })
  143. })
  144. describe('Recurly JS', function () {
  145. const recurlyFailedToLoadText =
  146. '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.'
  147. it('shows an alert and hides "Change plan" option when Recurly did not load', function () {
  148. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  149. metaTags: [
  150. { name: 'ol-subscription', value: annualActiveSubscription },
  151. ],
  152. recurlyNotLoaded: true,
  153. })
  154. screen.getByRole('alert')
  155. screen.getByText(recurlyFailedToLoadText)
  156. expect(screen.queryByText('Change plan')).to.be.null
  157. })
  158. it('should not show an alert and should show "Change plan" option when Recurly did load', function () {
  159. renderWithSubscriptionDashContext(<PersonalSubscription />, {
  160. metaTags: [
  161. { name: 'ol-subscription', value: annualActiveSubscription },
  162. ],
  163. })
  164. expect(screen.queryByRole('alert')).to.be.null
  165. screen.getByText('Change plan')
  166. })
  167. })
  168. it('shows different recurly email address section', async function () {
  169. fetchMock.post('/user/subscription/account/email', 200)
  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.recurly.account.email}.` +
  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. })