ProjectRootDocManagerTests.coffee 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. chai = require('chai')
  2. should = chai.should()
  3. sinon = require("sinon")
  4. modulePath = "../../../../app/js/Features/Project/ProjectRootDocManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe 'ProjectRootDocManager', ->
  7. beforeEach ->
  8. @project_id = "project-123"
  9. @sl_req_id = "sl-req-id-123"
  10. @ProjectRootDocManager = SandboxedModule.require modulePath, requires:
  11. "./ProjectEntityHandler" : @ProjectEntityHandler = {}
  12. describe "setRootDocAutomatically", ->
  13. describe "when there is a suitable root doc", ->
  14. beforeEach ->
  15. @docs =
  16. "/chapter1.tex":
  17. _id: "doc-id-1"
  18. lines: ["\\begin{document}", "Hello world", "\\end{document}"]
  19. "/main.tex":
  20. _id: "doc-id-2"
  21. lines: ["\\documentclass{article}", "\\input{chapter1}"]
  22. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(2, null, @docs)
  23. @ProjectEntityHandler.setRootDoc = sinon.stub().callsArgWith(3)
  24. @ProjectRootDocManager.setRootDocAutomatically @project_id, @sl_req_id, @callback
  25. it "should check the docs of the project", ->
  26. @ProjectEntityHandler.getAllDocs.calledWith(@project_id)
  27. .should.equal true
  28. it "should set the root doc to the doc containing a documentclass", ->
  29. @ProjectEntityHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  30. .should.equal true
  31. it "should call the callback", ->
  32. @callback.called.should.equal true
  33. it "should pass on the sl_req_id", ->
  34. @ProjectEntityHandler.getAllDocs.calledWith(sinon.match.any, @sl_req_id)
  35. .should.equal true
  36. @ProjectEntityHandler.setRootDoc.calledWith(sinon.match.any, sinon.match.any, @sl_req_id)
  37. .should.equal true
  38. describe "when the root doc is an Rtex file", ->
  39. beforeEach ->
  40. @docs =
  41. "/chapter1.tex":
  42. _id: "doc-id-1"
  43. lines: ["\\begin{document}", "Hello world", "\\end{document}"]
  44. "/main.Rtex":
  45. _id: "doc-id-2"
  46. lines: ["\\documentclass{article}", "\\input{chapter1}"]
  47. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(2, null, @docs)
  48. @ProjectEntityHandler.setRootDoc = sinon.stub().callsArgWith(3)
  49. @ProjectRootDocManager.setRootDocAutomatically @project_id, @sl_req_id, @callback
  50. it "should set the root doc to the doc containing a documentclass", ->
  51. @ProjectEntityHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  52. .should.equal true
  53. describe "when there is no suitable root doc", ->
  54. beforeEach ->
  55. @docs =
  56. "/chapter1.tex":
  57. _id: "doc-id-1"
  58. lines: ["\\begin{document}", "Hello world", "\\end{document}"]
  59. "/style.bst":
  60. _id: "doc-id-2"
  61. lines: ["%Example: \\documentclass{article}"]
  62. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(2, null, @docs)
  63. @ProjectEntityHandler.setRootDoc = sinon.stub().callsArgWith(3)
  64. @ProjectRootDocManager.setRootDocAutomatically @project_id, @callback
  65. it "should not set the root doc to the doc containing a documentclass", ->
  66. @ProjectEntityHandler.setRootDoc.called.should.equal false
  67. it "should call the callback", ->
  68. @callback.called.should.equal true