editor-theme-setting.test.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { screen, within, render } from '@testing-library/react'
  2. import { expect } from 'chai'
  3. import fetchMock from 'fetch-mock'
  4. import { SettingsModalProvider } from '@/features/settings/context/settings-modal-context'
  5. import { EditorProviders } from '../../../helpers/editor-providers'
  6. import EditorThemeSetting from '@/features/settings/components/appearance-settings/editor-theme-setting'
  7. import userEvent from '@testing-library/user-event'
  8. const MOCK_IEEE_BRAND_ID = 123
  9. describe('<EditorThemeSetting />', function () {
  10. const editorThemes = [
  11. { name: 'editortheme-1', dark: false },
  12. { name: 'editortheme-2', dark: false },
  13. { name: 'editortheme-3', dark: false },
  14. ]
  15. const legacyEditorThemes = [
  16. { name: 'legacytheme-1', dark: false },
  17. { name: 'legacytheme-2', dark: false },
  18. { name: 'legacytheme-3', dark: false },
  19. ]
  20. beforeEach(function () {
  21. window.metaAttributesCache.set('ol-editorThemes', editorThemes)
  22. window.metaAttributesCache.set('ol-legacyEditorThemes', legacyEditorThemes)
  23. window.metaAttributesCache.set('ol-brandVariation', {
  24. brand_id: undefined,
  25. })
  26. window.metaAttributesCache.get('ol-ExposedSettings').ieeeBrandId =
  27. MOCK_IEEE_BRAND_ID
  28. })
  29. afterEach(function () {
  30. fetchMock.removeRoutes().clearHistory()
  31. })
  32. async function checkSelect(select: HTMLElement, settingName: string) {
  33. const saveSettingsMock = fetchMock.post(
  34. `express:/user/settings`,
  35. {
  36. status: 200,
  37. },
  38. { delay: 0 }
  39. )
  40. for (const theme of editorThemes) {
  41. const option = within(select).getByText(theme.name.replace(/_/g, ' '))
  42. expect(option.getAttribute('value')).to.equal(theme.name)
  43. await userEvent.selectOptions(select, [option])
  44. expect(
  45. saveSettingsMock.callHistory.called(`/user/settings`, {
  46. body: { [settingName]: theme.name },
  47. })
  48. ).to.be.true
  49. }
  50. for (const theme of legacyEditorThemes) {
  51. const option = within(select).getByText(
  52. theme.name.replace(/_/g, ' ') + ' (Legacy)'
  53. )
  54. expect(option.getAttribute('value')).to.equal(theme.name)
  55. await userEvent.selectOptions(select, [option])
  56. expect(
  57. saveSettingsMock.callHistory.called(`/user/settings`, {
  58. body: { [settingName]: theme.name },
  59. })
  60. ).to.be.true
  61. }
  62. }
  63. describe('with default theme', function () {
  64. beforeEach(function () {
  65. render(
  66. <EditorProviders userSettings={{ overallTheme: '' }}>
  67. <SettingsModalProvider>
  68. <EditorThemeSetting />
  69. </SettingsModalProvider>
  70. </EditorProviders>
  71. )
  72. })
  73. it('each option is shown and can be selected', async function () {
  74. const select = screen.getByLabelText('Editor theme')
  75. expect(select).to.exist
  76. await checkSelect(select, 'editorTheme')
  77. })
  78. })
  79. describe('with system theme', function () {
  80. beforeEach(function () {
  81. render(
  82. <EditorProviders userSettings={{ overallTheme: 'system' }}>
  83. <SettingsModalProvider>
  84. <EditorThemeSetting />
  85. </SettingsModalProvider>
  86. </EditorProviders>
  87. )
  88. })
  89. it('splits the setting into two', async function () {
  90. const select = screen.queryByLabelText('Editor theme')
  91. expect(select).to.not.exist
  92. const lightModeSelect = screen.getByLabelText('Light editor theme')
  93. expect(lightModeSelect).to.exist
  94. await checkSelect(lightModeSelect, 'editorLightTheme')
  95. const darkModeSelect = screen.getByLabelText('Dark editor theme')
  96. expect(darkModeSelect).to.exist
  97. await checkSelect(darkModeSelect, 'editorDarkTheme')
  98. })
  99. })
  100. describe('with IEEE branding', function () {
  101. beforeEach(function () {
  102. window.metaAttributesCache.set('ol-brandVariation', {
  103. brand_id: MOCK_IEEE_BRAND_ID,
  104. })
  105. render(
  106. <EditorProviders userSettings={{ overallTheme: 'system' }}>
  107. <SettingsModalProvider>
  108. <EditorThemeSetting />
  109. </SettingsModalProvider>
  110. </EditorProviders>
  111. )
  112. })
  113. it('ignores the system theme and shows single selection', async function () {
  114. const select = screen.getByLabelText('Editor theme')
  115. expect(select).to.exist
  116. await checkSelect(select, 'editorTheme')
  117. })
  118. })
  119. })