2022-05-26 04:32:55 -04:00
|
|
|
import { render, screen } from '@testing-library/react'
|
|
|
|
import { expect } from 'chai'
|
|
|
|
import fetchMock from 'fetch-mock'
|
|
|
|
import { cloneDeep } from 'lodash'
|
|
|
|
import EmailsRow from '../../../../../../frontend/js/features/settings/components/emails/row'
|
|
|
|
import {
|
|
|
|
professionalUserData,
|
|
|
|
unconfirmedUserData,
|
|
|
|
} from '../../fixtures/test-user-email-data'
|
|
|
|
import { UserEmailData } from '../../../../../../types/user-email'
|
|
|
|
import { UserEmailsProvider } from '../../../../../../frontend/js/features/settings/context/user-email-context'
|
2022-05-31 05:08:49 -04:00
|
|
|
import { Affiliation } from '../../../../../../types/affiliation'
|
2022-05-26 04:32:55 -04:00
|
|
|
|
|
|
|
function renderEmailsRow(data: UserEmailData) {
|
|
|
|
return render(
|
|
|
|
<UserEmailsProvider>
|
|
|
|
<EmailsRow userEmailData={data} />
|
|
|
|
</UserEmailsProvider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getByTextContent(text: string) {
|
|
|
|
return screen.getAllByText(
|
|
|
|
(content, node) =>
|
2022-05-31 05:08:49 -04:00
|
|
|
content === text || node?.children[0]?.textContent === text
|
2022-05-26 04:32:55 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('<EmailsRow/>', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
window.metaAttributesCache = window.metaAttributesCache || new Map()
|
|
|
|
window.metaAttributesCache.set('ol-ExposedSettings', {
|
|
|
|
samlInitPath: '/saml',
|
|
|
|
hasSamlBeta: true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with unaffiliated email data', function () {
|
|
|
|
it('renders email info', function () {
|
|
|
|
renderEmailsRow(unconfirmedUserData)
|
|
|
|
screen.getByText('baz@overleaf.com')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('renders actions', function () {
|
|
|
|
renderEmailsRow(unconfirmedUserData)
|
|
|
|
screen.getByRole('button', { name: 'Make Primary' })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with affiliated email data', function () {
|
|
|
|
it('renders email info', function () {
|
|
|
|
renderEmailsRow(professionalUserData)
|
2022-06-07 03:45:14 -04:00
|
|
|
screen.getByText('foo@overleaf.com')
|
|
|
|
screen.getByText('Primary')
|
2022-05-26 04:32:55 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('renders actions', function () {
|
|
|
|
renderEmailsRow(professionalUserData)
|
|
|
|
screen.getByRole('button', { name: 'Remove' })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('renders institution info', function () {
|
|
|
|
renderEmailsRow(professionalUserData)
|
|
|
|
screen.getByText('Overleaf')
|
|
|
|
screen.getByText('Reader, Art History')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with email data affiliated to an institution with SSO available', function () {
|
2022-05-31 05:08:49 -04:00
|
|
|
let affiliatedEmail: UserEmailData & { affiliation: Affiliation }
|
2022-05-26 04:32:55 -04:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
window.metaAttributesCache.get('ol-ExposedSettings').hasSamlFeature = true
|
|
|
|
|
|
|
|
// make sure the institution has SSO available
|
|
|
|
affiliatedEmail = cloneDeep(professionalUserData)
|
|
|
|
affiliatedEmail.affiliation.institution.confirmed = true
|
|
|
|
affiliatedEmail.affiliation.institution.isUniversity = true
|
|
|
|
affiliatedEmail.affiliation.institution.ssoEnabled = true
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the email is not yet linked to the institution', function () {
|
|
|
|
beforeEach(async function () {
|
|
|
|
fetchMock.reset()
|
|
|
|
fetchMock.get(/\/user\/emails/, [affiliatedEmail, unconfirmedUserData])
|
|
|
|
await fetchMock.flush(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('prompts the user to link to their institutional account', function () {
|
|
|
|
renderEmailsRow(affiliatedEmail)
|
|
|
|
getByTextContent(
|
|
|
|
'You can now link your Overleaf account to your Overleaf institutional account.'
|
|
|
|
)
|
2022-05-30 09:47:42 -04:00
|
|
|
screen.getByRole('button', { name: 'Link Accounts' })
|
2022-05-26 04:32:55 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the email is already linked to the institution', function () {
|
|
|
|
beforeEach(async function () {
|
|
|
|
affiliatedEmail.samlProviderId = '1'
|
|
|
|
fetchMock.reset()
|
|
|
|
fetchMock.get(/\/user\/emails/, [affiliatedEmail, unconfirmedUserData])
|
|
|
|
await fetchMock.flush(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('prompts the user to login using their institutional account', function () {
|
|
|
|
renderEmailsRow(affiliatedEmail)
|
|
|
|
getByTextContent(
|
|
|
|
'You can log in to Overleaf through your Overleaf institutional login.'
|
|
|
|
)
|
2022-05-30 09:47:42 -04:00
|
|
|
expect(screen.queryByRole('button', { name: 'Link Accounts' })).to.be
|
|
|
|
.null
|
2022-05-26 04:32:55 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|