emails-section.test.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 {
  13. confirmedUserData,
  14. fakeUsersData,
  15. professionalUserData,
  16. unconfirmedUserData,
  17. } from '../../fixtures/test-user-email-data'
  18. import getMeta from '@/utils/meta'
  19. describe('<EmailsSection />', function () {
  20. beforeEach(function () {
  21. Object.assign(getMeta('ol-ExposedSettings'), {
  22. hasAffiliationsFeature: true,
  23. })
  24. fetchMock.removeRoutes().clearHistory()
  25. })
  26. afterEach(function () {
  27. fetchMock.removeRoutes().clearHistory()
  28. })
  29. it('renders translated heading', function () {
  30. render(<EmailsSection />)
  31. screen.getByRole('heading', { name: /emails and affiliations/i })
  32. })
  33. it('renders translated description', function () {
  34. render(<EmailsSection />)
  35. screen.getByText(/add additional email addresses/i)
  36. screen.getByText(/to change your primary email/i)
  37. screen.getByRole('link', {
  38. name: /learn more about managing your Overleaf emails/i,
  39. })
  40. })
  41. it('renders a loading message when loading', async function () {
  42. render(<EmailsSection />)
  43. await screen.findByText(/loading/i)
  44. })
  45. it('renders an error message and hides loading message on error', async function () {
  46. fetchMock.get('/user/emails?ensureAffiliation=true', 500)
  47. render(<EmailsSection />)
  48. await screen.findByText(
  49. /an error has occurred while performing your request/i
  50. )
  51. expect(screen.queryByText(/loading/i)).to.be.null
  52. })
  53. it('renders user emails', async function () {
  54. fetchMock.get('/user/emails?ensureAffiliation=true', fakeUsersData)
  55. render(<EmailsSection />)
  56. await waitFor(() => {
  57. fakeUsersData.forEach(userData => {
  58. screen.getByText(new RegExp(userData.email, 'i'))
  59. })
  60. })
  61. })
  62. it('renders primary status', async function () {
  63. fetchMock.get('/user/emails?ensureAffiliation=true', [professionalUserData])
  64. render(<EmailsSection />)
  65. await screen.findByText(`${professionalUserData.email}`)
  66. screen.getByText('Primary')
  67. })
  68. it('shows confirmation status for unconfirmed users', async function () {
  69. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  70. render(<EmailsSection />)
  71. await screen.findByText(/unconfirmed/i)
  72. })
  73. it('hides confirmation status for confirmed users', async function () {
  74. fetchMock.get('/user/emails?ensureAffiliation=true', [confirmedUserData])
  75. render(<EmailsSection />)
  76. await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
  77. expect(screen.queryByText(/please check your inbox/i)).to.be.null
  78. })
  79. it('renders resend link', async function () {
  80. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  81. render(<EmailsSection />)
  82. await screen.findByRole('button', { name: 'Send confirmation code' })
  83. })
  84. it('renders professional label', async function () {
  85. fetchMock.get('/user/emails?ensureAffiliation=true', [professionalUserData])
  86. render(<EmailsSection />)
  87. const node = await screen.findByText(professionalUserData.email, {
  88. exact: false,
  89. })
  90. expect(within(node).getByText(/professional/i)).to.exist
  91. })
  92. it('shows loader when resending email', async function () {
  93. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  94. render(<EmailsSection />)
  95. await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
  96. fetchMock.post('/user/emails/send-confirmation-code', 200)
  97. const button = screen.getByRole('button', {
  98. name: 'Send confirmation code',
  99. })
  100. fireEvent.click(button)
  101. expect(
  102. screen.queryByRole('button', {
  103. name: 'Send confirmation code',
  104. })
  105. ).to.be.null
  106. await screen.findByRole('dialog')
  107. expect(
  108. screen.queryByText(/an error has occurred while performing your request/i)
  109. ).to.be.null
  110. await screen.findAllByRole('button', {
  111. name: 'Resend confirmation code',
  112. })
  113. })
  114. it('shows error when resending email fails', async function () {
  115. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  116. render(<EmailsSection />)
  117. await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
  118. fetchMock.post('/user/emails/send-confirmation-code', 503)
  119. const button = screen.getByRole('button', {
  120. name: 'Send confirmation code',
  121. })
  122. fireEvent.click(button)
  123. expect(screen.queryByRole('button', { name: 'Send confirmation code' })).to
  124. .be.null
  125. await screen.findByRole('dialog')
  126. await screen.findByText(/sorry, something went wrong/i)
  127. screen.getByRole('button', { name: 'Resend confirmation code' })
  128. })
  129. it('sorts emails with primary first, then confirmed, then unconfirmed', async function () {
  130. const unconfirmedEmail = { ...unconfirmedUserData, email: 'b@example.com' }
  131. const unconfirmedEmailTwo = {
  132. ...unconfirmedUserData,
  133. email: 'd@example.com',
  134. }
  135. const confirmedEmail = {
  136. ...confirmedUserData,
  137. email: 'a@example.com',
  138. confirmedAt: new Date().toISOString(),
  139. }
  140. const confirmedEmailTwo = {
  141. ...confirmedUserData,
  142. email: 'e@example.com',
  143. confirmedAt: new Date().toISOString(),
  144. }
  145. const primaryEmail = {
  146. ...professionalUserData,
  147. email: 'c@example.com',
  148. default: true,
  149. }
  150. const emails = [
  151. confirmedEmailTwo,
  152. unconfirmedEmailTwo,
  153. unconfirmedEmail,
  154. confirmedEmail,
  155. primaryEmail,
  156. ]
  157. fetchMock.get('/user/emails?ensureAffiliation=true', emails)
  158. render(<EmailsSection />)
  159. await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
  160. const emailElements = screen.getAllByTestId(/email-row/i)
  161. // Primary should be first regardless of alphabetical order
  162. expect(within(emailElements[0]).getByText('c@example.com')).to.exist
  163. expect(within(emailElements[0]).getByText('Primary')).to.exist
  164. // Confirmed should be second in alphabetical order
  165. expect(within(emailElements[1]).getByText('a@example.com')).to.exist
  166. expect(within(emailElements[2]).getByText('e@example.com')).to.exist
  167. // Unconfirmed should be last in alphabetical order
  168. expect(within(emailElements[3]).getByText('b@example.com')).to.exist
  169. expect(within(emailElements[3]).getByText(/unconfirmed/i)).to.exist
  170. expect(within(emailElements[4]).getByText('d@example.com')).to.exist
  171. expect(within(emailElements[4]).getByText(/unconfirmed/i)).to.exist
  172. })
  173. })