add_comment_operation.test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. resolved: false,
  23. ranges: [
  24. {
  25. pos: 0,
  26. length: 1,
  27. },
  28. ],
  29. })
  30. })
  31. it('should apply operation', function () {
  32. const fileData = new StringFileData('abc')
  33. const op = new AddCommentOperation('123', [new Range(0, 1)])
  34. op.apply(fileData)
  35. expect(fileData.getComments().toRaw()).to.eql([
  36. {
  37. id: '123',
  38. ranges: [{ pos: 0, length: 1 }],
  39. resolved: false,
  40. },
  41. ])
  42. })
  43. describe('invert', function () {
  44. it('should delete added comment', function () {
  45. const initialFileData = new StringFileData('abc')
  46. const fileData = StringFileData.fromRaw(initialFileData.toRaw())
  47. const op = new AddCommentOperation('123', [new Range(0, 1)])
  48. op.apply(fileData)
  49. expect(fileData.getComments().toRaw()).to.eql([
  50. {
  51. id: '123',
  52. ranges: [{ pos: 0, length: 1 }],
  53. resolved: false,
  54. },
  55. ])
  56. const invertedOp = op.invert(initialFileData)
  57. invertedOp.apply(fileData)
  58. expect(fileData.getComments().toRaw()).to.eql([])
  59. })
  60. it('should restore previous comment ranges', function () {
  61. const initialComments = [
  62. {
  63. id: '123',
  64. ranges: [{ pos: 0, length: 1 }],
  65. resolved: false,
  66. },
  67. ]
  68. const initialFileData = new StringFileData(
  69. 'the quick brown fox jumps over the lazy dog',
  70. initialComments
  71. )
  72. const fileData = StringFileData.fromRaw(initialFileData.toRaw())
  73. const op = new AddCommentOperation('123', [new Range(12, 7)], true)
  74. op.apply(fileData)
  75. expect(fileData.getComments().toRaw()).to.eql([
  76. {
  77. id: '123',
  78. ranges: [{ pos: 12, length: 7 }],
  79. resolved: true,
  80. },
  81. ])
  82. const invertedOp = op.invert(initialFileData)
  83. invertedOp.apply(fileData)
  84. expect(fileData.getComments().toRaw()).to.deep.equal(initialComments)
  85. })
  86. it('should restore previous comment resolution status', function () {
  87. const initialComments = [
  88. {
  89. id: '123',
  90. ranges: [{ pos: 0, length: 1 }],
  91. resolved: false,
  92. },
  93. ]
  94. const initialFileData = new StringFileData(
  95. 'the quick brown fox jumps over the lazy dog',
  96. initialComments
  97. )
  98. const fileData = StringFileData.fromRaw(initialFileData.toRaw())
  99. const op = new AddCommentOperation('123', [new Range(0, 1)], true)
  100. op.apply(fileData)
  101. expect(fileData.getComments().toRaw()).to.eql([
  102. {
  103. id: '123',
  104. ranges: [{ pos: 0, length: 1 }],
  105. resolved: true,
  106. },
  107. ])
  108. const invertedOp = op.invert(initialFileData)
  109. invertedOp.apply(fileData)
  110. expect(fileData.getComments().toRaw()).to.deep.equal(initialComments)
  111. })
  112. })
  113. it('should compose with DeleteCommentOperation', function () {
  114. const addOp = new AddCommentOperation('123', [new Range(0, 1)])
  115. const deleteOp = new DeleteCommentOperation('123')
  116. expect(addOp.canBeComposedWith(deleteOp)).to.be.true
  117. const composedOp = addOp.compose(deleteOp)
  118. expect(composedOp).to.be.instanceOf(DeleteCommentOperation)
  119. })
  120. })