root-document-setting.test.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { screen, within, render } from '@testing-library/react'
  2. import { expect } from 'chai'
  3. import fetchMock from 'fetch-mock'
  4. import { Folder } from '../../../../../types/folder'
  5. import { SettingsModalProvider } from '@/features/settings/context/settings-modal-context'
  6. import {
  7. EditorProviders,
  8. projectDefaults,
  9. } from '../../../helpers/editor-providers'
  10. import RootDocumentSetting from '@/features/settings/components/compiler-settings/root-document-setting'
  11. import userEvent from '@testing-library/user-event'
  12. const OPTIONS = [
  13. {
  14. label: 'main.tex',
  15. value: '123abc',
  16. },
  17. {
  18. label: 'another.tex',
  19. value: '123abcd',
  20. },
  21. ]
  22. describe('<RootDocumentSetting />', function () {
  23. const rootFolder: Folder = {
  24. _id: 'root-folder-id',
  25. name: 'rootFolder',
  26. docs: [
  27. {
  28. _id: '123abc',
  29. name: 'main.tex',
  30. },
  31. {
  32. _id: '123abcd',
  33. name: 'another.tex',
  34. },
  35. ],
  36. fileRefs: [],
  37. folders: [],
  38. }
  39. let originalSettings: typeof window.metaAttributesCache
  40. beforeEach(function () {
  41. originalSettings = window.metaAttributesCache.get('ol-ExposedSettings')
  42. window.metaAttributesCache.set('ol-ExposedSettings', {
  43. validRootDocExtensions: ['tex'],
  44. })
  45. })
  46. afterEach(function () {
  47. fetchMock.removeRoutes().clearHistory()
  48. window.metaAttributesCache.set('ol-ExposedSettings', originalSettings)
  49. })
  50. it('each option is shown and can be selected', async function () {
  51. render(
  52. <EditorProviders rootFolder={[rootFolder as any]}>
  53. <SettingsModalProvider>
  54. <RootDocumentSetting />
  55. </SettingsModalProvider>
  56. </EditorProviders>
  57. )
  58. const saveSettingsMock = fetchMock.post(
  59. `express:/project/:projectId/settings`,
  60. {
  61. status: 200,
  62. },
  63. { delay: 0 }
  64. )
  65. const select = screen.getByLabelText('Main document')
  66. // Reverse order so we test changing to each option
  67. for (const option of OPTIONS.reverse()) {
  68. const optionElement = within(select).getByText(option.label)
  69. expect(optionElement.getAttribute('value')).to.equal(option.value)
  70. await userEvent.selectOptions(select, [optionElement])
  71. expect(
  72. saveSettingsMock.callHistory.called(
  73. `/project/${projectDefaults._id}/settings`,
  74. {
  75. body: { rootDocId: option.value },
  76. }
  77. )
  78. ).to.be.true
  79. }
  80. })
  81. })