linking-section.test.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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('lists SSO providers', async function () {
  51. renderSectionWithProviders()
  52. screen.getByText('Linked accounts')
  53. screen.getByText('Google')
  54. screen.getByText('Log in with Google.')
  55. screen.getByRole('button', { name: /unlink/i })
  56. screen.getByText('ORCID')
  57. screen.getByText(
  58. /Securely establish your identity by linking your ORCID iD/
  59. )
  60. const helpLink = screen.getByRole('link', {
  61. name: /learn more about orcid/i,
  62. })
  63. expect(helpLink.getAttribute('href')).to.equal('/blog/434')
  64. const linkButton = screen.getByRole('link', { name: /link orcid/i })
  65. expect(linkButton.getAttribute('href')).to.equal('/auth/orcid?intent=link')
  66. })
  67. it('shows SSO error message', async function () {
  68. window.metaAttributesCache.set('ol-ssoErrorMessage', 'You no SSO')
  69. renderSectionWithProviders()
  70. screen.getByText('Error linking account: You no SSO')
  71. })
  72. it('does not show providers section when empty', async function () {
  73. window.metaAttributesCache.delete('ol-oauthProviders')
  74. renderSectionWithProviders()
  75. expect(screen.queryByText('Linked accounts')).to.not.exist
  76. })
  77. })