emails-section-actions.test.tsx 8.5 KB

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