ProjectRootDocManagerTests.coffee 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. "./ProjectEntityUpdateHandler" : @ProjectEntityUpdateHandler = {}
  14. describe "setRootDocAutomatically", ->
  15. describe "when there is a suitable root doc", ->
  16. beforeEach (done)->
  17. @docs =
  18. "/chapter1.tex":
  19. _id: "doc-id-1"
  20. lines: ["something else","\\begin{document}", "Hello world", "\\end{document}"]
  21. "/main.tex":
  22. _id: "doc-id-2"
  23. lines: ["different line","\\documentclass{article}", "\\input{chapter1}"]
  24. "/nested/chapter1a.tex":
  25. _id: "doc-id-3"
  26. lines: ["Hello world"]
  27. "/nested/chapter1b.tex":
  28. _id: "doc-id-4"
  29. lines: ["Hello world"]
  30. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  31. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  32. @ProjectRootDocManager.setRootDocAutomatically @project_id, done
  33. it "should check the docs of the project", ->
  34. @ProjectEntityHandler.getAllDocs.calledWith(@project_id)
  35. .should.equal true
  36. it "should set the root doc to the doc containing a documentclass", ->
  37. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  38. .should.equal true
  39. describe "when the root doc is an Rtex file", ->
  40. beforeEach ->
  41. @docs =
  42. "/chapter1.tex":
  43. _id: "doc-id-1"
  44. lines: ["\\begin{document}", "Hello world", "\\end{document}"]
  45. "/main.Rtex":
  46. _id: "doc-id-2"
  47. lines: ["\\documentclass{article}", "\\input{chapter1}"]
  48. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  49. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  50. @ProjectRootDocManager.setRootDocAutomatically @project_id, @callback
  51. it "should set the root doc to the doc containing a documentclass", ->
  52. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  53. .should.equal true
  54. describe "when there is no suitable root doc", ->
  55. beforeEach (done)->
  56. @docs =
  57. "/chapter1.tex":
  58. _id: "doc-id-1"
  59. lines: ["\\begin{document}", "Hello world", "\\end{document}"]
  60. "/style.bst":
  61. _id: "doc-id-2"
  62. lines: ["%Example: \\documentclass{article}"]
  63. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  64. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  65. @ProjectRootDocManager.setRootDocAutomatically @project_id, done
  66. it "should not set the root doc to the doc containing a documentclass", ->
  67. @ProjectEntityUpdateHandler.setRootDoc.called.should.equal false
  68. describe "setRootDocFromName", ->
  69. describe "when there is a suitable root doc", ->
  70. beforeEach (done)->
  71. @docPaths =
  72. "doc-id-1": "/chapter1.tex"
  73. "doc-id-2": "/main.tex"
  74. "doc-id-3": "/nested/chapter1a.tex"
  75. "doc-id-4": "/nested/chapter1b.tex"
  76. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  77. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  78. @ProjectRootDocManager.setRootDocFromName @project_id, '/main.tex', done
  79. it "should check the docs of the project", ->
  80. @ProjectEntityHandler.getAllDocPathsFromProjectById.calledWith(@project_id)
  81. .should.equal true
  82. it "should set the root doc to main.tex", ->
  83. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  84. .should.equal true
  85. describe "when there is a suitable root doc but the leading slash is missing", ->
  86. beforeEach (done)->
  87. @docPaths =
  88. "doc-id-1": "/chapter1.tex"
  89. "doc-id-2": "/main.tex"
  90. "doc-id-3": "/nested/chapter1a.tex"
  91. "doc-id-4": "/nested/chapter1b.tex"
  92. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  93. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  94. @ProjectRootDocManager.setRootDocFromName @project_id, 'main.tex', done
  95. it "should check the docs of the project", ->
  96. @ProjectEntityHandler.getAllDocPathsFromProjectById.calledWith(@project_id)
  97. .should.equal true
  98. it "should set the root doc to main.tex", ->
  99. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  100. .should.equal true
  101. describe "when there is a suitable root doc with a basename match", ->
  102. beforeEach (done)->
  103. @docPaths =
  104. "doc-id-1": "/chapter1.tex"
  105. "doc-id-2": "/main.tex"
  106. "doc-id-3": "/nested/chapter1a.tex"
  107. "doc-id-4": "/nested/chapter1b.tex"
  108. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  109. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  110. @ProjectRootDocManager.setRootDocFromName @project_id, 'chapter1a.tex', done
  111. it "should check the docs of the project", ->
  112. @ProjectEntityHandler.getAllDocPathsFromProjectById.calledWith(@project_id)
  113. .should.equal true
  114. it "should set the root doc using the basename", ->
  115. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-3")
  116. .should.equal true
  117. describe "when there is no suitable root doc", ->
  118. beforeEach (done)->
  119. @docPaths =
  120. "doc-id-1": "/chapter1.tex"
  121. "doc-id-2": "/main.tex"
  122. "doc-id-3": "/nested/chapter1a.tex"
  123. "doc-id-4": "/nested/chapter1b.tex"
  124. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  125. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  126. @ProjectRootDocManager.setRootDocFromName @project_id, "other.tex", done
  127. it "should not set the root doc", ->
  128. @ProjectEntityUpdateHandler.setRootDoc.called.should.equal false