linking-section.test.tsx 3.0 KB

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