emails-section.test.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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}`)
  63. screen.getByText('Primary')
  64. })
  65. it('shows confirmation status for unconfirmed users', async function () {
  66. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  67. render(<EmailsSection />)
  68. await screen.findByText(/please check your inbox/i)
  69. })
  70. it('hides confirmation status for confirmed users', async function () {
  71. fetchMock.get('/user/emails?ensureAffiliation=true', [confirmedUserData])
  72. render(<EmailsSection />)
  73. await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
  74. expect(screen.queryByText(/please check your inbox/i)).to.be.null
  75. })
  76. it('renders resend link', async function () {
  77. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  78. render(<EmailsSection />)
  79. await screen.findByRole('button', { name: /resend confirmation email/i })
  80. })
  81. it('renders professional label', async function () {
  82. fetchMock.get('/user/emails?ensureAffiliation=true', [professionalUserData])
  83. render(<EmailsSection />)
  84. const node = await screen.findByText(professionalUserData.email, {
  85. exact: false,
  86. })
  87. expect(within(node).getByText(/professional/i)).to.exist
  88. })
  89. it('shows loader when resending email', async function () {
  90. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  91. render(<EmailsSection />)
  92. await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
  93. fetchMock.post('/user/emails/resend_confirmation', 200)
  94. const button = screen.getByRole('button', {
  95. name: /resend confirmation email/i,
  96. })
  97. fireEvent.click(button)
  98. expect(
  99. screen.queryByRole('button', {
  100. name: /resend confirmation email/i,
  101. })
  102. ).to.be.null
  103. await waitForElementToBeRemoved(() => screen.getByText(/sending/i))
  104. expect(
  105. screen.queryByText(/an error has occurred while performing your request/i)
  106. ).to.be.null
  107. await screen.findByRole('button', {
  108. name: /resend confirmation email/i,
  109. })
  110. })
  111. it('shows error when resending email fails', async function () {
  112. fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
  113. render(<EmailsSection />)
  114. await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
  115. fetchMock.post('/user/emails/resend_confirmation', 503)
  116. const button = screen.getByRole('button', {
  117. name: /resend confirmation email/i,
  118. })
  119. fireEvent.click(button)
  120. expect(
  121. screen.queryByRole('button', {
  122. name: /resend confirmation email/i,
  123. })
  124. ).to.be.null
  125. await waitForElementToBeRemoved(() => screen.getByText(/sending/i))
  126. screen.getByText(/sorry, something went wrong/i)
  127. screen.getByRole('button', { name: /resend confirmation email/i })
  128. })
  129. })