2022-04-22 09:49:26 -04:00
|
|
|
import {
|
|
|
|
render,
|
|
|
|
screen,
|
|
|
|
fireEvent,
|
|
|
|
waitForElementToBeRemoved,
|
|
|
|
} from '@testing-library/react'
|
2022-04-14 05:19:18 -04:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import { UserEmailData } from '../../../../../../types/user-email'
|
2022-04-22 09:49:26 -04:00
|
|
|
import fetchMock from 'fetch-mock'
|
2022-04-14 05:19:18 -04:00
|
|
|
import InstitutionAndRole from '../../../../../../frontend/js/features/settings/components/emails/institution-and-role'
|
2022-04-22 09:49:26 -04:00
|
|
|
import { UserEmailsProvider } from '../../../../../../frontend/js/features/settings/context/user-email-context'
|
|
|
|
import EmailsSection from '../../../../../../frontend/js/features/settings/components/emails-section'
|
2022-05-31 05:08:49 -04:00
|
|
|
import { Affiliation } from '../../../../../../types/affiliation'
|
2022-04-14 05:19:18 -04:00
|
|
|
|
2022-05-31 05:08:49 -04:00
|
|
|
const userData1: UserEmailData & { affiliation: Affiliation } = {
|
2022-04-14 05:19:18 -04:00
|
|
|
affiliation: {
|
|
|
|
cachedConfirmedAt: null,
|
2022-05-16 04:03:29 -04:00
|
|
|
cachedEntitlement: null,
|
|
|
|
cachedLastDayToReconfirm: null,
|
2022-04-14 05:19:18 -04:00
|
|
|
cachedPastReconfirmDate: false,
|
|
|
|
cachedReconfirmedAt: null,
|
|
|
|
department: null,
|
|
|
|
institution: {
|
|
|
|
commonsAccount: false,
|
|
|
|
confirmed: true,
|
|
|
|
id: 1,
|
|
|
|
isUniversity: false,
|
2022-05-16 04:03:29 -04:00
|
|
|
maxConfirmationMonths: null,
|
2022-04-14 05:19:18 -04:00
|
|
|
name: 'Overleaf',
|
|
|
|
ssoEnabled: false,
|
|
|
|
ssoBeta: false,
|
|
|
|
},
|
|
|
|
inReconfirmNotificationPeriod: false,
|
|
|
|
inferred: false,
|
|
|
|
licence: 'pro_plus',
|
|
|
|
pastReconfirmDate: false,
|
|
|
|
portal: { slug: '', templates_count: 1 },
|
|
|
|
role: null,
|
|
|
|
},
|
|
|
|
confirmedAt: '2022-03-09T10:59:44.139Z',
|
|
|
|
email: 'foo@overleaf.com',
|
|
|
|
default: true,
|
|
|
|
}
|
|
|
|
|
2022-05-31 05:08:49 -04:00
|
|
|
const userData2: UserEmailData & { affiliation: Affiliation } = {
|
2022-04-14 05:19:18 -04:00
|
|
|
affiliation: {
|
|
|
|
cachedConfirmedAt: null,
|
2022-05-16 04:03:29 -04:00
|
|
|
cachedEntitlement: null,
|
|
|
|
cachedLastDayToReconfirm: null,
|
2022-04-14 05:19:18 -04:00
|
|
|
cachedPastReconfirmDate: false,
|
|
|
|
cachedReconfirmedAt: null,
|
|
|
|
department: 'Art History',
|
|
|
|
institution: {
|
|
|
|
commonsAccount: false,
|
|
|
|
confirmed: true,
|
|
|
|
id: 1,
|
|
|
|
isUniversity: false,
|
2022-05-16 04:03:29 -04:00
|
|
|
maxConfirmationMonths: null,
|
2022-04-14 05:19:18 -04:00
|
|
|
name: 'Overleaf',
|
|
|
|
ssoEnabled: false,
|
|
|
|
ssoBeta: false,
|
|
|
|
},
|
|
|
|
inReconfirmNotificationPeriod: false,
|
|
|
|
inferred: false,
|
|
|
|
licence: 'pro_plus',
|
|
|
|
pastReconfirmDate: false,
|
|
|
|
portal: { slug: '', templates_count: 1 },
|
|
|
|
role: 'Reader',
|
|
|
|
},
|
|
|
|
email: 'baz@overleaf.com',
|
|
|
|
default: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('user role and institution', function () {
|
2022-04-22 09:49:26 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
window.metaAttributesCache = new Map()
|
|
|
|
window.metaAttributesCache.set('ol-ExposedSettings', {
|
|
|
|
hasAffiliationsFeature: true,
|
|
|
|
})
|
|
|
|
fetchMock.reset()
|
2023-03-16 08:32:48 -04:00
|
|
|
fetchMock.get('/user/emails?ensureAffiliation=true', [])
|
2022-04-22 09:49:26 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
window.metaAttributesCache = new Map()
|
|
|
|
fetchMock.reset()
|
|
|
|
})
|
|
|
|
|
2022-04-14 05:19:18 -04:00
|
|
|
it('renders affiliation name with add role/department button', function () {
|
|
|
|
const userEmailData = userData1
|
2022-04-22 09:49:26 -04:00
|
|
|
render(
|
|
|
|
<UserEmailsProvider>
|
|
|
|
<InstitutionAndRole userEmailData={userEmailData} />
|
|
|
|
</UserEmailsProvider>
|
|
|
|
)
|
2022-04-14 05:19:18 -04:00
|
|
|
|
|
|
|
screen.getByText(userEmailData.affiliation.institution.name, {
|
|
|
|
exact: false,
|
|
|
|
})
|
|
|
|
screen.getByRole('button', { name: /add role and department/i })
|
|
|
|
expect(screen.queryByRole('button', { name: /change/i })).to.not.exist
|
|
|
|
})
|
|
|
|
|
|
|
|
it('renders affiliation name, role and department with change button', function () {
|
|
|
|
const userEmailData = userData2
|
2022-04-22 09:49:26 -04:00
|
|
|
render(
|
|
|
|
<UserEmailsProvider>
|
|
|
|
<InstitutionAndRole userEmailData={userEmailData} />
|
|
|
|
</UserEmailsProvider>
|
|
|
|
)
|
2022-04-14 05:19:18 -04:00
|
|
|
|
|
|
|
screen.getByText(userEmailData.affiliation.institution.name, {
|
|
|
|
exact: false,
|
|
|
|
})
|
2022-05-31 05:08:49 -04:00
|
|
|
screen.getByText(userEmailData.affiliation.department!, { exact: false })
|
|
|
|
screen.getByText(userEmailData.affiliation.role!, { exact: false })
|
2022-04-14 05:19:18 -04:00
|
|
|
screen.getByRole('button', { name: /change/i })
|
|
|
|
expect(screen.queryByRole('button', { name: /add role and department/i }))
|
|
|
|
.to.not.exist
|
|
|
|
})
|
2022-04-22 09:49:26 -04:00
|
|
|
|
2022-04-29 07:09:53 -04:00
|
|
|
it('fetches institution data and replaces departments dropdown on add/change', async function () {
|
|
|
|
const userEmailData = userData1
|
2023-03-16 08:32:48 -04:00
|
|
|
fetchMock.get('/user/emails?ensureAffiliation=true', [userEmailData], {
|
|
|
|
overwriteRoutes: true,
|
|
|
|
})
|
2022-04-29 07:09:53 -04:00
|
|
|
render(<EmailsSection />)
|
|
|
|
|
|
|
|
await fetchMock.flush(true)
|
|
|
|
fetchMock.reset()
|
|
|
|
|
|
|
|
const fakeDepartment = 'Fake department'
|
|
|
|
const institution = userEmailData.affiliation.institution
|
|
|
|
fetchMock.get(`/institutions/list/${institution.id}`, {
|
|
|
|
id: institution.id,
|
|
|
|
name: institution.name,
|
|
|
|
country_code: 'de',
|
|
|
|
departments: [fakeDepartment],
|
|
|
|
})
|
|
|
|
|
|
|
|
fireEvent.click(
|
|
|
|
screen.getByRole('button', { name: /add role and department/i })
|
|
|
|
)
|
|
|
|
|
|
|
|
await fetchMock.flush(true)
|
|
|
|
fetchMock.reset()
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByRole('textbox', { name: /department/i }))
|
|
|
|
|
|
|
|
screen.getByText(fakeDepartment)
|
|
|
|
})
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
it('adds new role and department', async function () {
|
|
|
|
fetchMock
|
2023-03-16 08:32:48 -04:00
|
|
|
.get('/user/emails?ensureAffiliation=true', [userData1], {
|
|
|
|
overwriteRoutes: true,
|
|
|
|
})
|
2022-04-29 07:09:53 -04:00
|
|
|
.get(/\/institutions\/list/, { departments: [] })
|
2022-04-22 09:49:26 -04:00
|
|
|
.post('/user/emails/endorse', 200)
|
|
|
|
render(<EmailsSection />)
|
|
|
|
|
|
|
|
const addBtn = await screen.findByRole('button', {
|
|
|
|
name: /add role and department/i,
|
|
|
|
})
|
|
|
|
fireEvent.click(addBtn)
|
|
|
|
|
|
|
|
const submitBtn = screen.getByRole('button', {
|
|
|
|
name: /save/i,
|
|
|
|
}) as HTMLButtonElement
|
|
|
|
expect(submitBtn.disabled).to.be.true
|
|
|
|
|
|
|
|
const roleValue = 'Dummy role'
|
|
|
|
const departmentValue = 'Dummy department'
|
|
|
|
|
|
|
|
const roleInput = screen.getByPlaceholderText(/role/i)
|
|
|
|
fireEvent.change(roleInput, {
|
|
|
|
target: { value: roleValue },
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(submitBtn.disabled).to.be.true
|
|
|
|
|
|
|
|
const departmentInput = screen.getByPlaceholderText(/department/i)
|
|
|
|
fireEvent.change(departmentInput, {
|
|
|
|
target: { value: departmentValue },
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(submitBtn.disabled).to.be.false
|
|
|
|
|
|
|
|
fireEvent.click(submitBtn)
|
|
|
|
|
|
|
|
expect(submitBtn.disabled).to.be.true
|
|
|
|
|
|
|
|
await waitForElementToBeRemoved(() =>
|
|
|
|
screen.getByRole('button', { name: /saving/i })
|
|
|
|
)
|
|
|
|
|
|
|
|
screen.getByText(roleValue, { exact: false })
|
|
|
|
screen.getByText(departmentValue, { exact: false })
|
|
|
|
})
|
2022-04-14 05:19:18 -04:00
|
|
|
})
|