Files
overleaf-cep/services/web/test/frontend/features/settings/context/sso-context.test.tsx
T
Antoine Clausse b901bb6c75 [web] Update fetch-mock to version 12 (#24837)
* Update fetch-mock to version 12

* Replace `fetchMock.done` by `fetchMock.callHistory.done`

* Replace `…Mock.called` by `…Mock.callHistory.called`

* Replace `fetchMock.reset` by `fetchMock.hardReset`

* Replace `fetchMock.restore` by `fetchMock.hardReset`

* Replace `fetchMock.resetHistory` by `fetchMock.clearHistory`

* Replace `fetchMock.calls` by `fetchMock.callHistory.calls`

* Replace `fetchMock.flush` by `fetchMock.callHistory.flush`

* Update tests for fetch-mock version 12

See https://www.wheresrhys.co.uk/fetch-mock/docs/Usage/upgrade-guide

* Update stories for fetch-mock version 12

* Remove `overwriteRoutes` option

* Add `fetchMock.spyGlobal()` to storybook

* Remove deprecated `sendAsJson` param

* Replace `fetchMock.hardReset()` by `fetchMock.removeRoutes().clearHistory()`

* Fixup fetch-mock in storybook:

Call `mockGlobal` inside the hook, call `removeRoutes` and `unmockGlobal` on cleanup

Behaviour can be tested by navigating between

https://storybook.dev-overleaf.com/main/?path=/story/editor-ai-error-assistant-compile-log-entries--first-log-entry
https://storybook.dev-overleaf.com/main/?path=/story/editor-ai-error-assistant-compile-log-entries--rate-limited

https://storybook.dev-overleaf.com/main/?path=/story/project-list-notifications--project-invite
https://storybook.dev-overleaf.com/main/?path=/story/project-list-notifications--project-invite-network-error

And clicking the buttons

GitOrigin-RevId: 35611b4430259e4c21c3d819ad18b2e6dab66242
2025-04-17 08:06:24 +00:00

83 lines
2.3 KiB
TypeScript

import { expect } from 'chai'
import { renderHook } from '@testing-library/react-hooks'
import {
SSOProvider,
useSSOContext,
} from '../../../../../frontend/js/features/settings/context/sso-context'
import fetchMock from 'fetch-mock'
const mockOauthProviders = {
google: {
descriptionKey: 'login_with_service',
descriptionOptions: { service: 'Google' },
name: 'Google',
linkPath: '/auth/google',
},
orcid: {
descriptionKey: 'oauth_orcid_description',
descriptionOptions: {
link: '/blog/434',
appName: 'Overleaf',
},
name: 'ORCID',
linkPath: '/auth/orcid',
},
}
describe('SSOContext', function () {
const renderSSOContext = () =>
renderHook(() => useSSOContext(), {
wrapper: ({ children }) => <SSOProvider>{children}</SSOProvider>,
})
beforeEach(function () {
window.metaAttributesCache.set('ol-thirdPartyIds', {
google: 'google-id',
})
window.metaAttributesCache.set('ol-oauthProviders', mockOauthProviders)
fetchMock.removeRoutes().clearHistory()
})
it('should initialise subscriptions with their linked status', function () {
const { result } = renderSSOContext()
expect(result.current.subscriptions).to.deep.equal({
google: {
providerId: 'google',
provider: mockOauthProviders.google,
linked: true,
},
orcid: {
providerId: 'orcid',
provider: mockOauthProviders.orcid,
linked: false,
},
})
})
describe('unlink', function () {
beforeEach(function () {
fetchMock.post('express:/user/oauth-unlink', 200)
})
it('should unlink an existing subscription', async function () {
const { result, waitForNextUpdate } = renderSSOContext()
result.current.unlink('google')
await waitForNextUpdate()
expect(result.current.subscriptions.google.linked).to.be.false
})
it('when the provider is not linked, should do nothing', function () {
const { result } = renderSSOContext()
result.current.unlink('orcid')
expect(fetchMock.callHistory.called()).to.be.false
})
it('supports unmounting the component while the request is inflight', async function () {
const { result, unmount } = renderSSOContext()
result.current.unlink('google')
expect(fetchMock.callHistory.called()).to.be.true
unmount()
})
})
})