string_file_data.test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // @ts-check
  2. 'use strict'
  3. const { expect } = require('chai')
  4. const _ = require('lodash')
  5. const ot = require('..')
  6. const StringFileData = require('../lib/file_data/string_file_data')
  7. const TextOperation = ot.TextOperation
  8. describe('StringFileData', function () {
  9. it('throws when it contains non BMP chars', function () {
  10. const content = '𝌆𝌆𝌆'
  11. const fileData = new StringFileData(content)
  12. const operation = new TextOperation()
  13. operation.insert('aa')
  14. expect(() => {
  15. fileData.edit(operation)
  16. }).to.throw(TextOperation.ApplyError, /string contains non BMP characters/)
  17. })
  18. it('validates string length when edited', function () {
  19. const longString = _.repeat('a', TextOperation.MAX_STRING_LENGTH)
  20. const fileData = new StringFileData(longString)
  21. expect(fileData.getByteLength()).to.equal(longString.length)
  22. expect(fileData.getStringLength()).to.equal(longString.length)
  23. expect(() => {
  24. fileData.edit(new TextOperation().retain(longString.length).insert('x'))
  25. }).to.throw(TextOperation.TooLongError)
  26. expect(fileData.getByteLength()).to.equal(longString.length)
  27. expect(fileData.getStringLength()).to.equal(longString.length)
  28. fileData.edit(new TextOperation().retain(longString.length - 1).remove(1))
  29. expect(fileData.getByteLength()).to.equal(longString.length - 1)
  30. expect(fileData.getStringLength()).to.equal(longString.length - 1)
  31. })
  32. it('getComments() should return an empty array', function () {
  33. const fileData = new StringFileData('test')
  34. expect(fileData.getComments()).to.eql([])
  35. })
  36. it('creates StringFileData with comments', function () {
  37. const fileData = new StringFileData('test', [
  38. {
  39. id: 'comm1',
  40. ranges: [
  41. {
  42. pos: 5,
  43. length: 10,
  44. },
  45. ],
  46. },
  47. {
  48. id: 'comm2',
  49. ranges: [
  50. {
  51. pos: 20,
  52. length: 5,
  53. },
  54. ],
  55. },
  56. ])
  57. expect(fileData.getComments()).to.eql([
  58. { id: 'comm1', ranges: [{ pos: 5, length: 10 }], resolved: false },
  59. { id: 'comm2', ranges: [{ pos: 20, length: 5 }], resolved: false },
  60. ])
  61. })
  62. it('fromRaw() should create StringFileData with comments', function () {
  63. const fileData = StringFileData.fromRaw({
  64. content: 'test',
  65. comments: [
  66. {
  67. id: 'comm1',
  68. ranges: [
  69. {
  70. pos: 5,
  71. length: 10,
  72. },
  73. ],
  74. resolved: false,
  75. },
  76. {
  77. id: 'comm2',
  78. ranges: [
  79. {
  80. pos: 20,
  81. length: 5,
  82. },
  83. ],
  84. resolved: true,
  85. },
  86. ],
  87. })
  88. expect(fileData.getComments()).to.eql([
  89. { id: 'comm1', ranges: [{ pos: 5, length: 10 }], resolved: false },
  90. { id: 'comm2', ranges: [{ pos: 20, length: 5 }], resolved: true },
  91. ])
  92. })
  93. })