sso-alert.test.tsx 3.3 KB

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