Files
overleaf-cep/services/web/test/frontend/features/settings/components/linking-section.test.tsx
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

98 lines
2.9 KiB
TypeScript

import { expect } from 'chai'
import { screen, render } from '@testing-library/react'
import fetchMock from 'fetch-mock'
import LinkingSection from '@/features/settings/components/linking-section'
import { UserProvider } from '@/shared/context/user-context'
import { SSOProvider } from '@/features/settings/context/sso-context'
import { SplitTestProvider } from '@/shared/context/split-test-context'
function renderSectionWithProviders() {
render(<LinkingSection />, {
wrapper: ({ children }) => (
<SplitTestProvider>
<UserProvider>
<SSOProvider>{children}</SSOProvider>
</UserProvider>
</SplitTestProvider>
),
})
}
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('<LinkingSection />', function () {
beforeEach(function () {
window.metaAttributesCache.set('ol-user', {})
// suppress integrations and references widgets as they cannot be tested in
// all environments
window.metaAttributesCache.set('ol-hideLinkingWidgets', true)
window.metaAttributesCache.set('ol-thirdPartyIds', {
google: 'google-id',
})
window.metaAttributesCache.set('ol-oauthProviders', mockOauthProviders)
})
afterEach(function () {
fetchMock.removeRoutes().clearHistory()
})
it('shows header', async function () {
renderSectionWithProviders()
screen.getByText('Integrations')
screen.getByText(
'You can link your Overleaf account with other services to enable the features described below.'
)
})
it('lists SSO providers', async function () {
renderSectionWithProviders()
screen.getByText('linked accounts')
screen.getByText('Google')
screen.getByText('Log in with Google.')
screen.getByRole('button', { name: 'Unlink' })
screen.getByText('ORCID')
screen.getByText(
/Securely establish your identity by linking your ORCID iD/
)
const helpLink = screen.getByRole('link', { name: 'Learn more' })
expect(helpLink.getAttribute('href')).to.equal('/blog/434')
const linkButton = screen.getByRole('button', { name: 'Link' })
expect(linkButton.getAttribute('href')).to.equal('/auth/orcid?intent=link')
})
it('shows SSO error message', async function () {
window.metaAttributesCache.set('ol-ssoErrorMessage', 'You no SSO')
renderSectionWithProviders()
screen.getByText('Error linking account: You no SSO')
})
it('does not show providers section when empty', async function () {
window.metaAttributesCache.delete('ol-oauthProviders')
renderSectionWithProviders()
expect(screen.queryByText('linked accounts')).to.not.exist
})
})