delete_comment_operation.test.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. const Range = require('../lib/range')
  7. describe('DeleteCommentOperation', function () {
  8. it('constructs an DeleteCommentOperation fromJSON', function () {
  9. const op = DeleteCommentOperation.fromJSON({
  10. deleteComment: '123',
  11. })
  12. expect(op).to.be.instanceOf(DeleteCommentOperation)
  13. })
  14. it('should convert to JSON', function () {
  15. const op = new DeleteCommentOperation('123')
  16. expect(op.toJSON()).to.eql({
  17. deleteComment: '123',
  18. })
  19. })
  20. it('should apply operation', function () {
  21. const fileData = new StringFileData('abc')
  22. const op = new DeleteCommentOperation('123')
  23. fileData.comments.add('123', new Comment([new Range(0, 1)]))
  24. op.apply(fileData)
  25. expect(fileData.getComments()).to.eql([])
  26. })
  27. it('should invert operation', function () {
  28. const fileData = new StringFileData('abc')
  29. const op = new DeleteCommentOperation('123')
  30. fileData.comments.add('123', new Comment([new Range(0, 1)]))
  31. const invertedOp = /** @type {InstanceType<AddCommentOperation>} */ (
  32. op.invert(fileData)
  33. )
  34. expect(invertedOp).to.be.instanceOf(AddCommentOperation)
  35. expect(invertedOp.commentId).to.equal('123')
  36. expect(invertedOp.comment).to.be.instanceOf(Comment)
  37. expect(invertedOp.comment.ranges).to.eql([new Range(0, 1)])
  38. })
  39. it('should not throw if comment not found', function () {
  40. const fileData = new StringFileData('abc')
  41. const op = new DeleteCommentOperation('123')
  42. expect(() => op.invert(fileData)).to.not.throw()
  43. })
  44. })