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

164 lines
4.8 KiB
TypeScript

import {
render,
screen,
within,
fireEvent,
waitFor,
waitForElementToBeRemoved,
} from '@testing-library/react'
import EmailsSection from '../../../../../../frontend/js/features/settings/components/emails-section'
import { expect } from 'chai'
import fetchMock from 'fetch-mock'
import {
confirmedUserData,
fakeUsersData,
professionalUserData,
unconfirmedUserData,
} from '../../fixtures/test-user-email-data'
import getMeta from '@/utils/meta'
describe('<EmailsSection />', function () {
beforeEach(function () {
Object.assign(getMeta('ol-ExposedSettings'), {
hasAffiliationsFeature: true,
})
fetchMock.removeRoutes().clearHistory()
})
afterEach(function () {
fetchMock.removeRoutes().clearHistory()
})
it('renders translated heading', function () {
render(<EmailsSection />)
screen.getByRole('heading', { name: /emails and affiliations/i })
})
it('renders translated description', function () {
render(<EmailsSection />)
screen.getByText(/add additional email addresses/i)
screen.getByText(/to change your primary email/i)
})
it('renders a loading message when loading', async function () {
render(<EmailsSection />)
await screen.findByText(/loading/i)
})
it('renders an error message and hides loading message on error', async function () {
fetchMock.get('/user/emails?ensureAffiliation=true', 500)
render(<EmailsSection />)
await screen.findByText(
/an error has occurred while performing your request/i
)
expect(screen.queryByText(/loading/i)).to.be.null
})
it('renders user emails', async function () {
fetchMock.get('/user/emails?ensureAffiliation=true', fakeUsersData)
render(<EmailsSection />)
await waitFor(() => {
fakeUsersData.forEach(userData => {
screen.getByText(new RegExp(userData.email, 'i'))
})
})
})
it('renders primary status', async function () {
fetchMock.get('/user/emails?ensureAffiliation=true', [professionalUserData])
render(<EmailsSection />)
await screen.findByText(`${professionalUserData.email}`)
screen.getByText('Primary')
})
it('shows confirmation status for unconfirmed users', async function () {
fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
render(<EmailsSection />)
await screen.findByText(/unconfirmed/i)
})
it('hides confirmation status for confirmed users', async function () {
fetchMock.get('/user/emails?ensureAffiliation=true', [confirmedUserData])
render(<EmailsSection />)
await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
expect(screen.queryByText(/please check your inbox/i)).to.be.null
})
it('renders resend link', async function () {
fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
render(<EmailsSection />)
await screen.findByRole('button', { name: /resend confirmation code/i })
})
it('renders professional label', async function () {
fetchMock.get('/user/emails?ensureAffiliation=true', [professionalUserData])
render(<EmailsSection />)
const node = await screen.findByText(professionalUserData.email, {
exact: false,
})
expect(within(node).getByText(/professional/i)).to.exist
})
it('shows loader when resending email', async function () {
fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
render(<EmailsSection />)
await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
fetchMock.post('/user/emails/send-confirmation-code', 200)
const button = screen.getByRole('button', {
name: /resend confirmation code/i,
})
fireEvent.click(button)
expect(
screen.queryByRole('button', {
name: /resend confirmation code/i,
})
).to.be.null
await waitForElementToBeRemoved(() => screen.getByText(/sending/i))
expect(
screen.queryByText(/an error has occurred while performing your request/i)
).to.be.null
await screen.findAllByRole('button', {
name: /resend confirmation code/i,
})
})
it('shows error when resending email fails', async function () {
fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
render(<EmailsSection />)
await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
fetchMock.post('/user/emails/send-confirmation-code', 503)
const button = screen.getByRole('button', {
name: /resend confirmation code/i,
})
fireEvent.click(button)
expect(screen.queryByRole('button', { name: /resend confirmation code/i }))
.to.be.null
await waitForElementToBeRemoved(() => screen.getByText(/sending/i))
screen.getByText(/sorry, something went wrong/i)
screen.getByRole('button', { name: /resend confirmation code/i })
})
})