Merge pull request #23940 from overleaf/td-react-18

Upgrade to React 18

GitOrigin-RevId: 9b81936e6eea2bccd97fe5c2c5841f0b946371b8
This commit is contained in:
Tim Down
2025-05-02 08:05:29 +00:00
committed by Copybot
parent 4464320757
commit 7abafb01ea
213 changed files with 2469 additions and 2105 deletions
@@ -1,5 +1,5 @@
import { expect } from 'chai'
import { renderHook } from '@testing-library/react-hooks'
import { renderHook, waitFor } from '@testing-library/react'
import {
SSOProvider,
useSSOContext,
@@ -60,10 +60,11 @@ describe('SSOContext', function () {
})
it('should unlink an existing subscription', async function () {
const { result, waitForNextUpdate } = renderSSOContext()
const { result } = renderSSOContext()
result.current.unlink('google')
await waitForNextUpdate()
expect(result.current.subscriptions.google.linked).to.be.false
await waitFor(
() => expect(result.current.subscriptions.google.linked).to.be.false
)
})
it('when the provider is not linked, should do nothing', function () {
@@ -1,8 +1,6 @@
import { expect } from 'chai'
import { cloneDeep } from 'lodash'
import { renderHook } from '@testing-library/react-hooks'
import { waitFor } from '@testing-library/react'
import { renderHook, waitFor } from '@testing-library/react'
import {
EmailContextType,
UserEmailsProvider,
@@ -53,12 +51,14 @@ describe('UserEmailContext', function () {
const { result } = renderUserEmailsContext()
await fetchMock.callHistory.flush(true)
expect(fetchMock.callHistory.calls()).to.have.lengthOf(1)
expect(result.current.state.data.byId).to.deep.equal({
'bar@overleaf.com': { ...untrustedUserData, ...confirmedUserData },
'baz@overleaf.com': unconfirmedUserData,
'foo@overleaf.com': professionalUserData,
'qux@overleaf.com': unconfirmedCommonsUserData,
})
await waitFor(() =>
expect(result.current.state.data.byId).to.deep.equal({
'bar@overleaf.com': { ...untrustedUserData, ...confirmedUserData },
'baz@overleaf.com': unconfirmedUserData,
'foo@overleaf.com': professionalUserData,
'qux@overleaf.com': unconfirmedCommonsUserData,
})
)
expect(result.current.state.data.linkedInstitutionIds).to.have.lengthOf(0)
expect(result.current.isInitializing).to.equal(false)
@@ -70,10 +70,8 @@ describe('UserEmailContext', function () {
const { result } = renderUserEmailsContext()
await fetchMock.callHistory.flush()
await waitFor(() => {
expect(result.current.isInitializing).to.equal(false)
expect(result.current.isInitializingError).to.equal(true)
})
await waitFor(() => expect(result.current.isInitializing).to.equal(false))
expect(result.current.isInitializingError).to.equal(true)
})
describe('state.isLoading', function () {
@@ -82,12 +80,16 @@ describe('UserEmailContext', function () {
expect(result.current.state.isLoading).to.equal(true)
})
it('should be updated with `setLoading`', function () {
it('should be updated with `setLoading`', async function () {
const { result } = renderUserEmailsContext()
result.current.setLoading(true)
expect(result.current.state.isLoading).to.equal(true)
await waitFor(() =>
expect(result.current.state.isLoading).to.equal(true)
)
result.current.setLoading(false)
expect(result.current.state.isLoading).to.equal(false)
await waitFor(() =>
expect(result.current.state.isLoading).to.equal(false)
)
})
})
})
@@ -106,15 +108,19 @@ describe('UserEmailContext', function () {
fetchMock.removeRoutes().clearHistory()
})
it('should set `isLoading === true`', function () {
fetchMock.get(/\/user\/emails/, [
{
email: 'new@email.com',
default: true,
},
])
it('should set `isLoading === true`', async function () {
fetchMock.get(
/\/user\/emails/,
[
{
email: 'new@email.com',
default: true,
},
],
{ delay: 100 }
)
result.current.getEmails()
expect(result.current.state.isLoading).to.be.true
await waitFor(() => expect(result.current.state.isLoading).to.be.true)
})
it('requests a new set of emails', async function () {
@@ -125,9 +131,11 @@ describe('UserEmailContext', function () {
fetchMock.get(/\/user\/emails/, [emailData])
result.current.getEmails()
await fetchMock.callHistory.flush(true)
expect(result.current.state.data.byId).to.deep.equal({
'new@email.com': emailData,
})
await waitFor(() =>
expect(result.current.state.data.byId).to.deep.equal({
'new@email.com': emailData,
})
)
})
it('should populate `linkedInstitutionIds`', async function () {
@@ -138,42 +146,55 @@ describe('UserEmailContext', function () {
])
const { result } = renderUserEmailsContext()
await fetchMock.callHistory.flush(true)
expect(result.current.state.data.linkedInstitutionIds).to.deep.equal([
'saml_provider_1',
'saml_provider_2',
])
await waitFor(() =>
expect(result.current.state.data.linkedInstitutionIds).to.deep.equal([
'saml_provider_1',
'saml_provider_2',
])
)
})
})
describe('makePrimary()', function () {
it('sets an email as `default`', function () {
it('sets an email as `default`', async function () {
expect(result.current.state.data.byId['bar@overleaf.com'].default).to.be
.false
result.current.makePrimary('bar@overleaf.com')
expect(result.current.state.data.byId['bar@overleaf.com'].default).to.be
.true
await waitFor(
() =>
expect(result.current.state.data.byId['bar@overleaf.com'].default)
.to.be.true
)
})
it('sets `default=false` for the current primary email ', function () {
it('sets `default=false` for the current primary email ', async function () {
expect(result.current.state.data.byId['foo@overleaf.com'].default).to.be
.true
result.current.makePrimary('bar@overleaf.com')
expect(result.current.state.data.byId['foo@overleaf.com'].default).to.be
.false
await waitFor(
() =>
expect(result.current.state.data.byId['foo@overleaf.com'].default)
.to.be.false
)
})
it('produces no effect when passing a non-existing email', function () {
it('produces no effect when passing a non-existing email', async function () {
const emails = cloneDeep(result.current.state.data.byId)
result.current.makePrimary('non-existing@email.com')
expect(result.current.state.data.byId).to.deep.equal(emails)
await waitFor(() =>
expect(result.current.state.data.byId).to.deep.equal(emails)
)
})
})
describe('deleteEmail()', function () {
it('removes data from the deleted email', function () {
it('removes data from the deleted email', async function () {
result.current.deleteEmail('bar@overleaf.com')
expect(result.current.state.data.byId['bar@overleaf.com']).to.be
.undefined
await waitFor(
() =>
expect(result.current.state.data.byId['bar@overleaf.com']).to.be
.undefined
)
})
it('produces no effect when passing a non-existing email', function () {
@@ -184,14 +205,20 @@ describe('UserEmailContext', function () {
})
describe('setEmailAffiliationBeingEdited()', function () {
it('sets an email as currently being edited', function () {
it('sets an email as currently being edited', async function () {
result.current.setEmailAffiliationBeingEdited('bar@overleaf.com')
expect(result.current.state.data.emailAffiliationBeingEdited).to.equal(
'bar@overleaf.com'
await waitFor(() =>
expect(
result.current.state.data.emailAffiliationBeingEdited
).to.equal('bar@overleaf.com')
)
result.current.setEmailAffiliationBeingEdited(null)
expect(result.current.state.data.emailAffiliationBeingEdited).to.be.null
await waitFor(
() =>
expect(result.current.state.data.emailAffiliationBeingEdited).to.be
.null
)
})
it('produces no effect when passing a non-existing email', function () {
@@ -202,15 +229,17 @@ describe('UserEmailContext', function () {
})
describe('updateAffiliation()', function () {
it('updates affiliation data for an email', function () {
it('updates affiliation data for an email', async function () {
result.current.updateAffiliation(
'foo@overleaf.com',
'new role',
'new department'
)
expect(
result.current.state.data.byId['foo@overleaf.com'].affiliation!.role
).to.equal('new role')
await waitFor(() =>
expect(
result.current.state.data.byId['foo@overleaf.com'].affiliation!.role
).to.equal('new role')
)
expect(
result.current.state.data.byId['foo@overleaf.com'].affiliation!
.department
@@ -317,9 +346,11 @@ describe('UserEmailContext', function () {
result.current.deleteEmail(affiliatedEmail1.email)
result.current.resetLeaversSurveyExpiration(affiliatedEmail1)
expect(
localStorage.getItem('showInstitutionalLeaversSurveyUntil')
).to.be.greaterThan(Date.now())
await waitFor(() =>
expect(
localStorage.getItem('showInstitutionalLeaversSurveyUntil')
).to.be.greaterThan(Date.now())
)
})
it("when the leaver has no institution license, it shouldn't reset the survey expiration date", async function () {