add_comment_operation.test.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. it('should invert operation', function () {
  44. const fileData = new StringFileData('abc')
  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. resolved: false,
  52. },
  53. ])
  54. const invertedOp = op.invert()
  55. invertedOp.apply(fileData)
  56. expect(fileData.getComments().toRaw()).to.eql([])
  57. })
  58. it('should compose with DeleteCommentOperation', function () {
  59. const addOp = new AddCommentOperation('123', [new Range(0, 1)])
  60. const deleteOp = new DeleteCommentOperation('123')
  61. expect(addOp.canBeComposedWith(deleteOp)).to.be.true
  62. const composedOp = addOp.compose(deleteOp)
  63. expect(composedOp).to.be.instanceOf(DeleteCommentOperation)
  64. })
  65. })