ProjectRootDocManagerTests.coffee 2.8 KB

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