getDocVersionInMongoTests.coffee 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/PersistenceManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. Errors = require "../../../../app/js/Errors"
  7. {ObjectId} = require("mongojs")
  8. describe "PersistenceManager.getDocVersionInMongo", ->
  9. beforeEach ->
  10. @PersistenceManager = SandboxedModule.require modulePath, requires:
  11. "request": @request = sinon.stub()
  12. "settings-sharelatex": @Settings = {}
  13. "./Metrics": @Metrics =
  14. Timer: class Timer
  15. done: sinon.stub()
  16. "./mongojs":
  17. db: @db = { docOps: {} }
  18. ObjectId: ObjectId
  19. @doc_id = ObjectId().toString()
  20. @callback = sinon.stub()
  21. describe "getDocVersionInMongo", ->
  22. describe "when the doc exists", ->
  23. beforeEach ->
  24. @doc =
  25. version: @version = 42
  26. @db.docOps.find = sinon.stub().callsArgWith(2, null, [@doc])
  27. @PersistenceManager.getDocVersionInMongo @doc_id, @callback
  28. it "should look for the doc in the database", ->
  29. @db.docOps.find
  30. .calledWith({ doc_id: ObjectId(@doc_id) }, {version: 1})
  31. .should.equal true
  32. it "should call the callback with the version", ->
  33. @callback.calledWith(null, @version).should.equal true
  34. describe "when the doc doesn't exist", ->
  35. beforeEach ->
  36. @db.docOps.find = sinon.stub().callsArgWith(2, null, [])
  37. @PersistenceManager.getDocVersionInMongo @doc_id, @callback
  38. it "should call the callback with 0", ->
  39. @callback.calledWith(null, 0).should.equal true