2022-04-08 07:00:46 -04:00
|
|
|
import {
|
|
|
|
render,
|
|
|
|
screen,
|
|
|
|
within,
|
|
|
|
fireEvent,
|
2022-04-22 09:49:26 -04:00
|
|
|
waitFor,
|
2022-04-08 07:00:46 -04:00
|
|
|
waitForElementToBeRemoved,
|
|
|
|
} from '@testing-library/react'
|
|
|
|
import EmailsSection from '../../../../../../frontend/js/features/settings/components/emails-section'
|
|
|
|
import { expect } from 'chai'
|
|
|
|
import fetchMock from 'fetch-mock'
|
2022-05-24 03:48:06 -04:00
|
|
|
import {
|
|
|
|
confirmedUserData,
|
|
|
|
fakeUsersData,
|
|
|
|
professionalUserData,
|
|
|
|
unconfirmedUserData,
|
|
|
|
} from '../../fixtures/test-user-email-data'
|
2022-04-08 07:00:46 -04:00
|
|
|
|
|
|
|
describe('<EmailsSection />', function () {
|
2022-04-22 09:49:26 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
window.metaAttributesCache.set('ol-ExposedSettings', {
|
|
|
|
hasAffiliationsFeature: true,
|
|
|
|
})
|
|
|
|
fetchMock.reset()
|
|
|
|
})
|
|
|
|
|
2022-04-08 07:00:46 -04:00
|
|
|
afterEach(function () {
|
|
|
|
window.metaAttributesCache = new Map()
|
|
|
|
fetchMock.reset()
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
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)
|
2022-04-08 07:00:46 -04:00
|
|
|
render(<EmailsSection />)
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
await waitFor(() => {
|
|
|
|
fakeUsersData.forEach(userData => {
|
|
|
|
screen.getByText(new RegExp(userData.email, 'i'))
|
|
|
|
})
|
2022-04-08 07:00:46 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
it('renders primary status', async function () {
|
|
|
|
fetchMock.get('/user/emails?ensureAffiliation=true', [professionalUserData])
|
2022-04-08 07:00:46 -04:00
|
|
|
render(<EmailsSection />)
|
|
|
|
|
2022-06-07 03:45:14 -04:00
|
|
|
await screen.findByText(`${professionalUserData.email}`)
|
|
|
|
screen.getByText('Primary')
|
2022-04-08 07:00:46 -04:00
|
|
|
})
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
it('shows confirmation status for unconfirmed users', async function () {
|
|
|
|
fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
|
2022-04-08 07:00:46 -04:00
|
|
|
render(<EmailsSection />)
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
await screen.findByText(/please check your inbox/i)
|
2022-04-08 07:00:46 -04:00
|
|
|
})
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
it('hides confirmation status for confirmed users', async function () {
|
|
|
|
fetchMock.get('/user/emails?ensureAffiliation=true', [confirmedUserData])
|
2022-04-08 07:00:46 -04:00
|
|
|
render(<EmailsSection />)
|
2022-04-22 09:49:26 -04:00
|
|
|
await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
|
2022-04-08 07:00:46 -04:00
|
|
|
|
|
|
|
expect(screen.queryByText(/please check your inbox/i)).to.be.null
|
|
|
|
})
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
it('renders resend link', async function () {
|
|
|
|
fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
|
2022-04-08 07:00:46 -04:00
|
|
|
render(<EmailsSection />)
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
await screen.findByRole('button', { name: /resend confirmation email/i })
|
2022-04-08 07:00:46 -04:00
|
|
|
})
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
it('renders professional label', async function () {
|
|
|
|
fetchMock.get('/user/emails?ensureAffiliation=true', [professionalUserData])
|
2022-04-08 07:00:46 -04:00
|
|
|
render(<EmailsSection />)
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
const node = await screen.findByText(professionalUserData.email, {
|
2022-04-08 07:00:46 -04:00
|
|
|
exact: false,
|
|
|
|
})
|
|
|
|
expect(within(node).getByText(/professional/i)).to.exist
|
|
|
|
})
|
|
|
|
|
|
|
|
it('shows loader when resending email', async function () {
|
2022-04-22 09:49:26 -04:00
|
|
|
fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
|
|
|
|
|
2022-04-08 07:00:46 -04:00
|
|
|
render(<EmailsSection />)
|
2022-04-22 09:49:26 -04:00
|
|
|
await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
|
|
|
|
|
|
|
|
fetchMock.post('/user/emails/resend_confirmation', 200)
|
2022-04-08 07:00:46 -04:00
|
|
|
|
|
|
|
const button = screen.getByRole('button', {
|
|
|
|
name: /resend confirmation email/i,
|
|
|
|
})
|
|
|
|
fireEvent.click(button)
|
|
|
|
|
|
|
|
expect(
|
|
|
|
screen.queryByRole('button', {
|
|
|
|
name: /resend confirmation email/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.findByRole('button', {
|
|
|
|
name: /resend confirmation email/i,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('shows error when resending email fails', async function () {
|
2022-04-22 09:49:26 -04:00
|
|
|
fetchMock.get('/user/emails?ensureAffiliation=true', [unconfirmedUserData])
|
|
|
|
|
2022-04-08 07:00:46 -04:00
|
|
|
render(<EmailsSection />)
|
2022-04-22 09:49:26 -04:00
|
|
|
await waitForElementToBeRemoved(() => screen.getByText(/loading/i))
|
|
|
|
|
|
|
|
fetchMock.post('/user/emails/resend_confirmation', 503)
|
2022-04-08 07:00:46 -04:00
|
|
|
|
|
|
|
const button = screen.getByRole('button', {
|
|
|
|
name: /resend confirmation email/i,
|
|
|
|
})
|
|
|
|
fireEvent.click(button)
|
|
|
|
|
|
|
|
expect(
|
|
|
|
screen.queryByRole('button', {
|
|
|
|
name: /resend confirmation email/i,
|
|
|
|
})
|
|
|
|
).to.be.null
|
|
|
|
|
|
|
|
await waitForElementToBeRemoved(() => screen.getByText(/sending/i))
|
|
|
|
|
2022-04-29 07:10:10 -04:00
|
|
|
screen.getByText(/sorry, something went wrong/i)
|
2022-04-08 07:00:46 -04:00
|
|
|
screen.getByRole('button', { name: /resend confirmation email/i })
|
|
|
|
})
|
|
|
|
})
|