overleaf/services/web/test/UnitTests/coffee/Documents/DocumentControllerTests.coffee

93 lines
2.8 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
sinon = require('sinon')
chai = require('chai')
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/Features/Documents/DocumentController.js"
SandboxedModule = require('sandboxed-module')
events = require "events"
MockRequest = require "../helpers/MockRequest"
MockResponse = require "../helpers/MockResponse"
Errors = require "../../../../app/js/errors"
describe "DocumentController", ->
beforeEach ->
2014-05-08 08:42:30 -04:00
@DocumentController = SandboxedModule.require modulePath, requires:
2014-02-12 05:23:40 -05:00
"../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
@res = new MockResponse()
@req = new MockRequest()
@next = sinon.stub()
@project_id = "project-id-123"
@doc_id = "doc-id-123"
@doc_lines = ["one", "two", "three"]
2014-05-08 08:42:30 -04:00
@version = 42
@rev = 5
2014-02-12 05:23:40 -05:00
describe "getDocument", ->
beforeEach ->
@req.params =
Project_id: @project_id
doc_id: @doc_id
describe "when the document exists", ->
beforeEach ->
2014-05-08 08:42:30 -04:00
@ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @doc_lines, @version, @rev)
2014-02-12 05:23:40 -05:00
@DocumentController.getDocument(@req, @res, @next)
it "should get the document from Mongo", ->
2014-05-08 08:42:30 -04:00
@ProjectEntityHandler.getDoc
.calledWith(@project_id, @doc_id)
2014-02-12 05:23:40 -05:00
.should.equal true
it "should return the document data to the client as JSON", ->
@res.type.should.equal "json"
@res.body.should.equal JSON.stringify
lines: @doc_lines
2014-05-08 08:42:30 -04:00
version: @version
2014-02-12 05:23:40 -05:00
describe "when the document doesn't exist", ->
beforeEach ->
2014-05-08 08:42:30 -04:00
@ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, new Errors.NotFoundError("not found"), null)
2014-02-12 05:23:40 -05:00
@DocumentController.getDocument(@req, @res, @next)
it "should call next with the NotFoundError", ->
@next.calledWith(new Errors.NotFoundError("not found"))
.should.equal true
describe "setDocument", ->
beforeEach ->
@req.params =
Project_id: @project_id
doc_id: @doc_id
describe "when the document exists", ->
beforeEach ->
2014-05-08 10:47:50 -04:00
@ProjectEntityHandler.updateDocLines = sinon.stub().callsArg(4)
2014-02-12 05:23:40 -05:00
@req.body =
lines: @doc_lines
2014-05-08 10:47:50 -04:00
version: @version
2014-02-12 05:23:40 -05:00
@DocumentController.setDocument(@req, @res, @next)
it "should update the document in Mongo", ->
@ProjectEntityHandler.updateDocLines
2014-05-08 10:47:50 -04:00
.calledWith(@project_id, @doc_id, @doc_lines, @version)
2014-02-12 05:23:40 -05:00
.should.equal true
it "should return a successful response", ->
@res.success.should.equal true
describe "when the document doesn't exist", ->
beforeEach ->
2014-05-08 10:47:50 -04:00
@ProjectEntityHandler.updateDocLines = sinon.stub().callsArgWith(4, new Errors.NotFoundError("document does not exist"))
2014-02-12 05:23:40 -05:00
@req.body =
lines: @doc_lines
2014-05-08 10:47:50 -04:00
version: @version
2014-02-12 05:23:40 -05:00
@DocumentController.setDocument(@req, @res, @next)
it "should call next with the NotFoundError", ->
@next.calledWith(new Errors.NotFoundError("not found"))
.should.equal true