sso-context.test.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { expect } from 'chai'
  2. import { renderHook, waitFor } from '@testing-library/react'
  3. import {
  4. SSOProvider,
  5. useSSOContext,
  6. } from '../../../../../frontend/js/features/settings/context/sso-context'
  7. import fetchMock from 'fetch-mock'
  8. const mockOauthProviders = {
  9. google: {
  10. descriptionKey: 'login_with_service',
  11. descriptionOptions: { service: 'Google' },
  12. name: 'Google',
  13. linkPath: '/auth/google',
  14. },
  15. orcid: {
  16. descriptionKey: 'oauth_orcid_description',
  17. descriptionOptions: {
  18. link: '/blog/434',
  19. appName: 'Overleaf',
  20. },
  21. name: 'ORCID',
  22. linkPath: '/auth/orcid',
  23. },
  24. }
  25. describe('SSOContext', function () {
  26. const renderSSOContext = () =>
  27. renderHook(() => useSSOContext(), {
  28. wrapper: ({ children }) => <SSOProvider>{children}</SSOProvider>,
  29. })
  30. beforeEach(function () {
  31. window.metaAttributesCache.set('ol-thirdPartyIds', {
  32. google: 'google-id',
  33. })
  34. window.metaAttributesCache.set('ol-oauthProviders', mockOauthProviders)
  35. fetchMock.removeRoutes().clearHistory()
  36. })
  37. it('should initialise subscriptions with their linked status', function () {
  38. const { result } = renderSSOContext()
  39. expect(result.current.subscriptions).to.deep.equal({
  40. google: {
  41. providerId: 'google',
  42. provider: mockOauthProviders.google,
  43. linked: true,
  44. },
  45. orcid: {
  46. providerId: 'orcid',
  47. provider: mockOauthProviders.orcid,
  48. linked: false,
  49. },
  50. })
  51. })
  52. describe('unlink', function () {
  53. beforeEach(function () {
  54. fetchMock.post('express:/user/oauth-unlink', 200)
  55. })
  56. it('should unlink an existing subscription', async function () {
  57. const { result } = renderSSOContext()
  58. result.current.unlink('google')
  59. await waitFor(
  60. () => expect(result.current.subscriptions.google.linked).to.be.false
  61. )
  62. })
  63. it('when the provider is not linked, should do nothing', function () {
  64. const { result } = renderSSOContext()
  65. result.current.unlink('orcid')
  66. expect(fetchMock.callHistory.called()).to.be.false
  67. })
  68. it('supports unmounting the component while the request is inflight', async function () {
  69. const { result, unmount } = renderSSOContext()
  70. result.current.unlink('google')
  71. expect(fetchMock.callHistory.called()).to.be.true
  72. unmount()
  73. })
  74. })
  75. })