overall-theme-setting.test.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { screen, within, render } from '@testing-library/react'
  2. import { expect } from 'chai'
  3. import fetchMock from 'fetch-mock'
  4. import type { OverallThemeMeta } from '../../../../../types/project-settings'
  5. import getMeta from '@/utils/meta'
  6. import { EditorProviders } from '../../../helpers/editor-providers'
  7. import { SettingsModalProvider } from '@/features/ide-redesign/contexts/settings-modal-context'
  8. import OverallThemeSetting from '@/features/settings/components/appearance-settings/overall-theme-setting'
  9. import userEvent from '@testing-library/user-event'
  10. const IEEE_BRAND_ID = 1234
  11. const OTHER_BRAND_ID = 2234
  12. describe('<OverallThemeSetting />', function () {
  13. const overallThemes: OverallThemeMeta[] = [
  14. {
  15. name: 'Overall Theme 1',
  16. val: '',
  17. path: 'https://overleaf.com/overalltheme-1.css',
  18. },
  19. {
  20. name: 'Overall Theme 2',
  21. val: 'light-',
  22. path: 'https://overleaf.com/overalltheme-2.css',
  23. },
  24. ]
  25. beforeEach(function () {
  26. window.metaAttributesCache.set('ol-overallThemes', overallThemes)
  27. Object.assign(getMeta('ol-ExposedSettings'), {
  28. ieeeBrandId: IEEE_BRAND_ID,
  29. })
  30. })
  31. afterEach(function () {
  32. fetchMock.removeRoutes().clearHistory()
  33. })
  34. it('each option is shown and can be selected', async function () {
  35. render(
  36. <EditorProviders>
  37. <SettingsModalProvider>
  38. <OverallThemeSetting />
  39. </SettingsModalProvider>
  40. </EditorProviders>
  41. )
  42. const saveSettingsMock = fetchMock.post(
  43. 'express:/user/settings',
  44. {
  45. status: 200,
  46. },
  47. { delay: 0 }
  48. )
  49. const select = screen.getByLabelText('Overall theme')
  50. // Reverse order so we test changing to each option
  51. for (const theme of overallThemes.reverse()) {
  52. const option = within(select).getByText(theme.name)
  53. expect(option.getAttribute('value')).to.equal(theme.val)
  54. await userEvent.selectOptions(select, [option])
  55. expect(
  56. saveSettingsMock.callHistory.called('/user/settings', {
  57. body: { overallTheme: theme.val },
  58. })
  59. ).to.be.true
  60. }
  61. })
  62. describe('Branded Project', function () {
  63. it('should hide overall theme picker for IEEE branded projects', function () {
  64. window.metaAttributesCache.set('ol-brandVariation', {
  65. brand_id: IEEE_BRAND_ID,
  66. })
  67. render(
  68. <EditorProviders>
  69. <SettingsModalProvider>
  70. <OverallThemeSetting />
  71. </SettingsModalProvider>
  72. </EditorProviders>
  73. )
  74. const select = screen.queryByText('Overall theme')
  75. expect(select).to.not.exist
  76. })
  77. it('should show overall theme picker for branded projects that are not IEEE', function () {
  78. window.metaAttributesCache.set('ol-brandVariation', {
  79. brand_id: OTHER_BRAND_ID,
  80. })
  81. render(
  82. <EditorProviders>
  83. <SettingsModalProvider>
  84. <OverallThemeSetting />
  85. </SettingsModalProvider>
  86. </EditorProviders>
  87. )
  88. const select = screen.getByLabelText('Overall theme')
  89. expect(select).to.exist
  90. })
  91. it('should show overall theme picker for non branded projects', function () {
  92. window.metaAttributesCache.set('ol-brandVariation', undefined)
  93. render(
  94. <EditorProviders>
  95. <SettingsModalProvider>
  96. <OverallThemeSetting />
  97. </SettingsModalProvider>
  98. </EditorProviders>
  99. )
  100. const select = screen.getByLabelText('Overall theme')
  101. expect(select).to.exist
  102. })
  103. })
  104. })