emails-section.test.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import {
  2. render,
  3. screen,
  4. within,
  5. fireEvent,
  6. waitFor,
  7. waitForElementToBeRemoved,
  8. } from '@testing-library/react'
  9. import EmailsSection from '../../../../../../frontend/js/features/settings/components/emails-section'
  10. import { expect } from 'chai'
  11. import fetchMock from 'fetch-mock'
  12. import { UserEmailData } from '../../../../../../types/user-email'
  13. const confirmedUserData: UserEmailData = {
  14. confirmedAt: '2022-03-10T10:59:44.139Z',
  15. email: 'bar@overleaf.com',
  16. default: false,
  17. }
  18. const unconfirmedUserData: UserEmailData = {
  19. email: 'baz@overleaf.com',
  20. default: false,
  21. }
  22. const professionalUserData: UserEmailData = {
  23. affiliation: {
  24. cachedConfirmedAt: null,
  25. cachedPastReconfirmDate: false,
  26. cachedReconfirmedAt: null,
  27. department: 'Art History',
  28. institution: {
  29. commonsAccount: false,
  30. confirmed: true,
  31. id: 1,
  32. isUniversity: false,
  33. name: 'Overleaf',
  34. ssoEnabled: false,
  35. ssoBeta: false,
  36. },
  37. inReconfirmNotificationPeriod: false,
  38. inferred: false,
  39. licence: 'pro_plus',
  40. pastReconfirmDate: false,
  41. portal: { slug: '', templates_count: 1 },
  42. role: 'Reader',
  43. },
  44. confirmedAt: '2022-03-09T10:59:44.139Z',
  45. email: 'foo@overleaf.com',
  46. default: true,
  47. }
  48. const fakeUsersData = [
  49. { ...confirmedUserData },
  50. { ...unconfirmedUserData },
  51. { ...professionalUserData },
  52. ]
  53. describe('<EmailsSection />', function () {
  54. beforeEach(function () {
  55. window.metaAttributesCache.set('ol-ExposedSettings', {
  56. hasAffiliationsFeature: true,
  57. })
  58. fetchMock.reset()
  59. })
  60. afterEach(function () {
  61. window.metaAttributesCache = new Map()
  62. fetchMock.reset()
  63. })
  64. it('renders translated heading', function () {
  65. render(<EmailsSection />)
  66. screen.getByRole('heading', { name: /emails and affiliations/i })
  67. })
  68. it('renders translated description', function () {
  69. render(<EmailsSection />)
  70. screen.getByText(/add additional email addresses/i)
  71. screen.getByText(/to change your primary email/i)
  72. })
  73. it('renders a loading message when loading', async function () {
  74. render(<EmailsSection />)
  75. await screen.findByText(/loading/i)
  76. })
  77. it('renders an error message and hides loading message on error', async function () {
  78. fetchMock.get('/user/emails?ensureAffiliation=true', 500)
  79. render(<EmailsSection />)
  80. await screen.findByText(
  81. /an error has occurred while performing your request/i
  82. )
  83. expect(screen.queryByText(/loading/i)).to.be.null
  84. })
  85. it('renders user emails', async function () {
  86. fetchMock.get('/user/emails?ensureAffiliation=true', fakeUsersData)
  87. render(<EmailsSection />)
  88. await waitFor(() => {
  89. fakeUsersData.forEach(userData => {
  90. screen.getByText(new RegExp(userData.email, 'i'))
  91. })
  92. })
  93. })
  94. it('renders primary status', async function () {
  95. fetchMock.get('/user/emails?ensureAffiliation=true', [professionalUserData])
  96. render(<EmailsSection />)
  97. await screen.findByText(`${professionalUserData.email} (primary)`)
  98. })
  99. it('shows confirmation status for unconfirmed users', async function () {
  100. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  101. render(<EmailsSection />)
  102. await screen.findByText(/please check your inbox/i)
  103. })
  104. it('hides confirmation status for confirmed users', async function () {
  105. fetchMock.get('/user/emails?ensureAffiliation=true', [confirmedUserData])
  106. render(<EmailsSection />)
  107. await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
  108. expect(screen.queryByText(/please check your inbox/i)).to.be.null
  109. })
  110. it('renders resend link', async function () {
  111. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  112. render(<EmailsSection />)
  113. await screen.findByRole('button', { name: /resend confirmation email/i })
  114. })
  115. it('renders professional label', async function () {
  116. fetchMock.get('/user/emails?ensureAffiliation=true', [professionalUserData])
  117. render(<EmailsSection />)
  118. const node = await screen.findByText(professionalUserData.email, {
  119. exact: false,
  120. })
  121. expect(within(node).getByText(/professional/i)).to.exist
  122. })
  123. it('shows loader when resending email', async function () {
  124. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  125. render(<EmailsSection />)
  126. await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
  127. fetchMock.post('/user/emails/resend_confirmation', 200)
  128. const button = screen.getByRole('button', {
  129. name: /resend confirmation email/i,
  130. })
  131. fireEvent.click(button)
  132. expect(
  133. screen.queryByRole('button', {
  134. name: /resend confirmation email/i,
  135. })
  136. ).to.be.null
  137. await waitForElementToBeRemoved(() => screen.getByText(/sending/i))
  138. expect(
  139. screen.queryByText(/an error has occurred while performing your request/i)
  140. ).to.be.null
  141. await screen.findByRole('button', {
  142. name: /resend confirmation email/i,
  143. })
  144. })
  145. it('shows error when resending email fails', async function () {
  146. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  147. render(<EmailsSection />)
  148. await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
  149. fetchMock.post('/user/emails/resend_confirmation', 503)
  150. const button = screen.getByRole('button', {
  151. name: /resend confirmation email/i,
  152. })
  153. fireEvent.click(button)
  154. expect(
  155. screen.queryByRole('button', {
  156. name: /resend confirmation email/i,
  157. })
  158. ).to.be.null
  159. await waitForElementToBeRemoved(() => screen.getByText(/sending/i))
  160. screen.getByText(/an error has occurred while performing your request/i)
  161. screen.getByRole('button', { name: /resend confirmation email/i })
  162. })
  163. })