overall-theme-setting.test.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/settings/context/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. },
  18. {
  19. name: 'Overall Theme 2',
  20. val: 'light-',
  21. },
  22. ]
  23. beforeEach(function () {
  24. window.metaAttributesCache.set('ol-overallThemes', overallThemes)
  25. Object.assign(getMeta('ol-ExposedSettings'), {
  26. ieeeBrandId: IEEE_BRAND_ID,
  27. })
  28. })
  29. afterEach(function () {
  30. fetchMock.removeRoutes().clearHistory()
  31. })
  32. it('each option is shown and can be selected', async function () {
  33. render(
  34. <EditorProviders>
  35. <SettingsModalProvider>
  36. <OverallThemeSetting />
  37. </SettingsModalProvider>
  38. </EditorProviders>
  39. )
  40. const saveSettingsMock = fetchMock.post(
  41. 'express:/user/settings',
  42. {
  43. status: 200,
  44. },
  45. { delay: 0 }
  46. )
  47. const select = screen.getByLabelText('Overall theme')
  48. // Reverse order so we test changing to each option
  49. for (const theme of overallThemes.reverse()) {
  50. const option = within(select).getByText(theme.name)
  51. expect(option.getAttribute('value')).to.equal(theme.val)
  52. await userEvent.selectOptions(select, [option])
  53. expect(
  54. saveSettingsMock.callHistory.called('/user/settings', {
  55. body: { overallTheme: theme.val },
  56. })
  57. ).to.be.true
  58. }
  59. })
  60. describe('Branded Project', function () {
  61. it('should hide overall theme picker for IEEE branded projects', function () {
  62. window.metaAttributesCache.set('ol-brandVariation', {
  63. brand_id: IEEE_BRAND_ID,
  64. })
  65. render(
  66. <EditorProviders>
  67. <SettingsModalProvider>
  68. <OverallThemeSetting />
  69. </SettingsModalProvider>
  70. </EditorProviders>
  71. )
  72. const select = screen.queryByText('Overall theme')
  73. expect(select).to.not.exist
  74. })
  75. it('should show overall theme picker for branded projects that are not IEEE', function () {
  76. window.metaAttributesCache.set('ol-brandVariation', {
  77. brand_id: OTHER_BRAND_ID,
  78. })
  79. render(
  80. <EditorProviders>
  81. <SettingsModalProvider>
  82. <OverallThemeSetting />
  83. </SettingsModalProvider>
  84. </EditorProviders>
  85. )
  86. const select = screen.getByLabelText('Overall theme')
  87. expect(select).to.exist
  88. })
  89. it('should show overall theme picker for non branded projects', function () {
  90. window.metaAttributesCache.set('ol-brandVariation', undefined)
  91. render(
  92. <EditorProviders>
  93. <SettingsModalProvider>
  94. <OverallThemeSetting />
  95. </SettingsModalProvider>
  96. </EditorProviders>
  97. )
  98. const select = screen.getByLabelText('Overall theme')
  99. expect(select).to.exist
  100. })
  101. })
  102. })