add_comment_operation.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // @ts-check
  2. const { expect } = require('chai')
  3. const { AddCommentOperation, DeleteCommentOperation } = require('..')
  4. const Range = require('../lib/range')
  5. const StringFileData = require('../lib/file_data/string_file_data')
  6. describe('AddCommentOperation', function () {
  7. it('constructs an AddCommentOperation fromJSON', function () {
  8. const op = AddCommentOperation.fromJSON({
  9. commentId: '123',
  10. resolved: true,
  11. ranges: [{ pos: 0, length: 1 }],
  12. })
  13. expect(op).to.be.instanceOf(AddCommentOperation)
  14. expect(op.commentId).to.equal('123')
  15. expect(op.ranges[0]).to.be.instanceOf(Range)
  16. expect(op.resolved).to.be.true
  17. })
  18. it('should convert to JSON', function () {
  19. const op = new AddCommentOperation('123', [new Range(0, 1)])
  20. expect(op.toJSON()).to.eql({
  21. commentId: '123',
  22. ranges: [
  23. {
  24. pos: 0,
  25. length: 1,
  26. },
  27. ],
  28. })
  29. })
  30. it('should apply operation', function () {
  31. const fileData = new StringFileData('abc')
  32. const op = new AddCommentOperation('123', [new Range(0, 1)])
  33. op.apply(fileData)
  34. expect(fileData.getComments().toRaw()).to.eql([
  35. {
  36. id: '123',
  37. ranges: [{ pos: 0, length: 1 }],
  38. },
  39. ])
  40. })
  41. describe('invert', function () {
  42. it('should delete added comment', function () {
  43. const initialFileData = new StringFileData('abc')
  44. const fileData = StringFileData.fromRaw(initialFileData.toRaw())
  45. const op = new AddCommentOperation('123', [new Range(0, 1)])
  46. op.apply(fileData)
  47. expect(fileData.getComments().toRaw()).to.eql([
  48. {
  49. id: '123',
  50. ranges: [{ pos: 0, length: 1 }],
  51. },
  52. ])
  53. const invertedOp = op.invert(initialFileData)
  54. invertedOp.apply(fileData)
  55. expect(fileData.getComments().toRaw()).to.eql([])
  56. })
  57. it('should restore previous comment ranges', function () {
  58. const initialComments = [
  59. {
  60. id: '123',
  61. ranges: [{ pos: 0, length: 1 }],
  62. },
  63. ]
  64. const initialFileData = new StringFileData(
  65. 'the quick brown fox jumps over the lazy dog',
  66. initialComments
  67. )
  68. const fileData = StringFileData.fromRaw(initialFileData.toRaw())
  69. const op = new AddCommentOperation('123', [new Range(12, 7)], true)
  70. op.apply(fileData)
  71. expect(fileData.getComments().toRaw()).to.eql([
  72. {
  73. id: '123',
  74. ranges: [{ pos: 12, length: 7 }],
  75. resolved: true,
  76. },
  77. ])
  78. const invertedOp = op.invert(initialFileData)
  79. invertedOp.apply(fileData)
  80. expect(fileData.getComments().toRaw()).to.deep.equal(initialComments)
  81. })
  82. it('should restore previous comment resolution status', function () {
  83. const initialComments = [
  84. {
  85. id: '123',
  86. ranges: [{ pos: 0, length: 1 }],
  87. },
  88. ]
  89. const initialFileData = new StringFileData(
  90. 'the quick brown fox jumps over the lazy dog',
  91. initialComments
  92. )
  93. const fileData = StringFileData.fromRaw(initialFileData.toRaw())
  94. const op = new AddCommentOperation('123', [new Range(0, 1)], true)
  95. op.apply(fileData)
  96. expect(fileData.getComments().toRaw()).to.eql([
  97. {
  98. id: '123',
  99. ranges: [{ pos: 0, length: 1 }],
  100. resolved: true,
  101. },
  102. ])
  103. const invertedOp = op.invert(initialFileData)
  104. invertedOp.apply(fileData)
  105. expect(fileData.getComments().toRaw()).to.deep.equal(initialComments)
  106. })
  107. })
  108. it('should compose with DeleteCommentOperation', function () {
  109. const addOp = new AddCommentOperation('123', [new Range(0, 1)])
  110. const deleteOp = new DeleteCommentOperation('123')
  111. expect(addOp.canBeComposedWith(deleteOp)).to.be.true
  112. const composedOp = addOp.compose(deleteOp)
  113. expect(composedOp).to.be.instanceOf(DeleteCommentOperation)
  114. })
  115. })