LabelsHandlerTests.coffee 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 extract all the labels', ->
  32. docLabels = @LabelsHandler.extractLabelsFromDoc @lines
  33. expect(docLabels).to.deep.equal ['aaa', 'bbb']
  34. describe 'extractLabelsFromProjectDocs', ->
  35. beforeEach ->
  36. @docs = {
  37. 'doc_one': {
  38. _id: 'id_one',
  39. lines: ['one', '\\label{aaa} two', 'three']
  40. },
  41. 'doc_two': {
  42. _id: 'id_two',
  43. lines: ['four']
  44. },
  45. 'doc_three': {
  46. _id: 'id_three',
  47. lines: ['\\label{bbb}', 'five six', 'seven eight \\label{ccc} nine']
  48. }
  49. }
  50. it 'should extract all the labels', ->
  51. projectLabels = @LabelsHandler.extractLabelsFromProjectDocs @docs
  52. expect(projectLabels).to.deep.equal {
  53. 'id_one': ['aaa'],
  54. 'id_two': [],
  55. 'id_three': ['bbb', 'ccc']
  56. }
  57. describe 'getLabelsForDoc', ->
  58. beforeEach ->
  59. @fakeLines = ['one', '\\label{aaa}', 'two']
  60. @fakeLabels = ['aaa']
  61. @DocumentUpdaterHandler.flushDocToMongo = sinon.stub().callsArgWith(2, null)
  62. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @fakeLines)
  63. @LabelsHandler.extractLabelsFromDoc = sinon.stub().returns(@fakeLabels)
  64. @call = (callback) =>
  65. @LabelsHandler.getLabelsForDoc @projectId, @docId, callback
  66. it 'should not produce an error', (done) ->
  67. @call (err, docLabels) =>
  68. expect(err).to.equal null
  69. done()
  70. it 'should produce docLabels', (done) ->
  71. @call (err, docLabels) =>
  72. expect(docLabels).to.equal @fakeLabels
  73. done()
  74. it 'should call flushDocToMongo', (done) ->
  75. @call (err, docLabels) =>
  76. @DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal 1
  77. @DocumentUpdaterHandler.flushDocToMongo.calledWith(@projectId, @docId).should.equal true
  78. done()
  79. it 'should call getDoc', (done) ->
  80. @call (err, docLabels) =>
  81. @ProjectEntityHandler.getDoc.callCount.should.equal 1
  82. @ProjectEntityHandler.getDoc.calledWith(@projectId, @docId).should.equal true
  83. done()
  84. it 'should call extractLabelsFromDoc', (done) ->
  85. @call (err, docLabels) =>
  86. @LabelsHandler.extractLabelsFromDoc.callCount.should.equal 1
  87. @LabelsHandler.extractLabelsFromDoc.calledWith(@fakeLines).should.equal true
  88. done()
  89. describe 'getAllLabelsForProject', ->
  90. beforeEach ->
  91. @fakeDocs = {
  92. 'doc_one': {lines: ['\\label{aaa}']}
  93. }
  94. @fakeLabels = ['aaa']
  95. @DocumentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArgWith(1, null)
  96. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @fakeDocs)
  97. @LabelsHandler.extractLabelsFromProjectDocs = sinon.stub().returns(@fakeLabels)
  98. @call = (callback) =>
  99. @LabelsHandler.getAllLabelsForProject @projectId, callback
  100. it 'should not produce an error', (done) ->
  101. @call (err, projectLabels) =>
  102. expect(err).to.equal null
  103. done()
  104. it 'should produce projectLabels', (done) ->
  105. @call (err, projectLabels) =>
  106. expect(projectLabels).to.equal @fakeLabels
  107. done()
  108. it 'should call getAllDocs', (done) ->
  109. @call (err, projectLabels) =>
  110. @ProjectEntityHandler.getAllDocs.callCount.should.equal 1
  111. @ProjectEntityHandler.getAllDocs.calledWith(@projectId).should.equal true
  112. done()
  113. it 'should call extractLabelsFromDoc', (done) ->
  114. @call (err, docLabels) =>
  115. @LabelsHandler.extractLabelsFromProjectDocs.callCount.should.equal 1
  116. @LabelsHandler.extractLabelsFromProjectDocs.calledWith(@fakeDocs).should.equal true
  117. done()