string_file_data.test.js 2.9 KB

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