linking-section.test.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { expect } from 'chai'
  2. import { screen, render } from '@testing-library/react'
  3. import fetchMock from 'fetch-mock'
  4. import LinkingSection from '../../../../../frontend/js/features/settings/components/linking-section'
  5. import { UserProvider } from '../../../../../frontend/js/shared/context/user-context'
  6. import { SSOProvider } from '../../../../../frontend/js/features/settings/context/sso-context'
  7. function renderSectionWithProviders() {
  8. render(<LinkingSection />, {
  9. wrapper: ({ children }) => (
  10. <UserProvider>
  11. <SSOProvider>{children}</SSOProvider>
  12. </UserProvider>
  13. ),
  14. })
  15. }
  16. const mockOauthProviders = {
  17. google: {
  18. descriptionKey: 'login_with_service',
  19. descriptionOptions: { service: 'Google' },
  20. name: 'Google',
  21. linkPath: '/auth/google',
  22. },
  23. orcid: {
  24. descriptionKey: 'oauth_orcid_description',
  25. descriptionOptions: {
  26. link: '/blog/434',
  27. appName: 'Overleaf',
  28. },
  29. name: 'ORCID',
  30. linkPath: '/auth/orcid',
  31. },
  32. twitter: {
  33. hideWhenNotLinked: true,
  34. name: 'Twitter',
  35. linkPath: '/auth/twitter',
  36. },
  37. }
  38. describe('<LinkingSection />', function () {
  39. beforeEach(function () {
  40. window.metaAttributesCache = window.metaAttributesCache || new Map()
  41. window.metaAttributesCache.set('ol-user', {})
  42. // suppress integrations and references widgets as they cannot be tested in
  43. // all environments
  44. window.metaAttributesCache.set('integrationLinkingWidgets', [])
  45. window.metaAttributesCache.set('referenceLinkingWidgets', [])
  46. window.metaAttributesCache.set('ol-thirdPartyIds', {
  47. google: 'google-id',
  48. })
  49. window.metaAttributesCache.set('ol-oauthProviders', mockOauthProviders)
  50. })
  51. afterEach(function () {
  52. window.metaAttributesCache = new Map()
  53. fetchMock.reset()
  54. })
  55. it('shows header', async function () {
  56. renderSectionWithProviders()
  57. screen.getByText('Integrations')
  58. screen.getByText(
  59. 'You can link your Overleaf account with other services to enable the features described below.'
  60. )
  61. })
  62. it('lists SSO providers', async function () {
  63. renderSectionWithProviders()
  64. screen.getByText('linked accounts')
  65. screen.getByText('Google')
  66. screen.getByText('Log in with Google.')
  67. screen.getByRole('button', { name: 'Unlink' })
  68. screen.getByText('ORCID')
  69. screen.getByText(
  70. /Securely establish your identity by linking your ORCID iD/
  71. )
  72. const helpLink = screen.getByRole('link', { name: 'Learn more' })
  73. expect(helpLink.getAttribute('href')).to.equal('/blog/434')
  74. const linkButton = screen.getByRole('link', { name: 'Link' })
  75. expect(linkButton.getAttribute('href')).to.equal('/auth/orcid?intent=link')
  76. expect(screen.queryByText('Twitter')).to.not.exist
  77. })
  78. it('shows SSO error message', async function () {
  79. window.metaAttributesCache.set('ol-ssoErrorMessage', 'You no SSO')
  80. renderSectionWithProviders()
  81. screen.getByText('Error linking account: You no SSO')
  82. })
  83. it('does not show providers section when empty', async function () {
  84. window.metaAttributesCache.delete('ol-oauthProviders')
  85. renderSectionWithProviders()
  86. expect(screen.queryByText('linked accounts')).to.not.exist
  87. })
  88. })