emails-section.test.tsx 6.7 KB

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