path.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. }
  73. })
  74. describe('pathInFolder', function () {
  75. it('gets null path for non-existent entity', function () {
  76. const retrieved = pathInFolder(rootFolder, 'non-existent.tex')
  77. expect(retrieved).to.be.null
  78. })
  79. it('gets correct path for document in the root', function () {
  80. const retrieved = pathInFolder(rootFolder, docId)
  81. expect(retrieved).to.equal('main.tex')
  82. })
  83. it('gets correct path for document in a folder', function () {
  84. const retrieved = pathInFolder(rootFolder, 'test-doc-in-folder')
  85. expect(retrieved).to.equal('test-folder/example.tex')
  86. })
  87. it('gets correct path for document in a nested folder', function () {
  88. const retrieved = pathInFolder(rootFolder, 'test-doc-in-subfolder')
  89. expect(retrieved).to.equal(
  90. 'test-folder/test-subfolder/nested-example.tex'
  91. )
  92. })
  93. it('gets correct path for file in a nested folder', function () {
  94. const retrieved = pathInFolder(rootFolder, 'test-file-in-subfolder')
  95. expect(retrieved).to.equal(
  96. 'test-folder/test-subfolder/nested-example.png'
  97. )
  98. })
  99. it('gets correct path for file in a nested folder relative to folder', function () {
  100. const retrieved = pathInFolder(
  101. rootFolder.folders[0],
  102. 'test-file-in-subfolder'
  103. )
  104. expect(retrieved).to.equal('test-subfolder/nested-example.png')
  105. })
  106. })
  107. describe('findEntityByPath', function () {
  108. it('returns null for a non-existent path', function () {
  109. const retrieved = findEntityByPath(rootFolder, 'not-a-real-document.tex')
  110. expect(retrieved).to.be.null
  111. })
  112. it('finds a document in the root', function () {
  113. const retrieved = findEntityByPath(rootFolder, 'main.tex')
  114. expect(retrieved?.entity._id).to.equal(docId)
  115. })
  116. it('finds a document in a folder', function () {
  117. const retrieved = findEntityByPath(rootFolder, 'test-folder/example.tex')
  118. expect(retrieved?.entity._id).to.equal('test-doc-in-folder')
  119. })
  120. it('finds a document in a nested folder', function () {
  121. const retrieved = findEntityByPath(
  122. rootFolder,
  123. 'test-folder/test-subfolder/nested-example.tex'
  124. )
  125. expect(retrieved?.entity._id).to.equal('test-doc-in-subfolder')
  126. })
  127. it('finds a file in a nested folder', function () {
  128. const retrieved = findEntityByPath(
  129. rootFolder,
  130. 'test-folder/test-subfolder/nested-example.png'
  131. )
  132. expect(retrieved?.entity._id).to.equal('test-file-in-subfolder')
  133. })
  134. })
  135. describe('previewByPath', function () {
  136. it('returns extension without preceding dot', function () {
  137. const preview = previewByPath(
  138. rootFolder,
  139. 'test-project-id',
  140. 'test-folder/example.png'
  141. )
  142. expect(preview).to.deep.equal({
  143. url: '/project/test-project-id/blob/42',
  144. extension: 'png',
  145. })
  146. })
  147. })
  148. })