overleaf/libraries/overleaf-editor-core/test/file.test.js
Domagoj Kriskovic fc90db231c Add CommentList to StringFileData (#16568)
* Add CommentList to StringFileData

* added more types

* use toRaw

* using Map rather than array for comments

* using Range class

* Comment with ranges:Range[]

* Revert "Comment with ranges:Range[]"

This reverts commit 0783b1837562600637db03cc70c620129061c797.

* Comment with ranges:Range[]

* remove isDeleted

* commentList.toRaw()

* using toRaw

* commentId to id

* Revert "using toRaw"

This reverts commit 0c04ca5836f3befd5ec027bad5bf722e8b27f36c.

* fix merge

* make comment map internal to CommentList

* remove unused type

* fix parameter name in StringFileData

* import types more consistently

* more consistent type def

GitOrigin-RevId: 2be2225819d8e8ebcf90d08def280377205cb9ec
2024-01-24 09:04:15 +00:00

96 lines
3.1 KiB
JavaScript

'use strict'
const { expect } = require('chai')
const FakeBlobStore = require('./support/fake_blob_store')
const ot = require('..')
const File = ot.File
describe('File', function () {
it('can have attached metadata', function () {
// no metadata
let file = File.fromString('foo')
expect(file.getMetadata()).to.eql({})
// metadata passed in at construction time
file = File.fromString('foo', { main: true })
expect(file.getMetadata()).to.eql({ main: true })
// metadata set at runtime
file.setMetadata({ main: false })
expect(file.getMetadata()).to.eql({ main: false })
})
describe('toRaw', function () {
it('returns non-empty metadata', function () {
const metadata = { main: true }
const file = File.fromHash(File.EMPTY_FILE_HASH, metadata)
expect(file.toRaw()).to.eql({
hash: File.EMPTY_FILE_HASH,
metadata,
})
delete file.getMetadata().main
expect(file.toRaw()).to.eql({ hash: File.EMPTY_FILE_HASH })
})
it('returns a deep clone of metadata', function () {
const metadata = { externalFile: { id: 123 } }
const file = File.fromHash(File.EMPTY_FILE_HASH, metadata)
const raw = file.toRaw()
const fileMetadata = file.getMetadata()
const rawMetadata = raw.metadata
expect(rawMetadata).not.to.equal(fileMetadata)
expect(rawMetadata).to.deep.equal(fileMetadata)
})
})
describe('store', function () {
it('does not return empty metadata', async function () {
const file = File.fromHash(File.EMPTY_FILE_HASH)
const fakeBlobStore = new FakeBlobStore()
const raw = await file.store(fakeBlobStore)
expect(raw).to.eql({ hash: File.EMPTY_FILE_HASH })
})
it('returns non-empty metadata', async function () {
const metadata = { main: true }
const file = File.fromHash(File.EMPTY_FILE_HASH, metadata)
const fakeBlobStore = new FakeBlobStore()
const raw = await file.store(fakeBlobStore)
expect(raw).to.eql({
hash: File.EMPTY_FILE_HASH,
metadata,
})
})
it('returns a deep clone of metadata', async function () {
const metadata = { externalFile: { id: 123 } }
const file = File.fromHash(File.EMPTY_FILE_HASH, metadata)
const fakeBlobStore = new FakeBlobStore()
const raw = await file.store(fakeBlobStore)
raw.metadata.externalFile.id = 456
expect(file.getMetadata().externalFile.id).to.equal(123)
})
})
describe('with string data', function () {
it('can be created from a string', function () {
const file = File.fromString('foo')
expect(file.getContent()).to.equal('foo')
})
})
describe('with hollow string data', function () {
it('can be cloned', function () {
const file = File.createHollow(null, 0)
expect(file.getStringLength()).to.equal(0)
const clone = file.clone()
expect(clone.getStringLength()).to.equal(0)
})
})
it('getComments() returns an empty comment list', function () {
const file = File.fromString('foo')
expect(file.getComments()).to.eql([])
})
})