emails-section-actions.test.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import {
  2. render,
  3. screen,
  4. waitForElementToBeRemoved,
  5. within,
  6. fireEvent,
  7. } from '@testing-library/react'
  8. import userEvent from '@testing-library/user-event'
  9. import { expect } from 'chai'
  10. import { UserEmailData } from '../../../../../../types/user-email'
  11. import fetchMock from 'fetch-mock'
  12. import EmailsSection from '../../../../../../frontend/js/features/settings/components/emails-section'
  13. import { Institution } from '../../../../../../types/institution'
  14. import { Affiliation } from '../../../../../../types/affiliation'
  15. import getMeta from '@/utils/meta'
  16. const userEmailData: UserEmailData = {
  17. confirmedAt: '2022-03-10T10:59:44.139Z',
  18. email: 'bar@overleaf.com',
  19. default: false,
  20. }
  21. const userEmailData2: UserEmailData & { affiliation: Affiliation } = {
  22. affiliation: {
  23. inReconfirmNotificationPeriod: false,
  24. institution: {
  25. confirmed: false,
  26. } as Institution,
  27. } as Affiliation,
  28. confirmedAt: '2022-03-10T10:59:44.139Z',
  29. email: 'bar@overleaf.com',
  30. default: false,
  31. }
  32. describe('email actions - make primary', function () {
  33. beforeEach(function () {
  34. Object.assign(getMeta('ol-ExposedSettings'), {
  35. hasAffiliationsFeature: true,
  36. })
  37. fetchMock.removeRoutes().clearHistory()
  38. })
  39. afterEach(function () {
  40. fetchMock.removeRoutes().clearHistory()
  41. })
  42. describe('disabled `make primary` button', function () {
  43. it('when renders with unconfirmed email', async function () {
  44. const userEmailDataCopy = { ...userEmailData2 }
  45. const { confirmedAt: _, ...userEmailData } = userEmailDataCopy
  46. fetchMock.get('/user/emails?ensureAffiliation=true', [userEmailData])
  47. render(<EmailsSection />)
  48. const button = (await screen.findByRole('button', {
  49. name: /make primary/i,
  50. })) as HTMLButtonElement
  51. expect(button.disabled).to.be.true
  52. })
  53. it('when renders with email in reconfirmation period', async function () {
  54. const userEmailDataCopy = {
  55. ...userEmailData2,
  56. affiliation: {
  57. ...userEmailData2.affiliation,
  58. inReconfirmNotificationPeriod: true,
  59. },
  60. }
  61. fetchMock.get('/user/emails?ensureAffiliation=true', [userEmailDataCopy])
  62. render(<EmailsSection />)
  63. const button = (await screen.findByRole('button', {
  64. name: /make primary/i,
  65. })) as HTMLButtonElement
  66. expect(button.disabled).to.be.true
  67. })
  68. })
  69. describe('button tooltips', function () {
  70. it('when the email is in reconfirmation period', async function () {
  71. const userEmailDataCopy = {
  72. ...userEmailData2,
  73. affiliation: {
  74. ...userEmailData2.affiliation,
  75. inReconfirmNotificationPeriod: true,
  76. },
  77. }
  78. fetchMock.get('/user/emails?ensureAffiliation=true', [userEmailDataCopy])
  79. render(<EmailsSection />)
  80. const button = (await screen.findByRole('button', {
  81. name: /make primary/i,
  82. })) as HTMLButtonElement
  83. await userEvent.hover(button.parentElement!)
  84. await screen.findByText(
  85. /Please confirm your affiliation before making this the primary/i
  86. )
  87. })
  88. it('when the email is confirmed', async function () {
  89. const userEmailDataCopy = { ...userEmailData2 }
  90. fetchMock.get('/user/emails?ensureAffiliation=true', [userEmailDataCopy])
  91. render(<EmailsSection />)
  92. const button = (await screen.findByRole('button', {
  93. name: /make primary/i,
  94. })) as HTMLButtonElement
  95. await userEvent.hover(button.parentElement!)
  96. await screen.findByText('Make this the primary email, used to log in', {
  97. exact: false,
  98. })
  99. })
  100. it('when not linked to institution', async function () {
  101. Object.assign(getMeta('ol-ExposedSettings'), {
  102. hasAffiliationsFeature: true,
  103. hasSamlFeature: true,
  104. })
  105. const userEmailDataCopy = { ...userEmailData2 }
  106. const { confirmedAt: _, ...userEmailData } = userEmailDataCopy
  107. const userEmailDataCopy1 = { ...userEmailData }
  108. const userEmailDataCopy2 = {
  109. ...userEmailData,
  110. email: 'baz@overleaf.com',
  111. affiliation: {
  112. ...userEmailData.affiliation,
  113. institution: {
  114. ...userEmailData.affiliation.institution,
  115. id: 123,
  116. confirmed: true,
  117. isUniversity: true,
  118. ssoEnabled: true,
  119. },
  120. },
  121. }
  122. fetchMock.get('/user/emails?ensureAffiliation=true', [
  123. userEmailDataCopy1,
  124. userEmailDataCopy2,
  125. ])
  126. render(<EmailsSection />)
  127. const buttons = (await screen.findAllByRole('button', {
  128. name: /make primary/i,
  129. })) as HTMLButtonElement[]
  130. await userEvent.hover(buttons[1].parentElement!)
  131. await screen.findByText(
  132. 'Please confirm your email by linking to your institutional account before making it the primary email',
  133. {
  134. exact: false,
  135. }
  136. )
  137. })
  138. })
  139. describe('make an email primary', function () {
  140. const confirmPrimaryEmail = async () => {
  141. const button = await screen.findByRole('button', {
  142. name: /make primary/i,
  143. })
  144. fireEvent.click(button)
  145. const withinModal = within(screen.getByRole('dialog'))
  146. fireEvent.click(
  147. withinModal.getByRole('button', { name: 'Change primary email' })
  148. )
  149. await waitForElementToBeRemoved(() => screen.getByRole('dialog'))
  150. }
  151. it('shows confirmation modal and closes it', async function () {
  152. fetchMock.get('/user/emails?ensureAffiliation=true', [userEmailData])
  153. render(<EmailsSection />)
  154. const button = await screen.findByRole('button', {
  155. name: /make primary/i,
  156. })
  157. fireEvent.click(button)
  158. const withinModal = within(screen.getByRole('dialog'))
  159. withinModal.getByText(
  160. /do you want to change your primary email address to .*/i
  161. )
  162. withinModal.getByText(
  163. /this will be the email address to use if you log in with an email address and password/i
  164. )
  165. withinModal.getByText(
  166. /important .* notifications will be sent to this email address/i
  167. )
  168. withinModal.getByRole('button', { name: 'Change primary email' })
  169. fireEvent.click(withinModal.getByRole('button', { name: /cancel/i }))
  170. await waitForElementToBeRemoved(screen.getByRole('dialog'))
  171. })
  172. it('shows loader and removes button', async function () {
  173. fetchMock
  174. .get('/user/emails?ensureAffiliation=true', [userEmailData])
  175. .post('/user/emails/default?delete-unconfirmed-primary', 200)
  176. render(<EmailsSection />)
  177. await confirmPrimaryEmail()
  178. expect(
  179. screen.queryByText(
  180. /an error has occurred while performing your request/i
  181. )
  182. ).to.be.null
  183. expect(screen.queryByRole('button', { name: /make primary/i })).to.be.null
  184. })
  185. it('shows error', async function () {
  186. fetchMock
  187. .get('/user/emails?ensureAffiliation=true', [userEmailData])
  188. .post('/user/emails/default?delete-unconfirmed-primary', 503)
  189. render(<EmailsSection />)
  190. await confirmPrimaryEmail()
  191. screen.getByText(/sorry, something went wrong/i)
  192. await screen.findByRole('button', { name: /make primary/i })
  193. })
  194. })
  195. })
  196. describe('email actions - delete', function () {
  197. beforeEach(function () {
  198. Object.assign(getMeta('ol-ExposedSettings'), {
  199. hasAffiliationsFeature: true,
  200. })
  201. fetchMock.removeRoutes().clearHistory()
  202. })
  203. afterEach(function () {
  204. fetchMock.removeRoutes().clearHistory()
  205. })
  206. it('shows loader when deleting and removes the row', async function () {
  207. fetchMock
  208. .get('/user/emails?ensureAffiliation=true', [userEmailData])
  209. .post('/user/emails/delete', 200)
  210. render(<EmailsSection />)
  211. const button = await screen.findByRole('button', { name: /remove/i })
  212. fireEvent.click(button)
  213. await waitForElementToBeRemoved(() =>
  214. screen.queryByText(userEmailData.email)
  215. )
  216. })
  217. it('shows error when deleting', async function () {
  218. fetchMock
  219. .get('/user/emails?ensureAffiliation=true', [userEmailData])
  220. .post('/user/emails/delete', 503)
  221. render(<EmailsSection />)
  222. const button = await screen.findByRole('button', { name: /remove/i })
  223. fireEvent.click(button)
  224. await screen.queryByText(/sorry, something went wrong/i)
  225. screen.getByRole('button', { name: /remove/i })
  226. })
  227. })