editor-theme-setting.test.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/ide-redesign/contexts/settings-modal-context'
  5. import { EditorProviders } from '../../../helpers/editor-providers'
  6. import EditorThemeSetting from '@/features/ide-redesign/components/settings/appearance-settings/editor-theme-setting'
  7. import userEvent from '@testing-library/user-event'
  8. describe('<EditorThemeSetting />', function () {
  9. const editorThemes = ['editortheme-1', 'editortheme-2', 'editortheme-3']
  10. const legacyEditorThemes = ['legacytheme-1', 'legacytheme-2', 'legacytheme-3']
  11. beforeEach(function () {
  12. window.metaAttributesCache.set('ol-editorThemes', editorThemes)
  13. window.metaAttributesCache.set('ol-legacyEditorThemes', legacyEditorThemes)
  14. })
  15. afterEach(function () {
  16. fetchMock.removeRoutes().clearHistory()
  17. })
  18. it('each option is shown and can be selected', async function () {
  19. render(
  20. <EditorProviders>
  21. <SettingsModalProvider>
  22. <EditorThemeSetting />
  23. </SettingsModalProvider>
  24. </EditorProviders>
  25. )
  26. const saveSettingsMock = fetchMock.post(
  27. `express:/user/settings`,
  28. {
  29. status: 200,
  30. },
  31. { delay: 0 }
  32. )
  33. const select = screen.getByLabelText('Editor theme')
  34. for (const theme of editorThemes) {
  35. const option = within(select).getByText(theme.replace(/_/g, ' '))
  36. expect(option.getAttribute('value')).to.equal(theme)
  37. await userEvent.selectOptions(select, [option])
  38. expect(
  39. saveSettingsMock.callHistory.called(`/user/settings`, {
  40. body: { editorTheme: theme },
  41. })
  42. ).to.be.true
  43. }
  44. for (const theme of legacyEditorThemes) {
  45. const option = within(select).getByText(
  46. theme.replace(/_/g, ' ') + ' (Legacy)'
  47. )
  48. expect(option.getAttribute('value')).to.equal(theme)
  49. await userEvent.selectOptions(select, [option])
  50. expect(
  51. saveSettingsMock.callHistory.called(`/user/settings`, {
  52. body: { editorTheme: theme },
  53. })
  54. ).to.be.true
  55. }
  56. })
  57. })