pause-modal.test.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 * as useLocationModule from '../../../../../../frontend/js/shared/hooks/use-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. reloadStub = sinon.stub()
  43. this.locationStub = sinon.stub(useLocationModule, 'useLocation').returns({
  44. assign: sinon.stub(),
  45. replace: sinon.stub(),
  46. reload: reloadStub,
  47. setHash: sinon.stub(),
  48. toString: sinon
  49. .stub()
  50. .returns('https://www.dev-overleaf.com/user/subscription'),
  51. })
  52. this.replaceStateStub = sinon.stub(window.history, 'replaceState')
  53. })
  54. afterEach(function () {
  55. fetchMock.removeRoutes().clearHistory()
  56. this.locationStub.restore()
  57. this.replaceStateStub.restore()
  58. })
  59. it('does not render with an annual subscription', async function () {
  60. renderSubscriptionWithPauseSupport(annualActiveSubscription)
  61. clickCancelButton()
  62. // goes straight to cancel
  63. await screen.findByText('We’d love you to stay')
  64. })
  65. it('does not render with a group plan', async function () {
  66. renderSubscriptionWithPauseSupport(groupActiveSubscription)
  67. clickCancelButton()
  68. // goes straight to cancel
  69. await screen.findByText('We’d love you to stay')
  70. })
  71. it('does not render when in a trial', async function () {
  72. renderSubscriptionWithPauseSupport(trialSubscription)
  73. clickCancelButton()
  74. await screen.findByText('We’d love you to stay')
  75. })
  76. it('renders when trying to cancel subscription', async function () {
  77. renderSubscriptionWithPauseSupport()
  78. clickCancelButton()
  79. await screen.findByText('Pause instead, to pick up where you left off')
  80. })
  81. let reloadStub: sinon.SinonStub
  82. it('renders options for pause duration', async function () {
  83. renderSubscriptionWithPauseSupport()
  84. clickCancelButton()
  85. clickDurationSelect()
  86. await screen.findByRole('option', { name: '1 month' })
  87. await screen.findByRole('option', { name: '2 months' })
  88. await screen.findByRole('option', { name: '3 months' })
  89. })
  90. it('changes to selected duration', async function () {
  91. renderSubscriptionWithPauseSupport()
  92. clickCancelButton()
  93. clickDurationSelect()
  94. const twoMonthsOption = await screen.findByRole('option', {
  95. name: '2 months',
  96. selected: false,
  97. })
  98. fireEvent.click(twoMonthsOption)
  99. clickDurationSelect()
  100. await screen.findByRole('option', { name: '2 months', selected: true })
  101. })
  102. it('shows error if pausing failed', async function () {
  103. const endPointResponse = {
  104. status: 500,
  105. }
  106. fetchMock.post(`/user/subscription/pause/1`, endPointResponse)
  107. renderSubscriptionWithPauseSupport()
  108. clickCancelButton()
  109. clickSubmitButton()
  110. await screen.findByText('Sorry, something went wrong. ', {
  111. exact: false,
  112. })
  113. })
  114. it('reloads if pause successful', async function () {
  115. const endPointResponse = {
  116. status: 200,
  117. }
  118. fetchMock.post(`/user/subscription/pause/1`, endPointResponse)
  119. renderSubscriptionWithPauseSupport()
  120. clickCancelButton()
  121. clickSubmitButton()
  122. await waitFor(() => {
  123. expect(reloadStub).to.have.been.called
  124. })
  125. })
  126. })