linking-section.test.tsx 3.1 KB

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