emails-row.test.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 (primary)')
  48. })
  49. it('renders actions', function () {
  50. renderEmailsRow(professionalUserData)
  51. screen.getByRole('button', { name: 'Remove' })
  52. })
  53. it('renders institution info', function () {
  54. renderEmailsRow(professionalUserData)
  55. screen.getByText('Overleaf')
  56. screen.getByText('Reader, Art History')
  57. })
  58. })
  59. describe('with email data affiliated to an institution with SSO available', function () {
  60. let affiliatedEmail: UserEmailData & { affiliation: Affiliation }
  61. beforeEach(function () {
  62. window.metaAttributesCache.get('ol-ExposedSettings').hasSamlFeature = true
  63. // make sure the institution has SSO available
  64. affiliatedEmail = cloneDeep(professionalUserData)
  65. affiliatedEmail.affiliation.institution.confirmed = true
  66. affiliatedEmail.affiliation.institution.isUniversity = true
  67. affiliatedEmail.affiliation.institution.ssoEnabled = true
  68. })
  69. describe('when the email is not yet linked to the institution', function () {
  70. beforeEach(async function () {
  71. fetchMock.reset()
  72. fetchMock.get(/\/user\/emails/, [affiliatedEmail, unconfirmedUserData])
  73. await fetchMock.flush(true)
  74. })
  75. it('prompts the user to link to their institutional account', function () {
  76. renderEmailsRow(affiliatedEmail)
  77. getByTextContent(
  78. 'You can now link your Overleaf account to your Overleaf institutional account.'
  79. )
  80. screen.getByRole('button', { name: 'Link Accounts' })
  81. })
  82. })
  83. describe('when the email is already linked to the institution', function () {
  84. beforeEach(async function () {
  85. affiliatedEmail.samlProviderId = '1'
  86. fetchMock.reset()
  87. fetchMock.get(/\/user\/emails/, [affiliatedEmail, unconfirmedUserData])
  88. await fetchMock.flush(true)
  89. })
  90. it('prompts the user to login using their institutional account', function () {
  91. renderEmailsRow(affiliatedEmail)
  92. getByTextContent(
  93. 'You can log in to Overleaf through your Overleaf institutional login.'
  94. )
  95. expect(screen.queryByRole('button', { name: 'Link Accounts' })).to.be
  96. .null
  97. })
  98. })
  99. })
  100. })