modal-form.test.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import { fireEvent, screen, render, waitFor } from '@testing-library/react'
  4. import fetchMock from 'fetch-mock'
  5. import LeaveModalForm from '../../../../../../frontend/js/features/settings/components/leave/modal-form'
  6. describe('<LeaveModalForm />', function () {
  7. beforeEach(function () {
  8. window.metaAttributesCache = new Map()
  9. window.metaAttributesCache.set('ol-usersEmail', 'foo@bar.com')
  10. window.metaAttributesCache.set('ol-ExposedSettings', { isOverleaf: true })
  11. })
  12. afterEach(function () {
  13. window.metaAttributesCache = new Map()
  14. fetchMock.reset()
  15. })
  16. it('validates form', async function () {
  17. const setIsFormValid = sinon.stub()
  18. render(
  19. <LeaveModalForm
  20. setInFlight={() => {}}
  21. isFormValid={false}
  22. setIsFormValid={setIsFormValid}
  23. />
  24. )
  25. const emailInput = screen.getByLabelText('Email')
  26. fireEvent.change(emailInput, { target: { value: 'foo@bar.com' } })
  27. const passwordInput = screen.getByLabelText('Password')
  28. fireEvent.change(passwordInput, { target: { value: 'foobar' } })
  29. const checkbox = screen.getByLabelText(
  30. 'I understand this will delete all projects in my Overleaf account with email address foo@bar.com'
  31. )
  32. fireEvent.click(checkbox)
  33. const setIsFormValidCalls = setIsFormValid.getCalls()
  34. const lastSetIsFormValidCall = setIsFormValidCalls.pop()
  35. expect(lastSetIsFormValidCall.args[0]).to.be.true
  36. for (const setIsFormValidCall of setIsFormValidCalls) {
  37. expect(setIsFormValidCall.args[0]).to.be.false
  38. }
  39. })
  40. describe('submits', async function () {
  41. let setInFlight
  42. let setIsFormValid
  43. let deleteMock
  44. let locationStub
  45. const originalLocation = window.location
  46. beforeEach(function () {
  47. setInFlight = sinon.stub()
  48. setIsFormValid = sinon.stub()
  49. deleteMock = fetchMock.post('/user/delete', 200)
  50. locationStub = sinon.stub()
  51. Object.defineProperty(window, 'location', {
  52. value: {
  53. assign: locationStub,
  54. },
  55. })
  56. window.metaAttributesCache.set('ol-ExposedSettings', { isOverleaf: true })
  57. })
  58. afterEach(function () {
  59. fetchMock.reset()
  60. Object.defineProperty(window, 'location', {
  61. value: originalLocation,
  62. })
  63. })
  64. it('with valid form', async function () {
  65. render(
  66. <LeaveModalForm
  67. setInFlight={setInFlight}
  68. isFormValid
  69. setIsFormValid={setIsFormValid}
  70. />
  71. )
  72. fireEvent.submit(screen.getByLabelText('Email'))
  73. sinon.assert.calledOnce(setInFlight)
  74. sinon.assert.calledWithMatch(setInFlight, true)
  75. expect(deleteMock.called()).to.be.true
  76. await waitFor(() => {
  77. sinon.assert.calledTwice(setInFlight)
  78. sinon.assert.calledWithMatch(setInFlight, false)
  79. sinon.assert.calledOnce(locationStub)
  80. sinon.assert.calledWithMatch(locationStub, '/login')
  81. })
  82. })
  83. it('with invalid form', async function () {
  84. render(
  85. <LeaveModalForm
  86. setInFlight={setInFlight}
  87. isFormValid={false}
  88. setIsFormValid={setIsFormValid}
  89. />
  90. )
  91. fireEvent.submit(screen.getByLabelText('Email'))
  92. expect(deleteMock.called()).to.be.false
  93. sinon.assert.notCalled(setInFlight)
  94. })
  95. })
  96. it('handles credentials error without Saas tip', async function () {
  97. window.metaAttributesCache.set('ol-ExposedSettings', { isOverleaf: false })
  98. fetchMock.post('/user/delete', 403)
  99. render(
  100. <LeaveModalForm
  101. setInFlight={() => {}}
  102. isFormValid
  103. setIsFormValid={() => {}}
  104. />
  105. )
  106. fireEvent.submit(screen.getByLabelText('Email'))
  107. await waitFor(() => {
  108. screen.getByText(/Your email or password is incorrect. Please try again/)
  109. })
  110. expect(screen.queryByText(/If you cannot remember your password/)).to.not
  111. .exist
  112. })
  113. it('handles credentials error with Saas tip', async function () {
  114. fetchMock.post('/user/delete', 403)
  115. render(
  116. <LeaveModalForm
  117. setInFlight={() => {}}
  118. isFormValid
  119. setIsFormValid={() => {}}
  120. />
  121. )
  122. fireEvent.submit(screen.getByLabelText('Email'))
  123. await waitFor(() => {
  124. screen.getByText(/Your email or password is incorrect. Please try again/)
  125. })
  126. screen.getByText(/If you cannot remember your password/)
  127. const link = screen.getByRole('link', { name: 'reset your password' })
  128. expect(link.getAttribute('href')).to.equal('/user/password/reset')
  129. })
  130. it('handles subscription error', async function () {
  131. fetchMock.post('/user/delete', {
  132. status: 422,
  133. body: {
  134. error: 'SubscriptionAdminDeletionError',
  135. },
  136. })
  137. render(
  138. <LeaveModalForm
  139. setInFlight={() => {}}
  140. isFormValid
  141. setIsFormValid={() => {}}
  142. />
  143. )
  144. fireEvent.submit(screen.getByLabelText('Email'))
  145. await waitFor(() => {
  146. screen.getByText(
  147. 'You cannot delete your account while on a subscription. Please cancel your subscription and try again. If you keep seeing this message please contact us.'
  148. )
  149. })
  150. })
  151. it('handles generic error', async function () {
  152. fetchMock.post('/user/delete', 500)
  153. render(
  154. <LeaveModalForm
  155. setInFlight={() => {}}
  156. isFormValid
  157. setIsFormValid={() => {}}
  158. />
  159. )
  160. fireEvent.submit(screen.getByLabelText('Email'))
  161. await waitFor(() => {
  162. screen.getByText(
  163. 'Sorry, something went wrong deleting your account. Please try again in a minute.'
  164. )
  165. })
  166. })
  167. })