pause-modal.test.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { fireEvent, screen, waitFor } from '@testing-library/react'
  2. import fetchMock from 'fetch-mock'
  3. import sinon from 'sinon'
  4. import { expect } from 'chai'
  5. import {
  6. annualActiveSubscription,
  7. groupActiveSubscription,
  8. monthlyActiveCollaborator,
  9. trialSubscription,
  10. } from '../../fixtures/subscriptions'
  11. import { renderActiveSubscription } from '../../helpers/render-active-subscription'
  12. import { location } from '@/shared/components/location'
  13. import { MetaTag } from '@/utils/meta'
  14. const pauseSubscriptionSplitTestMeta: MetaTag[] = [
  15. { name: 'ol-splitTestVariants', value: { 'pause-subscription': 'enabled' } },
  16. ]
  17. function renderSubscriptionWithPauseSupport(
  18. subscription = monthlyActiveCollaborator
  19. ) {
  20. return renderActiveSubscription(subscription, pauseSubscriptionSplitTestMeta)
  21. }
  22. function clickCancelButton() {
  23. const button = screen.getByRole('button', {
  24. name: /Cancel your subscription/i,
  25. })
  26. fireEvent.click(button)
  27. }
  28. function clickDurationSelect() {
  29. const pauseDurationSelect = screen.getByLabelText('Pause subscription for', {
  30. selector: 'input',
  31. })
  32. fireEvent.click(pauseDurationSelect)
  33. }
  34. function clickSubmitButton() {
  35. const buttonConfirm = screen.getByRole('button', {
  36. name: 'Pause subscription',
  37. })
  38. fireEvent.click(buttonConfirm)
  39. }
  40. describe('<PauseSubscriptionModal />', function () {
  41. beforeEach(function () {
  42. this.locationWrapperSandbox = sinon.createSandbox()
  43. this.locationWrapperStub = this.locationWrapperSandbox.stub(location)
  44. this.locationWrapperStub.toString.returns(
  45. 'https://www.dev-overleaf.com/user/subscription'
  46. )
  47. this.replaceStateStub = sinon.stub(window.history, 'replaceState')
  48. })
  49. afterEach(function () {
  50. fetchMock.removeRoutes().clearHistory()
  51. this.locationWrapperSandbox.restore()
  52. this.replaceStateStub.restore()
  53. })
  54. it('does not render with an annual subscription', async function () {
  55. renderSubscriptionWithPauseSupport(annualActiveSubscription)
  56. clickCancelButton()
  57. // goes straight to cancel
  58. await screen.findByText('We’d love you to stay')
  59. })
  60. it('does not render with a group plan', async function () {
  61. renderSubscriptionWithPauseSupport(groupActiveSubscription)
  62. clickCancelButton()
  63. // goes straight to cancel
  64. await screen.findByText('We’d love you to stay')
  65. })
  66. it('does not render when in a trial', async function () {
  67. renderSubscriptionWithPauseSupport(trialSubscription)
  68. clickCancelButton()
  69. await screen.findByText('We’d love you to stay')
  70. })
  71. it('renders when trying to cancel subscription', async function () {
  72. renderSubscriptionWithPauseSupport()
  73. clickCancelButton()
  74. await screen.findByText('Pause instead, to pick up where you left off')
  75. })
  76. it('renders options for pause duration', async function () {
  77. renderSubscriptionWithPauseSupport()
  78. clickCancelButton()
  79. clickDurationSelect()
  80. await screen.findByRole('option', { name: '1 month' })
  81. await screen.findByRole('option', { name: '2 months' })
  82. await screen.findByRole('option', { name: '3 months' })
  83. })
  84. it('changes to selected duration', async function () {
  85. renderSubscriptionWithPauseSupport()
  86. clickCancelButton()
  87. clickDurationSelect()
  88. const twoMonthsOption = await screen.findByRole('option', {
  89. name: '2 months',
  90. selected: false,
  91. })
  92. fireEvent.click(twoMonthsOption)
  93. clickDurationSelect()
  94. await screen.findByRole('option', { name: '2 months', selected: true })
  95. })
  96. it('shows error if pausing failed', async function () {
  97. const endPointResponse = {
  98. status: 500,
  99. }
  100. fetchMock.post(`/user/subscription/pause/1`, endPointResponse)
  101. renderSubscriptionWithPauseSupport()
  102. clickCancelButton()
  103. clickSubmitButton()
  104. await screen.findByText('Sorry, something went wrong. ', {
  105. exact: false,
  106. })
  107. })
  108. it('reloads if pause successful', async function () {
  109. const endPointResponse = {
  110. status: 200,
  111. }
  112. fetchMock.post(`/user/subscription/pause/1`, endPointResponse)
  113. renderSubscriptionWithPauseSupport()
  114. clickCancelButton()
  115. clickSubmitButton()
  116. const reloadStub = this.locationWrapperStub.reload
  117. await waitFor(() => {
  118. expect(reloadStub).to.have.been.called
  119. })
  120. })
  121. })