create-folder.test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import { expect } from 'chai'
  2. import React from 'react'
  3. import sinon from 'sinon'
  4. import { screen, render, fireEvent } from '@testing-library/react'
  5. import fetchMock from 'fetch-mock'
  6. import MockedSocket from 'socket.io-mock'
  7. import FileTreeRoot from '../../../../../frontend/js/features/file-tree/components/file-tree-root'
  8. describe('FileTree Create Folder Flow', function() {
  9. const onSelect = sinon.stub()
  10. const onInit = sinon.stub()
  11. beforeEach(function() {
  12. global.requestAnimationFrame = sinon.stub()
  13. window._ide = {
  14. socket: new MockedSocket()
  15. }
  16. })
  17. afterEach(function() {
  18. delete global.requestAnimationFrame
  19. fetchMock.restore()
  20. onSelect.reset()
  21. onInit.reset()
  22. delete window._ide
  23. })
  24. it('add to root', async function() {
  25. const rootFolder = [
  26. {
  27. _id: 'root-folder-id',
  28. docs: [{ _id: '456def', name: 'main.tex' }],
  29. folders: [],
  30. fileRefs: []
  31. }
  32. ]
  33. render(
  34. <FileTreeRoot
  35. rootFolder={rootFolder}
  36. projectId="123abc"
  37. hasWritePermissions
  38. rootDocId="456def"
  39. onSelect={onSelect}
  40. onInit={onInit}
  41. />
  42. )
  43. const newFolderName = 'Foo Bar In Root'
  44. const matcher = /\/project\/\w+\/folder/
  45. const response = {
  46. folders: [],
  47. fileRefs: [],
  48. docs: [],
  49. _id: fakeId(),
  50. name: newFolderName
  51. }
  52. fetchMock.post(matcher, response)
  53. fireCreateFolder(newFolderName)
  54. const lastCallBody = JSON.parse(fetchMock.lastCall(matcher)[1].body)
  55. expect(lastCallBody.name).to.equal(newFolderName)
  56. expect(lastCallBody.parent_folder_id).to.equal('root-folder-id')
  57. window._ide.socket.socketClient.emit('reciveNewFolder', 'root-folder-id', {
  58. _id: fakeId(),
  59. name: newFolderName,
  60. docs: [],
  61. fileRefs: [],
  62. folders: []
  63. })
  64. await screen.findByRole('treeitem', { name: newFolderName })
  65. })
  66. it('add to folder from folder', async function() {
  67. const rootFolder = [
  68. {
  69. _id: 'root-folder-id',
  70. docs: [],
  71. folders: [
  72. {
  73. _id: '789ghi',
  74. name: 'thefolder',
  75. docs: [],
  76. fileRefs: [],
  77. folders: []
  78. }
  79. ],
  80. fileRefs: []
  81. }
  82. ]
  83. render(
  84. <FileTreeRoot
  85. rootFolder={rootFolder}
  86. projectId="123abc"
  87. hasWritePermissions
  88. rootDocId="789ghi"
  89. onSelect={onSelect}
  90. onInit={onInit}
  91. />
  92. )
  93. const expandButton = screen.getByRole('button', { name: 'Expand' })
  94. fireEvent.click(expandButton)
  95. const newFolderName = 'Foo Bar In thefolder'
  96. const matcher = /\/project\/\w+\/folder/
  97. const response = {
  98. folders: [],
  99. fileRefs: [],
  100. docs: [],
  101. _id: fakeId(),
  102. name: newFolderName
  103. }
  104. fetchMock.post(matcher, response)
  105. fireCreateFolder(newFolderName)
  106. const lastCallBody = JSON.parse(fetchMock.lastCall(matcher)[1].body)
  107. expect(lastCallBody.name).to.equal(newFolderName)
  108. expect(lastCallBody.parent_folder_id).to.equal('789ghi')
  109. window._ide.socket.socketClient.emit('reciveNewFolder', '789ghi', {
  110. _id: fakeId(),
  111. name: newFolderName,
  112. docs: [],
  113. fileRefs: [],
  114. folders: []
  115. })
  116. // find the created folder
  117. await screen.findByRole('treeitem', { name: newFolderName })
  118. // collapse the parent folder; created folder should not be rendered anymore
  119. fireEvent.click(expandButton)
  120. expect(screen.queryByRole('treeitem', { name: newFolderName })).to.not.exist
  121. })
  122. it('add to folder from child', async function() {
  123. const rootFolder = [
  124. {
  125. _id: 'root-folder-id',
  126. docs: [],
  127. folders: [
  128. {
  129. _id: '789ghi',
  130. name: 'thefolder',
  131. docs: [],
  132. fileRefs: [{ _id: '456def', name: 'sub.tex' }],
  133. folders: []
  134. }
  135. ],
  136. fileRefs: []
  137. }
  138. ]
  139. render(
  140. <FileTreeRoot
  141. rootFolder={rootFolder}
  142. projectId="123abc"
  143. hasWritePermissions
  144. rootDocId="456def"
  145. onSelect={onSelect}
  146. onInit={onInit}
  147. />
  148. )
  149. const expandButton = screen.getByRole('button', { name: 'Expand' })
  150. fireEvent.click(expandButton)
  151. const newFolderName = 'Foo Bar In thefolder'
  152. const matcher = /\/project\/\w+\/folder/
  153. const response = {
  154. folders: [],
  155. fileRefs: [],
  156. docs: [],
  157. _id: fakeId(),
  158. name: newFolderName
  159. }
  160. fetchMock.post(matcher, response)
  161. fireCreateFolder(newFolderName)
  162. const lastCallBody = JSON.parse(fetchMock.lastCall(matcher)[1].body)
  163. expect(lastCallBody.name).to.equal(newFolderName)
  164. expect(lastCallBody.parent_folder_id).to.equal('789ghi')
  165. window._ide.socket.socketClient.emit('reciveNewFolder', '789ghi', {
  166. _id: fakeId(),
  167. name: newFolderName,
  168. docs: [],
  169. fileRefs: [],
  170. folders: []
  171. })
  172. // find the created folder
  173. await screen.findByRole('treeitem', { name: newFolderName })
  174. // collapse the parent folder; created folder should not be rendered anymore
  175. fireEvent.click(expandButton)
  176. expect(screen.queryByRole('treeitem', { name: newFolderName })).to.not.exist
  177. })
  178. it('prevents adding duplicate or invalid names', async function() {
  179. const rootFolder = [
  180. {
  181. _id: 'root-folder-id',
  182. docs: [{ _id: '456def', name: 'existingFile' }],
  183. folders: [],
  184. fileRefs: []
  185. }
  186. ]
  187. render(
  188. <FileTreeRoot
  189. rootFolder={rootFolder}
  190. projectId="123abc"
  191. hasWritePermissions
  192. rootDocId="456def"
  193. onSelect={onSelect}
  194. onInit={onInit}
  195. />
  196. )
  197. var newFolderName = 'existingFile'
  198. fireCreateFolder(newFolderName)
  199. expect(fetchMock.called()).to.be.false
  200. await screen.findByRole('alert', {
  201. name: 'A file or folder with this name already exists',
  202. hidden: true
  203. })
  204. newFolderName = 'in/valid '
  205. setFolderName(newFolderName)
  206. await screen.findByRole('alert', {
  207. name: 'File name is empty or contains invalid characters',
  208. hidden: true
  209. })
  210. })
  211. function fireCreateFolder(name) {
  212. const createFolderButton = screen.getByRole('button', {
  213. name: 'New Folder'
  214. })
  215. fireEvent.click(createFolderButton)
  216. setFolderName(name)
  217. const modalCreateButton = getModalCreateButton()
  218. fireEvent.click(modalCreateButton)
  219. }
  220. function setFolderName(name) {
  221. const input = screen.getByRole('textbox', {
  222. hidden: true // FIXME: modal should not be hidden but it has the aria-hidden label due to a react-bootstrap bug
  223. })
  224. fireEvent.change(input, { target: { value: name } })
  225. }
  226. function fakeId() {
  227. return Math.random()
  228. .toString(16)
  229. .replace(/0\./, 'random-test-id-')
  230. }
  231. function getModalCreateButton() {
  232. return screen.getAllByRole('button', {
  233. name: 'Create',
  234. hidden: true // FIXME: modal should not be hidden but it has the aria-hidden label due to a react-bootstrap bug
  235. })[0] // the first matched button is the toolbar button
  236. }
  237. })