add_comment_operation.test.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // @ts-check
  2. const { expect } = require('chai')
  3. const { AddCommentOperation, DeleteCommentOperation } = require('..')
  4. const Comment = require('../lib/comment')
  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.comment).to.be.instanceOf(Comment)
  16. expect(op.comment.resolved).to.be.true
  17. })
  18. it('should convert to JSON', function () {
  19. const op = new AddCommentOperation(
  20. '123',
  21. Comment.fromRaw({
  22. ranges: [
  23. {
  24. pos: 0,
  25. length: 1,
  26. },
  27. ],
  28. })
  29. )
  30. expect(op.toJSON()).to.eql({
  31. commentId: '123',
  32. resolved: false,
  33. ranges: [
  34. {
  35. pos: 0,
  36. length: 1,
  37. },
  38. ],
  39. })
  40. })
  41. it('should apply operation', function () {
  42. const fileData = new StringFileData('abc')
  43. const op = new AddCommentOperation(
  44. '123',
  45. Comment.fromRaw({ ranges: [{ pos: 0, length: 1 }] })
  46. )
  47. op.apply(fileData)
  48. expect(fileData.getComments()).to.eql([
  49. {
  50. id: '123',
  51. ranges: [{ pos: 0, length: 1 }],
  52. resolved: false,
  53. },
  54. ])
  55. })
  56. it('should invert operation', function () {
  57. const fileData = new StringFileData('abc')
  58. const op = new AddCommentOperation(
  59. '123',
  60. Comment.fromRaw({ ranges: [{ pos: 0, length: 1 }] })
  61. )
  62. op.apply(fileData)
  63. expect(fileData.getComments()).to.eql([
  64. {
  65. id: '123',
  66. ranges: [{ pos: 0, length: 1 }],
  67. resolved: false,
  68. },
  69. ])
  70. const invertedOp = op.invert()
  71. invertedOp.apply(fileData)
  72. expect(fileData.getComments()).to.eql([])
  73. })
  74. it('should compose with DeleteCommentOperation', function () {
  75. const addOp = new AddCommentOperation(
  76. '123',
  77. Comment.fromRaw({ ranges: [{ pos: 0, length: 1 }] })
  78. )
  79. const deleteOp = new DeleteCommentOperation('123')
  80. expect(addOp.canBeComposedWith(deleteOp)).to.be.true
  81. const composedOp = addOp.compose(deleteOp)
  82. expect(composedOp).to.be.instanceOf(DeleteCommentOperation)
  83. })
  84. })