FolderStructureBuilderTests.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const { expect } = require('chai')
  2. const SandboxedModule = require('sandboxed-module')
  3. const { ObjectId } = require('mongodb-legacy')
  4. const sinon = require('sinon')
  5. const MODULE_PATH =
  6. '../../../../app/src/Features/Project/FolderStructureBuilder'
  7. describe('FolderStructureBuilder', function () {
  8. beforeEach(function () {
  9. this.FolderStructureBuilder = SandboxedModule.require(MODULE_PATH, {
  10. requires: { 'mongodb-legacy': { ObjectId } },
  11. })
  12. })
  13. describe('buildFolderStructure', function () {
  14. describe('when given no documents at all', function () {
  15. beforeEach(function () {
  16. this.result = this.FolderStructureBuilder.buildFolderStructure([], [])
  17. })
  18. it('returns an empty root folder', function () {
  19. sinon.assert.match(this.result, {
  20. _id: sinon.match.instanceOf(ObjectId),
  21. name: 'rootFolder',
  22. folders: [],
  23. docs: [],
  24. fileRefs: [],
  25. })
  26. })
  27. })
  28. describe('when given documents and files', function () {
  29. beforeEach(function () {
  30. const docUploads = [
  31. { path: '/main.tex', doc: { _id: 'doc-1', name: 'main.tex' } },
  32. { path: '/foo/other.tex', doc: { _id: 'doc-2', name: 'other.tex' } },
  33. { path: '/foo/other.bib', doc: { _id: 'doc-3', name: 'other.bib' } },
  34. {
  35. path: '/foo/foo1/foo2/another.tex',
  36. doc: { _id: 'doc-4', name: 'another.tex' },
  37. },
  38. ]
  39. const fileUploads = [
  40. { path: '/aaa.jpg', file: { _id: 'file-1', name: 'aaa.jpg' } },
  41. { path: '/foo/bbb.jpg', file: { _id: 'file-2', name: 'bbb.jpg' } },
  42. { path: '/bar/ccc.jpg', file: { _id: 'file-3', name: 'ccc.jpg' } },
  43. ]
  44. this.result = this.FolderStructureBuilder.buildFolderStructure(
  45. docUploads,
  46. fileUploads
  47. )
  48. })
  49. it('returns a full folder structure', function () {
  50. sinon.assert.match(this.result, {
  51. _id: sinon.match.instanceOf(ObjectId),
  52. name: 'rootFolder',
  53. docs: [{ _id: 'doc-1', name: 'main.tex' }],
  54. fileRefs: [{ _id: 'file-1', name: 'aaa.jpg' }],
  55. folders: [
  56. {
  57. _id: sinon.match.instanceOf(ObjectId),
  58. name: 'foo',
  59. docs: [
  60. { _id: 'doc-2', name: 'other.tex' },
  61. { _id: 'doc-3', name: 'other.bib' },
  62. ],
  63. fileRefs: [{ _id: 'file-2', name: 'bbb.jpg' }],
  64. folders: [
  65. {
  66. _id: sinon.match.instanceOf(ObjectId),
  67. name: 'foo1',
  68. docs: [],
  69. fileRefs: [],
  70. folders: [
  71. {
  72. _id: sinon.match.instanceOf(ObjectId),
  73. name: 'foo2',
  74. docs: [{ _id: 'doc-4', name: 'another.tex' }],
  75. fileRefs: [],
  76. folders: [],
  77. },
  78. ],
  79. },
  80. ],
  81. },
  82. {
  83. _id: sinon.match.instanceOf(ObjectId),
  84. name: 'bar',
  85. docs: [],
  86. fileRefs: [{ _id: 'file-3', name: 'ccc.jpg' }],
  87. folders: [],
  88. },
  89. ],
  90. })
  91. })
  92. })
  93. describe('when given duplicate files', function () {
  94. it('throws an error', function () {
  95. const docUploads = [
  96. { path: '/foo/doc.tex', doc: { _id: 'doc-1', name: 'doc.tex' } },
  97. { path: '/foo/doc.tex', doc: { _id: 'doc-2', name: 'doc.tex' } },
  98. ]
  99. expect(() =>
  100. this.FolderStructureBuilder.buildFolderStructure(docUploads, [])
  101. ).to.throw()
  102. })
  103. })
  104. })
  105. })