LabelsHandler.Tests.coffee 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. chai = require('chai')
  2. chai.should()
  3. expect = chai.expect
  4. sinon = require("sinon")
  5. modulePath = "../../../../app/js/Features/Labels/LabelsHandler"
  6. SandboxedModule = require('sandboxed-module')
  7. describe 'LabelsHandler', ->
  8. beforeEach ->
  9. @projectId = 'someprojectid'
  10. @docId = 'somedocid'
  11. @ProjectEntityHandler = {
  12. getAllDocs: sinon.stub()
  13. getDoc: sinon.stub()
  14. }
  15. @DocumentUpdaterHandler = {
  16. flushDocToMongo: sinon.stub()
  17. }
  18. @LabelsHandler = SandboxedModule.require modulePath, requires:
  19. '../Project/ProjectEntityHandler': @ProjectEntityHandler
  20. '../DocumentUpdater/DocumentUpdaterHandler': @DocumentUpdaterHandler
  21. describe 'extractLabelsFromDoc', ->
  22. beforeEach ->
  23. @lines = [
  24. 'one',
  25. 'two',
  26. 'three \\label{aaa}',
  27. 'four five',
  28. '\\label{bbb}',
  29. 'six seven'
  30. ]
  31. it 'should not produce an error', (done) ->
  32. @LabelsHandler.extractLabelsFromDoc @lines, (err, docLabels) ->
  33. expect(err).to.equal null
  34. done()
  35. it 'should extract all the labels', (done) ->
  36. @LabelsHandler.extractLabelsFromDoc @lines, (err, docLabels) ->
  37. expect(docLabels).to.deep.equal ['aaa', 'bbb']
  38. done()
  39. describe 'extractLabelsFromProjectDocs', ->
  40. beforeEach ->
  41. @docs = {
  42. 'doc_one': {
  43. _id: 'id_one',
  44. lines: ['one', '\\label{aaa} two', 'three']
  45. },
  46. 'doc_two': {
  47. _id: 'id_two',
  48. lines: ['four']
  49. },
  50. 'doc_three': {
  51. _id: 'id_three',
  52. lines: ['\\label{bbb}', 'five six', 'seven eight \\label{ccc} nine']
  53. }
  54. }
  55. it 'should not produce an error', (done) ->
  56. @LabelsHandler.extractLabelsFromProjectDocs @docs, (err, projectLabels) ->
  57. expect(err).to.equal null
  58. done()
  59. it 'should extract all the labels', (done) ->
  60. @LabelsHandler.extractLabelsFromProjectDocs @docs, (err, projectLabels) ->
  61. expect(projectLabels).to.deep.equal {
  62. 'id_one': ['aaa'],
  63. 'id_two': [],
  64. 'id_three': ['bbb', 'ccc']
  65. }
  66. done()
  67. describe 'getLabelsForDoc', ->
  68. beforeEach ->
  69. @fakeLines = ['one', '\\label{aaa}', 'two']
  70. @fakeLabels = ['aaa']
  71. @DocumentUpdaterHandler.flushDocToMongo = sinon.stub().callsArgWith(2, null)
  72. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @fakeLines)
  73. @LabelsHandler.extractLabelsFromDoc = sinon.stub().callsArgWith(1, null, @fakeLabels)
  74. @call = (callback) =>
  75. @LabelsHandler.getLabelsForDoc @projectId, @docId, callback
  76. it 'should not produce an error', (done) ->
  77. @call (err, docLabels) =>
  78. expect(err).to.equal null
  79. done()
  80. it 'should produce docLabels', (done) ->
  81. @call (err, docLabels) =>
  82. expect(docLabels).to.equal @fakeLabels
  83. done()
  84. it 'should call flushDocToMongo', (done) ->
  85. @call (err, docLabels) =>
  86. @DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal 1
  87. @DocumentUpdaterHandler.flushDocToMongo.calledWith(@projectId, @docId).should.equal true
  88. done()
  89. it 'should call getDoc', (done) ->
  90. @call (err, docLabels) =>
  91. @ProjectEntityHandler.getDoc.callCount.should.equal 1
  92. @ProjectEntityHandler.getDoc.calledWith(@projectId, @docId).should.equal true
  93. done()
  94. it 'should call extractLabelsFromDoc', (done) ->
  95. @call (err, docLabels) =>
  96. @LabelsHandler.extractLabelsFromDoc.callCount.should.equal 1
  97. @LabelsHandler.extractLabelsFromDoc.calledWith(@fakeLines).should.equal true
  98. done()
  99. describe 'getAllLabelsForProject', ->
  100. beforeEach ->
  101. @fakeDocs = {
  102. 'doc_one': {lines: ['\\label{aaa}']}
  103. }
  104. @fakeLabels = ['aaa']
  105. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @fakeDocs)
  106. @LabelsHandler.extractLabelsFromProjectDocs = sinon.stub().callsArgWith(1, null, @fakeLabels)
  107. @call = (callback) =>
  108. @LabelsHandler.getAllLabelsForProject @projectId, callback
  109. it 'should not produce an error', (done) ->
  110. @call (err, projectLabels) =>
  111. expect(err).to.equal null
  112. done()
  113. it 'should produce projectLabels', (done) ->
  114. @call (err, projectLabels) =>
  115. expect(projectLabels).to.equal @fakeLabels
  116. done()
  117. it 'should call getAllDocs', (done) ->
  118. @call (err, projectLabels) =>
  119. @ProjectEntityHandler.getAllDocs.callCount.should.equal 1
  120. @ProjectEntityHandler.getAllDocs.calledWith(@projectId).should.equal true
  121. done()
  122. it 'should call extractLabelsFromDoc', (done) ->
  123. @call (err, docLabels) =>
  124. @LabelsHandler.extractLabelsFromProjectDocs.callCount.should.equal 1
  125. @LabelsHandler.extractLabelsFromProjectDocs.calledWith(@fakeDocs).should.equal true
  126. done()