string_file_data.test.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. })