emails-section-actions.test.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. const userEmailData: UserEmailData = {
  16. confirmedAt: '2022-03-10T10:59:44.139Z',
  17. email: 'bar@overleaf.com',
  18. default: false,
  19. }
  20. const userEmailData2: UserEmailData & { affiliation: Affiliation } = {
  21. affiliation: {
  22. inReconfirmNotificationPeriod: false,
  23. institution: {
  24. confirmed: false,
  25. } as Institution,
  26. } as Affiliation,
  27. confirmedAt: '2022-03-10T10:59:44.139Z',
  28. email: 'bar@overleaf.com',
  29. default: false,
  30. }
  31. describe('email actions - make primary', function () {
  32. beforeEach(function () {
  33. window.metaAttributesCache.set('ol-ExposedSettings', {
  34. hasAffiliationsFeature: true,
  35. })
  36. fetchMock.reset()
  37. })
  38. afterEach(function () {
  39. window.metaAttributesCache = new Map()
  40. fetchMock.reset()
  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. 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. 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. window.metaAttributesCache.set('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. 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(withinModal.getByRole('button', { name: /confirm/i }))
  147. expect(screen.queryByRole('dialog')).to.be.null
  148. await waitForElementToBeRemoved(() =>
  149. screen.getByRole('button', { name: /sending/i, hidden: true })
  150. )
  151. }
  152. it('shows confirmation modal and closes it', async function () {
  153. fetchMock.get('/user/emails?ensureAffiliation=true', [userEmailData])
  154. render(<EmailsSection />)
  155. const button = await screen.findByRole('button', {
  156. name: /make primary/i,
  157. })
  158. fireEvent.click(button)
  159. const withinModal = within(screen.getByRole('dialog'))
  160. withinModal.getByText(
  161. /do you want to change your primary email address to .*/i
  162. )
  163. withinModal.getByText(
  164. /this will be the email address to use if you log in with an email address and password/i
  165. )
  166. withinModal.getByText(
  167. /important .* notifications will be sent to this email address/i
  168. )
  169. withinModal.getByRole('button', { name: /confirm/i })
  170. fireEvent.click(withinModal.getByRole('button', { name: /cancel/i }))
  171. expect(screen.queryByRole('dialog')).to.be.null
  172. })
  173. it('shows loader and removes button', async function () {
  174. fetchMock
  175. .get('/user/emails?ensureAffiliation=true', [userEmailData])
  176. .post('/user/emails/default', 200)
  177. render(<EmailsSection />)
  178. await confirmPrimaryEmail()
  179. expect(
  180. screen.queryByText(
  181. /an error has occurred while performing your request/i
  182. )
  183. ).to.be.null
  184. expect(screen.queryByRole('button', { name: /make primary/i })).to.be.null
  185. })
  186. it('shows error', async function () {
  187. fetchMock
  188. .get('/user/emails?ensureAffiliation=true', [userEmailData])
  189. .post('/user/emails/default', 503)
  190. render(<EmailsSection />)
  191. await confirmPrimaryEmail()
  192. screen.getByText(/sorry, something went wrong/i)
  193. await screen.findByRole('button', { name: /make primary/i })
  194. })
  195. })
  196. })
  197. describe('email actions - delete', function () {
  198. beforeEach(function () {
  199. window.metaAttributesCache.set('ol-ExposedSettings', {
  200. hasAffiliationsFeature: true,
  201. })
  202. fetchMock.reset()
  203. })
  204. afterEach(function () {
  205. window.metaAttributesCache = new Map()
  206. fetchMock.reset()
  207. })
  208. it('shows loader when deleting and removes the row', async function () {
  209. fetchMock
  210. .get('/user/emails?ensureAffiliation=true', [userEmailData])
  211. .post('/user/emails/delete', 200)
  212. render(<EmailsSection />)
  213. const button = await screen.findByRole('button', { name: /remove/i })
  214. fireEvent.click(button)
  215. await waitForElementToBeRemoved(() =>
  216. screen.queryByText(userEmailData.email)
  217. )
  218. })
  219. it('shows error when deleting', async function () {
  220. fetchMock
  221. .get('/user/emails?ensureAffiliation=true', [userEmailData])
  222. .post('/user/emails/delete', 503)
  223. render(<EmailsSection />)
  224. const button = await screen.findByRole('button', { name: /remove/i })
  225. fireEvent.click(button)
  226. await screen.queryByText(/sorry, something went wrong/i)
  227. screen.getByRole('button', { name: /remove/i })
  228. })
  229. })