ProjectEntityHandlerTests.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS205: Consider reworking code to avoid use of IIFEs
  13. * DS206: Consider reworking classes to avoid initClass
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. const { assert, expect } = require('chai')
  17. const sinon = require('sinon')
  18. const modulePath = '../../../../app/src/Features/Project/ProjectEntityHandler'
  19. const SandboxedModule = require('sandboxed-module')
  20. const Errors = require('../../../../app/src/Features/Errors/Errors')
  21. describe('ProjectEntityHandler', function() {
  22. const project_id = '4eecb1c1bffa66588e0000a1'
  23. const doc_id = '4eecb1c1bffa66588e0000a2'
  24. const folder_id = '4eecaffcbffa66588e000008'
  25. const rootFolderId = '4eecaffcbffa66588e000007'
  26. const userId = 1234
  27. beforeEach(function() {
  28. let Project
  29. this.TpdsUpdateSender = {
  30. addDoc: sinon.stub().callsArg(1),
  31. addFile: sinon.stub().callsArg(1)
  32. }
  33. this.ProjectModel = Project = (function() {
  34. Project = class Project {
  35. static initClass() {
  36. this.prototype.rootFolder = [this.rootFolder]
  37. }
  38. constructor(options) {
  39. this._id = project_id
  40. this.name = 'project_name_here'
  41. this.rev = 0
  42. }
  43. }
  44. Project.initClass()
  45. return Project
  46. })()
  47. this.project = new this.ProjectModel()
  48. this.ProjectLocator = { findElement: sinon.stub() }
  49. this.DocumentUpdaterHandler = {
  50. updateProjectStructure: sinon.stub().yields()
  51. }
  52. this.callback = sinon.stub()
  53. return (this.ProjectEntityHandler = SandboxedModule.require(modulePath, {
  54. requires: {
  55. '../Docstore/DocstoreManager': (this.DocstoreManager = {}),
  56. '../../Features/DocumentUpdater/DocumentUpdaterHandler': this
  57. .DocumentUpdaterHandler,
  58. '../../models/Project': {
  59. Project: this.ProjectModel
  60. },
  61. './ProjectLocator': this.ProjectLocator,
  62. './ProjectGetter': (this.ProjectGetter = {}),
  63. '../ThirdPartyDataStore/TpdsUpdateSender': this.TpdsUpdateSender
  64. }
  65. }))
  66. })
  67. describe('getting folders, docs and files', function() {
  68. beforeEach(function() {
  69. this.project.rootFolder = [
  70. {
  71. docs: [
  72. (this.doc1 = {
  73. name: 'doc1',
  74. _id: 'doc1_id'
  75. })
  76. ],
  77. fileRefs: [
  78. (this.file1 = {
  79. rev: 1,
  80. _id: 'file1_id',
  81. name: 'file1'
  82. })
  83. ],
  84. folders: [
  85. (this.folder1 = {
  86. name: 'folder1',
  87. docs: [
  88. (this.doc2 = {
  89. name: 'doc2',
  90. _id: 'doc2_id'
  91. })
  92. ],
  93. fileRefs: [
  94. (this.file2 = {
  95. rev: 2,
  96. name: 'file2',
  97. _id: 'file2_id'
  98. })
  99. ],
  100. folders: []
  101. })
  102. ]
  103. }
  104. ]
  105. this.ProjectGetter.getProjectWithoutDocLines = sinon
  106. .stub()
  107. .yields(null, this.project)
  108. })
  109. describe('getAllDocs', function() {
  110. beforeEach(function() {
  111. this.docs = [
  112. {
  113. _id: this.doc1._id,
  114. lines: (this.lines1 = ['one']),
  115. rev: (this.rev1 = 1)
  116. },
  117. {
  118. _id: this.doc2._id,
  119. lines: (this.lines2 = ['two']),
  120. rev: (this.rev2 = 2)
  121. }
  122. ]
  123. this.DocstoreManager.getAllDocs = sinon
  124. .stub()
  125. .callsArgWith(1, null, this.docs)
  126. this.ProjectEntityHandler.getAllDocs(project_id, this.callback)
  127. })
  128. it('should get the doc lines and rev from the docstore', function() {
  129. this.DocstoreManager.getAllDocs
  130. .calledWith(project_id)
  131. .should.equal(true)
  132. })
  133. it('should call the callback with the docs with the lines and rev included', function() {
  134. this.callback
  135. .calledWith(null, {
  136. '/doc1': {
  137. _id: this.doc1._id,
  138. lines: this.lines1,
  139. name: this.doc1.name,
  140. rev: this.rev1
  141. },
  142. '/folder1/doc2': {
  143. _id: this.doc2._id,
  144. lines: this.lines2,
  145. name: this.doc2.name,
  146. rev: this.rev2
  147. }
  148. })
  149. .should.equal(true)
  150. })
  151. })
  152. describe('getAllFiles', function() {
  153. beforeEach(function() {
  154. this.callback = sinon.stub()
  155. this.ProjectEntityHandler.getAllFiles(project_id, this.callback)
  156. })
  157. it('should call the callback with the files', function() {
  158. this.callback
  159. .calledWith(null, {
  160. '/file1': this.file1,
  161. '/folder1/file2': this.file2
  162. })
  163. .should.equal(true)
  164. })
  165. })
  166. describe('getAllDocPathsFromProject', function() {
  167. beforeEach(function() {
  168. this.docs = [
  169. {
  170. _id: this.doc1._id,
  171. lines: (this.lines1 = ['one']),
  172. rev: (this.rev1 = 1)
  173. },
  174. {
  175. _id: this.doc2._id,
  176. lines: (this.lines2 = ['two']),
  177. rev: (this.rev2 = 2)
  178. }
  179. ]
  180. this.callback = sinon.stub()
  181. this.ProjectEntityHandler.getAllDocPathsFromProject(
  182. this.project,
  183. this.callback
  184. )
  185. })
  186. it('should call the callback with the path for each doc_id', function() {
  187. this.expected = {}
  188. this.expected[this.doc1._id] = `/${this.doc1.name}`
  189. this.expected[this.doc2._id] = `/folder1/${this.doc2.name}`
  190. this.callback.calledWith(null, this.expected).should.equal(true)
  191. })
  192. })
  193. describe('getDocPathByProjectIdAndDocId', function() {
  194. beforeEach(function() {
  195. this.callback = sinon.stub()
  196. })
  197. it('should call the callback with the path for an existing doc id at the root level', function() {
  198. this.ProjectEntityHandler.getDocPathByProjectIdAndDocId(
  199. project_id,
  200. this.doc1._id,
  201. this.callback
  202. )
  203. this.callback.calledWith(null, `/${this.doc1.name}`).should.equal(true)
  204. })
  205. it('should call the callback with the path for an existing doc id nested within a folder', function() {
  206. this.ProjectEntityHandler.getDocPathByProjectIdAndDocId(
  207. project_id,
  208. this.doc2._id,
  209. this.callback
  210. )
  211. this.callback
  212. .calledWith(null, `/folder1/${this.doc2.name}`)
  213. .should.equal(true)
  214. })
  215. it('should call the callback with a NotFoundError for a non-existing doc', function() {
  216. this.ProjectEntityHandler.getDocPathByProjectIdAndDocId(
  217. project_id,
  218. 'non-existing-id',
  219. this.callback
  220. )
  221. expect(this.callback.firstCall.args[0]).to.be.an.instanceof(
  222. Errors.NotFoundError
  223. )
  224. })
  225. it('should call the callback with a NotFoundError for an existing file', function() {
  226. this.ProjectEntityHandler.getDocPathByProjectIdAndDocId(
  227. project_id,
  228. this.file1._id,
  229. this.callback
  230. )
  231. expect(this.callback.firstCall.args[0]).to.be.an.instanceof(
  232. Errors.NotFoundError
  233. )
  234. })
  235. })
  236. describe('_getAllFolders', function() {
  237. beforeEach(function() {
  238. this.callback = sinon.stub()
  239. this.ProjectEntityHandler._getAllFolders(project_id, this.callback)
  240. })
  241. it('should get the project without the docs lines', function() {
  242. this.ProjectGetter.getProjectWithoutDocLines
  243. .calledWith(project_id)
  244. .should.equal(true)
  245. })
  246. it('should call the callback with the folders', function() {
  247. this.callback
  248. .calledWith(null, {
  249. '/': this.project.rootFolder[0],
  250. '/folder1': this.folder1
  251. })
  252. .should.equal(true)
  253. })
  254. })
  255. describe('_getAllFoldersFromProject', function() {
  256. beforeEach(function() {
  257. this.callback = sinon.stub()
  258. this.ProjectEntityHandler._getAllFoldersFromProject(
  259. this.project,
  260. this.callback
  261. )
  262. })
  263. it('should call the callback with the folders', function() {
  264. this.callback
  265. .calledWith(null, {
  266. '/': this.project.rootFolder[0],
  267. '/folder1': this.folder1
  268. })
  269. .should.equal(true)
  270. })
  271. })
  272. })
  273. describe('getDoc', function() {
  274. beforeEach(function() {
  275. this.lines = ['mock', 'doc', 'lines']
  276. this.rev = 5
  277. this.version = 42
  278. this.ranges = { mock: 'ranges' }
  279. this.DocstoreManager.getDoc = sinon
  280. .stub()
  281. .callsArgWith(3, null, this.lines, this.rev, this.version, this.ranges)
  282. this.ProjectEntityHandler.getDoc(project_id, doc_id, this.callback)
  283. })
  284. it('should call the docstore', function() {
  285. this.DocstoreManager.getDoc
  286. .calledWith(project_id, doc_id)
  287. .should.equal(true)
  288. })
  289. it('should call the callback with the lines, version and rev', function() {
  290. this.callback
  291. .calledWith(null, this.lines, this.rev, this.version, this.ranges)
  292. .should.equal(true)
  293. })
  294. })
  295. describe('promises.getDoc', function() {
  296. let result
  297. beforeEach(async function() {
  298. this.lines = ['mock', 'doc', 'lines']
  299. this.rev = 5
  300. this.version = 42
  301. this.ranges = { mock: 'ranges' }
  302. this.DocstoreManager.getDoc = sinon
  303. .stub()
  304. .callsArgWith(3, null, this.lines, this.rev, this.version, this.ranges)
  305. result = await this.ProjectEntityHandler.promises.getDoc(
  306. project_id,
  307. doc_id
  308. )
  309. })
  310. it('should call the docstore', function() {
  311. this.DocstoreManager.getDoc
  312. .calledWith(project_id, doc_id)
  313. .should.equal(true)
  314. })
  315. it('should return the lines, rev, version and ranges', function() {
  316. expect(result.lines).to.equal(this.lines)
  317. expect(result.rev).to.equal(this.rev)
  318. expect(result.version).to.equal(this.version)
  319. expect(result.ranges).to.equal(this.ranges)
  320. })
  321. })
  322. })