sso-context.test.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { expect } from 'chai'
  2. import { renderHook } from '@testing-library/react-hooks'
  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 = new Map()
  32. window.metaAttributesCache.set('ol-thirdPartyIds', {
  33. google: 'google-id',
  34. })
  35. window.metaAttributesCache.set('ol-oauthProviders', mockOauthProviders)
  36. fetchMock.reset()
  37. })
  38. it('should initialise subscriptions with their linked status', function () {
  39. const { result } = renderSSOContext()
  40. expect(result.current.subscriptions).to.deep.equal({
  41. google: {
  42. providerId: 'google',
  43. provider: mockOauthProviders.google,
  44. linked: true,
  45. },
  46. orcid: {
  47. providerId: 'orcid',
  48. provider: mockOauthProviders.orcid,
  49. linked: false,
  50. },
  51. })
  52. })
  53. describe('unlink', function () {
  54. beforeEach(function () {
  55. fetchMock.post('express:/user/oauth-unlink', 200)
  56. })
  57. it('should unlink an existing subscription', async function () {
  58. const { result, waitForNextUpdate } = renderSSOContext()
  59. result.current.unlink('google')
  60. await waitForNextUpdate()
  61. expect(result.current.subscriptions.google.linked).to.be.false
  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.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.called()).to.be.true
  72. unmount()
  73. })
  74. })
  75. })