linking-section.test.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. twitter: {
  36. hideWhenNotLinked: true,
  37. name: 'Twitter',
  38. linkPath: '/auth/twitter',
  39. },
  40. }
  41. describe('<LinkingSection />', function () {
  42. beforeEach(function () {
  43. window.metaAttributesCache = window.metaAttributesCache || new Map()
  44. window.metaAttributesCache.set('ol-user', {})
  45. // suppress integrations and references widgets as they cannot be tested in
  46. // all environments
  47. window.metaAttributesCache.set('integrationLinkingWidgets', [])
  48. window.metaAttributesCache.set('referenceLinkingWidgets', [])
  49. window.metaAttributesCache.set('integrationLinkingWidgets', [])
  50. window.metaAttributesCache.set('ol-thirdPartyIds', {
  51. google: 'google-id',
  52. })
  53. window.metaAttributesCache.set('ol-oauthProviders', mockOauthProviders)
  54. })
  55. afterEach(function () {
  56. window.metaAttributesCache = new Map()
  57. fetchMock.reset()
  58. })
  59. it('shows header', async function () {
  60. renderSectionWithProviders()
  61. screen.getByText('Integrations')
  62. screen.getByText(
  63. 'You can link your Overleaf account with other services to enable the features described below.'
  64. )
  65. })
  66. it('lists SSO providers', async function () {
  67. renderSectionWithProviders()
  68. screen.getByText('linked accounts')
  69. screen.getByText('Google')
  70. screen.getByText('Log in with Google.')
  71. screen.getByRole('button', { name: 'Unlink' })
  72. screen.getByText('ORCID')
  73. screen.getByText(
  74. /Securely establish your identity by linking your ORCID iD/
  75. )
  76. const helpLink = screen.getByRole('link', { name: 'Learn more' })
  77. expect(helpLink.getAttribute('href')).to.equal('/blog/434')
  78. const linkButton = screen.getByRole('link', { name: 'Link' })
  79. expect(linkButton.getAttribute('href')).to.equal('/auth/orcid?intent=link')
  80. expect(screen.queryByText('Twitter')).to.not.exist
  81. })
  82. it('shows SSO error message', async function () {
  83. window.metaAttributesCache.set('ol-ssoErrorMessage', 'You no SSO')
  84. renderSectionWithProviders()
  85. screen.getByText('Error linking account: You no SSO')
  86. })
  87. it('does not show providers section when empty', async function () {
  88. window.metaAttributesCache.delete('ol-oauthProviders')
  89. renderSectionWithProviders()
  90. expect(screen.queryByText('linked accounts')).to.not.exist
  91. })
  92. })