overall-theme-setting.test.tsx 3.6 KB

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