emails-row.test.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { render, screen } from '@testing-library/react'
  2. import { expect } from 'chai'
  3. import fetchMock from 'fetch-mock'
  4. import { cloneDeep } from 'lodash'
  5. import EmailsRow from '../../../../../../frontend/js/features/settings/components/emails/row'
  6. import {
  7. professionalUserData,
  8. unconfirmedUserData,
  9. } from '../../fixtures/test-user-email-data'
  10. import { UserEmailData } from '../../../../../../types/user-email'
  11. import { UserEmailsProvider } from '../../../../../../frontend/js/features/settings/context/user-email-context'
  12. import { Affiliation } from '../../../../../../types/affiliation'
  13. function renderEmailsRow(data: UserEmailData) {
  14. return render(
  15. <UserEmailsProvider>
  16. <EmailsRow userEmailData={data} />
  17. </UserEmailsProvider>
  18. )
  19. }
  20. function getByTextContent(text: string) {
  21. return screen.getAllByText(
  22. (content, node) =>
  23. content === text || node?.children[0]?.textContent === text
  24. )
  25. }
  26. describe('<EmailsRow/>', function () {
  27. beforeEach(function () {
  28. window.metaAttributesCache = window.metaAttributesCache || new Map()
  29. window.metaAttributesCache.set('ol-ExposedSettings', {
  30. samlInitPath: '/saml',
  31. hasSamlBeta: true,
  32. })
  33. })
  34. describe('with unaffiliated email data', function () {
  35. it('renders email info', function () {
  36. renderEmailsRow(unconfirmedUserData)
  37. screen.getByText('baz@overleaf.com')
  38. })
  39. it('renders actions', function () {
  40. renderEmailsRow(unconfirmedUserData)
  41. screen.getByRole('button', { name: 'Make Primary' })
  42. })
  43. })
  44. describe('with affiliated email data', function () {
  45. it('renders email info', function () {
  46. renderEmailsRow(professionalUserData)
  47. screen.getByText('foo@overleaf.com')
  48. screen.getByText('Primary')
  49. })
  50. it('renders actions', function () {
  51. renderEmailsRow(professionalUserData)
  52. screen.getByRole('button', { name: 'Remove' })
  53. })
  54. it('renders institution info', function () {
  55. renderEmailsRow(professionalUserData)
  56. screen.getByText('Overleaf')
  57. screen.getByText('Reader, Art History')
  58. })
  59. })
  60. describe('with email data affiliated to an institution with SSO available', function () {
  61. let affiliatedEmail: UserEmailData & { affiliation: Affiliation }
  62. beforeEach(function () {
  63. window.metaAttributesCache.get('ol-ExposedSettings').hasSamlFeature = true
  64. // make sure the institution has SSO available
  65. affiliatedEmail = cloneDeep(professionalUserData)
  66. affiliatedEmail.affiliation.institution.confirmed = true
  67. affiliatedEmail.affiliation.institution.isUniversity = true
  68. affiliatedEmail.affiliation.institution.ssoEnabled = true
  69. })
  70. describe('when the email is not yet linked to the institution', function () {
  71. beforeEach(async function () {
  72. fetchMock.reset()
  73. fetchMock.get(/\/user\/emails/, [affiliatedEmail, unconfirmedUserData])
  74. await fetchMock.flush(true)
  75. })
  76. it('prompts the user to link to their institutional account', function () {
  77. renderEmailsRow(affiliatedEmail)
  78. getByTextContent(
  79. 'You can now link your Overleaf account to your Overleaf institutional account.'
  80. )
  81. screen.getByRole('button', { name: 'Link Accounts' })
  82. })
  83. })
  84. describe('when the email is already linked to the institution', function () {
  85. beforeEach(async function () {
  86. affiliatedEmail.samlProviderId = '1'
  87. fetchMock.reset()
  88. fetchMock.get(/\/user\/emails/, [affiliatedEmail, unconfirmedUserData])
  89. await fetchMock.flush(true)
  90. })
  91. it('prompts the user to login using their institutional account', function () {
  92. renderEmailsRow(affiliatedEmail)
  93. getByTextContent(
  94. 'You can log in to Overleaf through your Overleaf institutional login.'
  95. )
  96. expect(screen.queryByRole('button', { name: 'Link Accounts' })).to.be
  97. .null
  98. })
  99. })
  100. })
  101. })