editor-theme-setting.test.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 = [
  10. { name: 'editortheme-1', dark: false },
  11. { name: 'editortheme-2', dark: false },
  12. { name: 'editortheme-3', dark: false },
  13. ]
  14. const legacyEditorThemes = [
  15. { name: 'legacytheme-1', dark: false },
  16. { name: 'legacytheme-2', dark: false },
  17. { name: 'legacytheme-3', dark: false },
  18. ]
  19. beforeEach(function () {
  20. window.metaAttributesCache.set('ol-editorThemes', editorThemes)
  21. window.metaAttributesCache.set('ol-legacyEditorThemes', legacyEditorThemes)
  22. })
  23. afterEach(function () {
  24. fetchMock.removeRoutes().clearHistory()
  25. })
  26. it('each option is shown and can be selected', async function () {
  27. render(
  28. <EditorProviders>
  29. <SettingsModalProvider>
  30. <EditorThemeSetting />
  31. </SettingsModalProvider>
  32. </EditorProviders>
  33. )
  34. const saveSettingsMock = fetchMock.post(
  35. `express:/user/settings`,
  36. {
  37. status: 200,
  38. },
  39. { delay: 0 }
  40. )
  41. const select = screen.getByLabelText('Editor theme')
  42. for (const theme of editorThemes) {
  43. const option = within(select).getByText(theme.name.replace(/_/g, ' '))
  44. expect(option.getAttribute('value')).to.equal(theme.name)
  45. await userEvent.selectOptions(select, [option])
  46. expect(
  47. saveSettingsMock.callHistory.called(`/user/settings`, {
  48. body: { editorTheme: theme.name },
  49. })
  50. ).to.be.true
  51. }
  52. for (const theme of legacyEditorThemes) {
  53. const option = within(select).getByText(
  54. theme.name.replace(/_/g, ' ') + ' (Legacy)'
  55. )
  56. expect(option.getAttribute('value')).to.equal(theme.name)
  57. await userEvent.selectOptions(select, [option])
  58. expect(
  59. saveSettingsMock.callHistory.called(`/user/settings`, {
  60. body: { editorTheme: theme.name },
  61. })
  62. ).to.be.true
  63. }
  64. })
  65. })