emails-section.test.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. describe('<EmailsSection />', function () {
  19. beforeEach(function () {
  20. window.metaAttributesCache.set('ol-ExposedSettings', {
  21. hasAffiliationsFeature: true,
  22. })
  23. fetchMock.reset()
  24. })
  25. afterEach(function () {
  26. window.metaAttributesCache = new Map()
  27. fetchMock.reset()
  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. })
  38. it('renders a loading message when loading', async function () {
  39. render(<EmailsSection />)
  40. await screen.findByText(/loading/i)
  41. })
  42. it('renders an error message and hides loading message on error', async function () {
  43. fetchMock.get('/user/emails?ensureAffiliation=true', 500)
  44. render(<EmailsSection />)
  45. await screen.findByText(
  46. /an error has occurred while performing your request/i
  47. )
  48. expect(screen.queryByText(/loading/i)).to.be.null
  49. })
  50. it('renders user emails', async function () {
  51. fetchMock.get('/user/emails?ensureAffiliation=true', fakeUsersData)
  52. render(<EmailsSection />)
  53. await waitFor(() => {
  54. fakeUsersData.forEach(userData => {
  55. screen.getByText(new RegExp(userData.email, 'i'))
  56. })
  57. })
  58. })
  59. it('renders primary status', async function () {
  60. fetchMock.get('/user/emails?ensureAffiliation=true', [professionalUserData])
  61. render(<EmailsSection />)
  62. await screen.findByText(`${professionalUserData.email} (primary)`)
  63. })
  64. it('shows confirmation status for unconfirmed users', async function () {
  65. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  66. render(<EmailsSection />)
  67. await screen.findByText(/please check your inbox/i)
  68. })
  69. it('hides confirmation status for confirmed users', async function () {
  70. fetchMock.get('/user/emails?ensureAffiliation=true', [confirmedUserData])
  71. render(<EmailsSection />)
  72. await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
  73. expect(screen.queryByText(/please check your inbox/i)).to.be.null
  74. })
  75. it('renders resend link', async function () {
  76. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  77. render(<EmailsSection />)
  78. await screen.findByRole('button', { name: /resend confirmation email/i })
  79. })
  80. it('renders professional label', async function () {
  81. fetchMock.get('/user/emails?ensureAffiliation=true', [professionalUserData])
  82. render(<EmailsSection />)
  83. const node = await screen.findByText(professionalUserData.email, {
  84. exact: false,
  85. })
  86. expect(within(node).getByText(/professional/i)).to.exist
  87. })
  88. it('shows loader when resending email', async function () {
  89. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  90. render(<EmailsSection />)
  91. await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
  92. fetchMock.post('/user/emails/resend_confirmation', 200)
  93. const button = screen.getByRole('button', {
  94. name: /resend confirmation email/i,
  95. })
  96. fireEvent.click(button)
  97. expect(
  98. screen.queryByRole('button', {
  99. name: /resend confirmation email/i,
  100. })
  101. ).to.be.null
  102. await waitForElementToBeRemoved(() => screen.getByText(/sending/i))
  103. expect(
  104. screen.queryByText(/an error has occurred while performing your request/i)
  105. ).to.be.null
  106. await screen.findByRole('button', {
  107. name: /resend confirmation email/i,
  108. })
  109. })
  110. it('shows error when resending email fails', async function () {
  111. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  112. render(<EmailsSection />)
  113. await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
  114. fetchMock.post('/user/emails/resend_confirmation', 503)
  115. const button = screen.getByRole('button', {
  116. name: /resend confirmation email/i,
  117. })
  118. fireEvent.click(button)
  119. expect(
  120. screen.queryByRole('button', {
  121. name: /resend confirmation email/i,
  122. })
  123. ).to.be.null
  124. await waitForElementToBeRemoved(() => screen.getByText(/sending/i))
  125. screen.getByText(/sorry, something went wrong/i)
  126. screen.getByRole('button', { name: /resend confirmation email/i })
  127. })
  128. })