FolderStructureBuilderTests.js 3.6 KB

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