file-tree-modal-create-file.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. import { expect } from 'chai'
  2. import React, { useEffect } from 'react'
  3. import {
  4. screen,
  5. render,
  6. fireEvent,
  7. cleanup,
  8. waitFor
  9. } from '@testing-library/react'
  10. import fetchMock from 'fetch-mock'
  11. import PropTypes from 'prop-types'
  12. import { contextProps } from './context-props'
  13. import FileTreeModalCreateFile from '../../../../../../frontend/js/features/file-tree/components/modals/file-tree-modal-create-file'
  14. import FileTreeContext from '../../../../../../frontend/js/features/file-tree/components/file-tree-context'
  15. import { useFileTreeActionable } from '../../../../../../frontend/js/features/file-tree/contexts/file-tree-actionable'
  16. import { useFileTreeMutable } from '../../../../../../frontend/js/features/file-tree/contexts/file-tree-mutable'
  17. describe('<FileTreeModalCreateFile/>', function() {
  18. afterEach(function() {
  19. fetchMock.restore()
  20. cleanup()
  21. })
  22. it('handles invalid file names', async function() {
  23. render(
  24. <FileTreeContext {...contextProps}>
  25. <OpenWithMode mode="doc" />
  26. </FileTreeContext>
  27. )
  28. const submitButton = screen.getByRole('button', { name: 'Create' })
  29. const input = screen.getByLabelText('File Name')
  30. expect(input.value).to.equal('name.tex')
  31. expect(submitButton.disabled).to.be.false
  32. expect(screen.queryAllByRole('alert')).to.be.empty
  33. fireEvent.change(input, { target: { value: '' } })
  34. expect(submitButton.disabled).to.be.true
  35. screen.getByRole(
  36. (role, element) =>
  37. role === 'alert' && element.textContent.match(/File name is empty/)
  38. )
  39. await fireEvent.change(input, { target: { value: 'test.tex' } })
  40. expect(submitButton.disabled).to.be.false
  41. expect(screen.queryAllByRole('alert')).to.be.empty
  42. await fireEvent.change(input, { target: { value: 'oops/i/did/it/again' } })
  43. expect(submitButton.disabled).to.be.true
  44. screen.getByRole(
  45. (role, element) =>
  46. role === 'alert' &&
  47. element.textContent.match(/contains invalid characters/)
  48. )
  49. })
  50. it('displays an error when the file limit is reached', async function() {
  51. const rootFolder = [
  52. {
  53. docs: Array.from({ length: 10 }, (_, index) => ({
  54. _id: `entity-${index}`
  55. })),
  56. fileRefs: [],
  57. folders: []
  58. }
  59. ]
  60. render(
  61. <FileTreeContext
  62. {...contextProps}
  63. rootFolder={rootFolder}
  64. initialSelectedEntityId="entity-1"
  65. >
  66. <OpenWithMode mode="doc" />
  67. </FileTreeContext>
  68. )
  69. screen.getByRole(
  70. (role, element) =>
  71. role === 'alert' &&
  72. element.textContent.match(/This project has reached the \d+ file limit/)
  73. )
  74. })
  75. it('displays a warning when the file limit is nearly reached', async function() {
  76. const rootFolder = [
  77. {
  78. docs: Array.from({ length: 9 }, (_, index) => ({
  79. _id: `entity-${index}`
  80. })),
  81. fileRefs: [],
  82. folders: []
  83. }
  84. ]
  85. render(
  86. <FileTreeContext
  87. {...contextProps}
  88. rootFolder={rootFolder}
  89. initialSelectedEntityId="entity-1"
  90. >
  91. <OpenWithMode mode="doc" />
  92. </FileTreeContext>
  93. )
  94. screen.getByText(/This project is approaching the file limit \(\d+\/\d+\)/)
  95. })
  96. it('counts files in nested folders', async function() {
  97. const rootFolder = [
  98. {
  99. docs: [{ _id: 'entity-1' }],
  100. fileRefs: [],
  101. folders: [
  102. {
  103. docs: [{ _id: 'entity-1-2' }],
  104. fileRefs: [],
  105. folders: [
  106. {
  107. docs: [
  108. { _id: 'entity-3' },
  109. { _id: 'entity-4' },
  110. { _id: 'entity-5' },
  111. { _id: 'entity-6' },
  112. { _id: 'entity-7' },
  113. { _id: 'entity-8' },
  114. { _id: 'entity-9' }
  115. ],
  116. fileRefs: [],
  117. folders: []
  118. }
  119. ]
  120. }
  121. ]
  122. }
  123. ]
  124. render(
  125. <FileTreeContext
  126. {...contextProps}
  127. rootFolder={rootFolder}
  128. initialSelectedEntityId="entity-1"
  129. >
  130. <OpenWithMode mode="doc" />
  131. </FileTreeContext>
  132. )
  133. screen.getByText(/This project is approaching the file limit \(\d+\/\d+\)/)
  134. })
  135. it('creates a new file when the form is submitted', async function() {
  136. fetchMock.post('express:/project/:projectId/doc', () => 204)
  137. render(
  138. <FileTreeContext {...contextProps}>
  139. <OpenWithMode mode="doc" />
  140. </FileTreeContext>
  141. )
  142. const input = screen.getByLabelText('File Name')
  143. await fireEvent.change(input, { target: { value: 'test.tex' } })
  144. const submitButton = screen.getByRole('button', { name: 'Create' })
  145. await fireEvent.click(submitButton)
  146. expect(
  147. fetchMock.called('express:/project/:projectId/doc', {
  148. body: { name: 'test.tex' }
  149. })
  150. ).to.be.true
  151. })
  152. it('imports a new file from a project', async function() {
  153. fetchMock
  154. .get('path:/user/projects', {
  155. projects: [
  156. {
  157. _id: 'test-project',
  158. name: 'This Project'
  159. },
  160. {
  161. _id: 'project-1',
  162. name: 'Project One'
  163. },
  164. {
  165. _id: 'project-2',
  166. name: 'Project Two'
  167. }
  168. ]
  169. })
  170. .get('express:/project/:projectId/entities', {
  171. entities: [
  172. {
  173. path: '/foo.tex'
  174. },
  175. {
  176. path: '/bar.tex'
  177. }
  178. ]
  179. })
  180. .post('express:/project/:projectId/compile', {
  181. status: 'success',
  182. outputFiles: [
  183. {
  184. build: 'test',
  185. path: 'baz.jpg'
  186. },
  187. {
  188. build: 'test',
  189. path: 'ball.jpg'
  190. }
  191. ]
  192. })
  193. .post('express:/project/:projectId/linked_file', () => 204)
  194. render(
  195. <FileTreeContext {...contextProps}>
  196. <OpenWithMode mode="project" />
  197. </FileTreeContext>
  198. )
  199. // initial state, no project selected
  200. const projectInput = screen.getByLabelText('Select a Project')
  201. expect(projectInput.disabled).to.be.true
  202. await waitFor(() => {
  203. expect(projectInput.disabled).to.be.false
  204. })
  205. // the submit button should be disabled
  206. const submitButton = screen.getByRole('button', { name: 'Create' })
  207. expect(submitButton.disabled).to.be.true
  208. // the source file selector should be disabled
  209. const fileInput = screen.getByLabelText('Select a File')
  210. expect(fileInput.disabled).to.be.true
  211. // TODO: check for options length, excluding current project
  212. // select a project
  213. await fireEvent.change(projectInput, { target: { value: 'project-2' } }) // TODO: getByRole('option')?
  214. // wait for the source file selector to be enabled
  215. await waitFor(() => {
  216. expect(fileInput.disabled).to.be.false
  217. })
  218. expect(screen.queryByLabelText('Select a File')).not.to.be.null
  219. expect(screen.queryByLabelText('Select an Output File')).to.be.null
  220. expect(submitButton.disabled).to.be.true
  221. // TODO: check for fileInput options length, excluding current project
  222. // click on the button to toggle between source and output files
  223. const sourceTypeButton = screen.getByRole('button', {
  224. name: 'select from output files'
  225. })
  226. await fireEvent.click(sourceTypeButton)
  227. // wait for the output file selector to be enabled
  228. const entityInput = screen.getByLabelText('Select an Output File')
  229. await waitFor(() => {
  230. expect(entityInput.disabled).to.be.false
  231. })
  232. expect(screen.queryByLabelText('Select a File')).to.be.null
  233. expect(screen.queryByLabelText('Select an Output File')).not.to.be.null
  234. expect(submitButton.disabled).to.be.true
  235. // TODO: check for entityInput options length, excluding current project
  236. await fireEvent.change(entityInput, { target: { value: 'ball.jpg' } }) // TODO: getByRole('option')?
  237. await waitFor(() => {
  238. expect(submitButton.disabled).to.be.false
  239. })
  240. await fireEvent.click(submitButton)
  241. expect(
  242. fetchMock.called('express:/project/:projectId/linked_file', {
  243. body: {
  244. name: 'ball.jpg',
  245. provider: 'project_output_file',
  246. data: {
  247. source_project_id: 'project-2',
  248. source_output_file_path: 'ball.jpg',
  249. build_id: 'test'
  250. }
  251. }
  252. })
  253. ).to.be.true
  254. })
  255. it('import from a URL when the form is submitted', async function() {
  256. fetchMock.post('express:/project/:projectId/linked_file', () => 204)
  257. render(
  258. <FileTreeContext {...contextProps}>
  259. <OpenWithMode mode="url" />
  260. </FileTreeContext>
  261. )
  262. const urlInput = screen.getByLabelText('URL to fetch the file from')
  263. const nameInput = screen.getByLabelText('File Name In This Project')
  264. await fireEvent.change(urlInput, {
  265. target: { value: 'https://example.com/example.tex' }
  266. })
  267. // check that the name has updated automatically
  268. expect(nameInput.value).to.equal('example.tex')
  269. await fireEvent.change(nameInput, {
  270. target: { value: 'test.tex' }
  271. })
  272. // check that the name can still be edited manually
  273. expect(nameInput.value).to.equal('test.tex')
  274. const submitButton = screen.getByRole('button', { name: 'Create' })
  275. await fireEvent.click(submitButton)
  276. expect(
  277. fetchMock.called('express:/project/:projectId/linked_file', {
  278. body: {
  279. name: 'test.tex',
  280. provider: 'url',
  281. data: { url: 'https://example.com/example.tex' }
  282. }
  283. })
  284. ).to.be.true
  285. })
  286. // eslint-disable-next-line mocha/no-skipped-tests
  287. it.skip('uploads a new file', async function() {
  288. fetchMock.post('express:/project/:projectId/linked_file', () => 204)
  289. render(
  290. <FileTreeContext {...contextProps}>
  291. <OpenWithMode mode="upload" />
  292. </FileTreeContext>
  293. )
  294. // the submit button should not be present
  295. expect(screen.queryByRole('button', { name: 'Create' })).to.be.null
  296. const dropzone = screen.getByLabelText('File Uploader')
  297. expect(dropzone).not.to.be.null
  298. // https://github.com/jsdom/jsdom/issues/1568 - no paste
  299. fireEvent.drop(dropzone, {
  300. dataTransfer: {
  301. files: [new File(['test'], 'test.tex', { type: 'text/plain' })]
  302. }
  303. })
  304. expect(fetchMock.called('express:/project/:projectId/upload')).to.be.true
  305. })
  306. })
  307. function OpenWithMode({ mode }) {
  308. const { newFileCreateMode, startCreatingFile } = useFileTreeActionable()
  309. const { fileCount } = useFileTreeMutable()
  310. // eslint-disable-next-line react-hooks/exhaustive-deps
  311. useEffect(() => startCreatingFile(mode), [])
  312. if (!fileCount || !newFileCreateMode) {
  313. return null
  314. }
  315. return <FileTreeModalCreateFile />
  316. }
  317. OpenWithMode.propTypes = {
  318. mode: PropTypes.string.isRequired
  319. }