overleaf-cep/services/web/test/frontend/features/subscription/components/dashboard/managed-publishers.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

76 lines
2.4 KiB
TypeScript

import { expect } from 'chai'
import { render, screen } from '@testing-library/react'
import { SubscriptionDashboardProvider } from '../../../../../../frontend/js/features/subscription/context/subscription-dashboard-context'
import fetchMock from 'fetch-mock'
import ManagedPublishers from '../../../../../../frontend/js/features/subscription/components/dashboard/managed-publishers'
import { SplitTestProvider } from '@/shared/context/split-test-context'
import { Publisher } from '../../../../../../types/subscription/dashboard/publisher'
const userId = 'fff999fff999'
const publisher1 = {
slug: 'pub-1',
managerIds: [],
name: 'Pub 1',
partner: 'p1',
}
const publisher2 = {
slug: 'pub-2',
managerIds: [],
name: 'Pub 2',
partner: 'p2',
}
const managedPublishers: Publisher[] = [publisher1, publisher2]
describe('<ManagedPublishers />', function () {
beforeEach(function () {
window.metaAttributesCache.set('ol-managedPublishers', managedPublishers)
window.metaAttributesCache.set('ol-user_id', userId)
})
afterEach(function () {
fetchMock.removeRoutes().clearHistory()
})
it('renders all managed publishers', function () {
render(
<SplitTestProvider>
<SubscriptionDashboardProvider>
<ManagedPublishers />
</SubscriptionDashboardProvider>
</SplitTestProvider>
)
const elements = screen.getAllByText('You are a', {
exact: false,
})
expect(elements.length).to.equal(2)
expect(elements[0].textContent).to.equal('You are a manager of Pub 1')
expect(elements[1].textContent).to.equal('You are a manager of Pub 2')
const links = screen.getAllByRole('link')
expect(links[0].getAttribute('href')).to.equal('/publishers/pub-1/hub')
expect(links[1].getAttribute('href')).to.equal(
'/manage/publishers/pub-1/managers'
)
expect(links[2].getAttribute('href')).to.equal('/publishers/pub-2/hub')
expect(links[3].getAttribute('href')).to.equal(
'/manage/publishers/pub-2/managers'
)
})
it('renders nothing when there are no publishers', function () {
window.metaAttributesCache.set('ol-managedPublishers', undefined)
render(
<SplitTestProvider>
<SubscriptionDashboardProvider>
<ManagedPublishers />
</SubscriptionDashboardProvider>
</SplitTestProvider>
)
const elements = screen.queryAllByText('You are a', {
exact: false,
})
expect(elements.length).to.equal(0)
})
})