RangeManagerTests.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. expect = require('chai').expect
  5. modulePath = require('path').join __dirname, '../../../app/js/RangeManager'
  6. ObjectId = require("mongojs").ObjectId
  7. assert = require("chai").assert
  8. _ = require "underscore"
  9. describe "RangeManager", ->
  10. beforeEach ->
  11. @RangeManager = SandboxedModule.require modulePath, requires:
  12. "./mongojs":
  13. ObjectId: ObjectId
  14. describe "jsonRangesToMongo", ->
  15. it "should convert ObjectIds and dates to proper objects", ->
  16. change_id = ObjectId().toString()
  17. comment_id = ObjectId().toString()
  18. user_id = ObjectId().toString()
  19. thread_id = ObjectId().toString()
  20. ts = new Date().toJSON()
  21. @RangeManager.jsonRangesToMongo({
  22. changes: [{
  23. id: change_id
  24. op: { i: "foo", p: 3 }
  25. metadata:
  26. user_id: user_id
  27. ts: ts
  28. }]
  29. comments: [{
  30. id: comment_id
  31. op: { c: "foo", p: 3, t: thread_id }
  32. }]
  33. }).should.deep.equal {
  34. changes: [{
  35. id: ObjectId(change_id)
  36. op: { i: "foo", p: 3 }
  37. metadata:
  38. user_id: ObjectId(user_id)
  39. ts: new Date(ts)
  40. }]
  41. comments: [{
  42. id: ObjectId(comment_id)
  43. op: { c: "foo", p: 3, t: ObjectId(thread_id) }
  44. }]
  45. }
  46. it "should leave malformed ObjectIds as they are", ->
  47. change_id = "foo"
  48. comment_id = "bar"
  49. user_id = "baz"
  50. @RangeManager.jsonRangesToMongo({
  51. changes: [{
  52. id: change_id
  53. metadata:
  54. user_id: user_id
  55. }]
  56. comments: [{
  57. id: comment_id
  58. }]
  59. }).should.deep.equal {
  60. changes: [{
  61. id: change_id
  62. metadata:
  63. user_id: user_id
  64. }]
  65. comments: [{
  66. id: comment_id
  67. }]
  68. }
  69. it "should be consistent when transformed through json -> mongo -> json", ->
  70. change_id = ObjectId().toString()
  71. comment_id = ObjectId().toString()
  72. user_id = ObjectId().toString()
  73. thread_id = ObjectId().toString()
  74. ts = new Date().toJSON()
  75. ranges1 = {
  76. changes: [{
  77. id: change_id
  78. op: { i: "foo", p: 3 }
  79. metadata:
  80. user_id: user_id
  81. ts: ts
  82. }]
  83. comments: [{
  84. id: comment_id
  85. op: { c: "foo", p: 3, t: thread_id }
  86. }]
  87. }
  88. ranges1_copy = JSON.parse(JSON.stringify(ranges1)) # jsonRangesToMongo modifies in place
  89. ranges2 = JSON.parse(JSON.stringify(@RangeManager.jsonRangesToMongo(ranges1_copy)))
  90. ranges1.should.deep.equal ranges2
  91. describe "shouldUpdateRanges", ->
  92. beforeEach () ->
  93. @ranges = {
  94. changes: [{
  95. id: ObjectId()
  96. op: { i: "foo", p: 3 }
  97. metadata:
  98. user_id: ObjectId()
  99. ts: new Date()
  100. }]
  101. comments: [{
  102. id: ObjectId()
  103. op: { c: "foo", p: 3, t: ObjectId() }
  104. }]
  105. }
  106. @ranges_copy = @RangeManager.jsonRangesToMongo(JSON.parse(JSON.stringify(@ranges)))
  107. describe "with a blank new range", ->
  108. it "should throw an error", ->
  109. expect(() =>
  110. @RangeManager.shouldUpdateRanges(@ranges, null)
  111. ).to.throw(Error)
  112. describe "with a blank old range", ->
  113. it "should treat it like {}", ->
  114. @RangeManager.shouldUpdateRanges(null, {}).should.equal false
  115. @RangeManager.shouldUpdateRanges(null, @ranges).should.equal true
  116. describe "with no changes", ->
  117. it "should return false", ->
  118. @RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal false
  119. describe "with changes", ->
  120. it "should return true when the change id changes", ->
  121. @ranges_copy.changes[0].id = ObjectId()
  122. @RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal true
  123. it "should return true when the change user id changes", ->
  124. @ranges_copy.changes[0].metadata.user_id = ObjectId()
  125. @RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal true
  126. it "should return true when the change ts changes", ->
  127. @ranges_copy.changes[0].metadata.ts = new Date(Date.now() + 1000)
  128. @RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal true
  129. it "should return true when the change op changes", ->
  130. @ranges_copy.changes[0].op.i = "bar"
  131. @RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal true
  132. it "should return true when the comment id changes", ->
  133. @ranges_copy.comments[0].id = ObjectId()
  134. @RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal true
  135. it "should return true when the comment offset changes", ->
  136. @ranges_copy.comments[0].op.p = 17
  137. @RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal true
  138. it "should return true when the comment content changes", ->
  139. @ranges_copy.comments[0].op.c = "bar"
  140. @RangeManager.shouldUpdateRanges(@ranges, @ranges_copy).should.equal true