path.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { expect } from 'chai'
  2. import { Folder } from '../../../../../types/folder'
  3. import { docId } from '../../source-editor/helpers/mock-doc'
  4. import {
  5. findEntityByPath,
  6. pathInFolder,
  7. previewByPath,
  8. } from '@/features/file-tree/util/path'
  9. describe('Path utils', function () {
  10. let rootFolder: Folder
  11. beforeEach(function () {
  12. rootFolder = {
  13. _id: 'root-folder-id',
  14. name: 'rootFolder',
  15. docs: [
  16. {
  17. _id: docId,
  18. name: 'main.tex',
  19. },
  20. ],
  21. folders: [
  22. {
  23. _id: 'test-folder-id',
  24. name: 'test-folder',
  25. docs: [
  26. {
  27. _id: 'test-doc-in-folder',
  28. name: 'example.tex',
  29. },
  30. ],
  31. fileRefs: [
  32. {
  33. _id: 'test-file-in-folder',
  34. name: 'example.png',
  35. hash: '42',
  36. },
  37. ],
  38. folders: [
  39. {
  40. _id: 'test-subfolder-id',
  41. name: 'test-subfolder',
  42. docs: [
  43. {
  44. _id: 'test-doc-in-subfolder',
  45. name: 'nested-example.tex',
  46. },
  47. ],
  48. fileRefs: [
  49. {
  50. _id: 'test-file-in-subfolder',
  51. name: 'nested-example.png',
  52. hash: '43',
  53. },
  54. ],
  55. folders: [],
  56. },
  57. ],
  58. },
  59. ],
  60. fileRefs: [
  61. {
  62. _id: 'test-image-file',
  63. name: 'frog.jpg',
  64. hash: '21',
  65. },
  66. {
  67. _id: 'uppercase-extension-image-file',
  68. name: 'frog.JPG',
  69. hash: '22',
  70. },
  71. {
  72. _id: 'test-svg-file',
  73. name: 'diagram.svg',
  74. hash: '44',
  75. },
  76. ],
  77. }
  78. })
  79. describe('pathInFolder', function () {
  80. it('gets null path for non-existent entity', function () {
  81. const retrieved = pathInFolder(rootFolder, 'non-existent.tex')
  82. expect(retrieved).to.be.null
  83. })
  84. it('gets correct path for document in the root', function () {
  85. const retrieved = pathInFolder(rootFolder, docId)
  86. expect(retrieved).to.equal('main.tex')
  87. })
  88. it('gets correct path for document in a folder', function () {
  89. const retrieved = pathInFolder(rootFolder, 'test-doc-in-folder')
  90. expect(retrieved).to.equal('test-folder/example.tex')
  91. })
  92. it('gets correct path for document in a nested folder', function () {
  93. const retrieved = pathInFolder(rootFolder, 'test-doc-in-subfolder')
  94. expect(retrieved).to.equal(
  95. 'test-folder/test-subfolder/nested-example.tex'
  96. )
  97. })
  98. it('gets correct path for file in a nested folder', function () {
  99. const retrieved = pathInFolder(rootFolder, 'test-file-in-subfolder')
  100. expect(retrieved).to.equal(
  101. 'test-folder/test-subfolder/nested-example.png'
  102. )
  103. })
  104. it('gets correct path for file in a nested folder relative to folder', function () {
  105. const retrieved = pathInFolder(
  106. rootFolder.folders[0],
  107. 'test-file-in-subfolder'
  108. )
  109. expect(retrieved).to.equal('test-subfolder/nested-example.png')
  110. })
  111. })
  112. describe('findEntityByPath', function () {
  113. it('returns null for a non-existent path', function () {
  114. const retrieved = findEntityByPath(rootFolder, 'not-a-real-document.tex')
  115. expect(retrieved).to.be.null
  116. })
  117. it('finds a document in the root', function () {
  118. const retrieved = findEntityByPath(rootFolder, 'main.tex')
  119. expect(retrieved?.entity._id).to.equal(docId)
  120. })
  121. it('finds a document in a folder', function () {
  122. const retrieved = findEntityByPath(rootFolder, 'test-folder/example.tex')
  123. expect(retrieved?.entity._id).to.equal('test-doc-in-folder')
  124. })
  125. it('finds a document in a nested folder', function () {
  126. const retrieved = findEntityByPath(
  127. rootFolder,
  128. 'test-folder/test-subfolder/nested-example.tex'
  129. )
  130. expect(retrieved?.entity._id).to.equal('test-doc-in-subfolder')
  131. })
  132. it('finds a file in a nested folder', function () {
  133. const retrieved = findEntityByPath(
  134. rootFolder,
  135. 'test-folder/test-subfolder/nested-example.png'
  136. )
  137. expect(retrieved?.entity._id).to.equal('test-file-in-subfolder')
  138. })
  139. })
  140. describe('previewByPath', function () {
  141. it('returns extension without preceding dot', function () {
  142. const preview = previewByPath(
  143. rootFolder,
  144. 'test-project-id',
  145. 'test-folder/example.png'
  146. )
  147. expect(preview).to.deep.equal({
  148. url: '/project/test-project-id/blob/42',
  149. extension: 'png',
  150. })
  151. })
  152. it('returns SVG files with correct extension', function () {
  153. const preview = previewByPath(
  154. rootFolder,
  155. 'test-project-id',
  156. 'diagram.svg'
  157. )
  158. expect(preview).to.deep.equal({
  159. url: '/project/test-project-id/blob/44',
  160. extension: 'svg',
  161. })
  162. })
  163. })
  164. })