sso-alert.test.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { render, screen, fireEvent } from '@testing-library/react'
  2. import { expect } from 'chai'
  3. import { SSOAlert } from '../../../../../../frontend/js/features/settings/components/emails/sso-alert'
  4. describe('<SSOAlert/>', function () {
  5. describe('when there is no institutional linking information', function () {
  6. it('should be empty', function () {
  7. render(<SSOAlert />)
  8. expect(screen.queryByRole('alert')).to.be.null
  9. })
  10. })
  11. describe('when there is institutional linking information', function () {
  12. beforeEach(function () {
  13. window.metaAttributesCache.set('ol-institutionLinked', {
  14. universityName: 'Overleaf University',
  15. })
  16. })
  17. it('should render an information alert with the university name', function () {
  18. render(<SSOAlert />)
  19. screen.getByRole('alert')
  20. screen.getByText('account was successfully linked', { exact: false })
  21. screen.getByText('Overleaf University', { exact: false })
  22. })
  23. it('when entitled, it should render access granted to "professional" features', function () {
  24. window.metaAttributesCache.get('ol-institutionLinked').hasEntitlement =
  25. true
  26. render(<SSOAlert />)
  27. screen.getByText('this grants you access', { exact: false })
  28. screen.getByText('Professional')
  29. })
  30. it('when the email is not canonical it should also render a warning alert', function () {
  31. window.metaAttributesCache.set(
  32. 'ol-institutionEmailNonCanonical',
  33. 'user@example.com'
  34. )
  35. render(<SSOAlert />)
  36. const alerts = screen.getAllByRole('alert')
  37. expect(alerts.length).to.equal(2)
  38. })
  39. it('the alerts should be closeable', function () {
  40. window.metaAttributesCache.set(
  41. 'ol-institutionEmailNonCanonical',
  42. 'user@example.com'
  43. )
  44. render(<SSOAlert />)
  45. const closeButtons = screen.getAllByRole('button', {
  46. name: 'Close',
  47. })
  48. fireEvent.click(closeButtons[0])
  49. fireEvent.click(closeButtons[1])
  50. expect(screen.queryByRole('button', { name: 'Close' })).to.be.null
  51. })
  52. })
  53. describe('when there is a SAML Error', function () {
  54. beforeEach(function () {
  55. window.metaAttributesCache.set('ol-samlError', {
  56. message: 'there was an error',
  57. })
  58. })
  59. it('should render an error alert', function () {
  60. render(<SSOAlert />)
  61. screen.getByRole('alert')
  62. screen.getByText('there was an error')
  63. })
  64. it('should render translated error if available', function () {
  65. window.metaAttributesCache.get('ol-samlError').translatedMessage =
  66. 'translated error'
  67. render(<SSOAlert />)
  68. screen.getByText('translated error')
  69. expect(screen.queryByText('there was an error')).to.be.null
  70. })
  71. it('should render a "try again" label when requested by the error payload', function () {
  72. window.metaAttributesCache.get('ol-samlError').tryAgain = true
  73. render(<SSOAlert />)
  74. screen.getByText('Please try again')
  75. })
  76. it('should render reconfirmation unable-to-find-user message for matching SAML error name', function () {
  77. window.metaAttributesCache.set('ol-samlError', {
  78. name: 'SAMLCommonsReconfirmationUnableToFindUserError',
  79. })
  80. render(<SSOAlert />)
  81. screen.getByText(/unable to confirm your affiliation/i)
  82. const contactLink = screen.getByRole('link', { name: /contact us/i })
  83. expect(contactLink.getAttribute('href')).to.equal('/contact')
  84. expect(contactLink.getAttribute('target')).to.equal('_blank')
  85. })
  86. it('the alert should be closeable', function () {
  87. render(<SSOAlert />)
  88. const closeButton = screen.getByRole('button', {
  89. name: 'Close',
  90. })
  91. fireEvent.click(closeButton)
  92. expect(screen.queryByRole('button', { name: 'Close' })).to.be.null
  93. })
  94. })
  95. })