mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-29 09:23:33 -05:00
Merge pull request #15481 from overleaf/td-preview-by-path-extension-fix
Fix off-by-one error in previewByPath GitOrigin-RevId: 28d351aa288ff2e92df421e0bc33c2d44eae91c0
This commit is contained in:
parent
cd27ee3c8f
commit
31299ed31e
2 changed files with 159 additions and 1 deletions
|
@ -125,7 +125,7 @@ export function previewByPath(
|
||||||
const { name, _id: id } = result.entity
|
const { name, _id: id } = result.entity
|
||||||
return {
|
return {
|
||||||
url: `/project/${projectId}/file/${id}`,
|
url: `/project/${projectId}/file/${id}`,
|
||||||
extension: name.slice(name.lastIndexOf('.')),
|
extension: name.slice(name.lastIndexOf('.') + 1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
158
services/web/test/frontend/features/file-tree/util/path.test.ts
Normal file
158
services/web/test/frontend/features/file-tree/util/path.test.ts
Normal file
|
@ -0,0 +1,158 @@
|
||||||
|
import { expect } from 'chai'
|
||||||
|
import { Folder } from '../../../../../types/folder'
|
||||||
|
import { docId } from '../../source-editor/helpers/mock-doc'
|
||||||
|
import {
|
||||||
|
findEntityByPath,
|
||||||
|
pathInFolder,
|
||||||
|
previewByPath,
|
||||||
|
} from '@/features/file-tree/util/path'
|
||||||
|
|
||||||
|
describe('Path utils', function () {
|
||||||
|
let rootFolder: Folder
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
rootFolder = {
|
||||||
|
_id: 'root-folder-id',
|
||||||
|
name: 'rootFolder',
|
||||||
|
docs: [
|
||||||
|
{
|
||||||
|
_id: docId,
|
||||||
|
name: 'main.tex',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
folders: [
|
||||||
|
{
|
||||||
|
_id: 'test-folder-id',
|
||||||
|
name: 'test-folder',
|
||||||
|
docs: [
|
||||||
|
{
|
||||||
|
_id: 'test-doc-in-folder',
|
||||||
|
name: 'example.tex',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
fileRefs: [
|
||||||
|
{
|
||||||
|
_id: 'test-file-in-folder',
|
||||||
|
name: 'example.png',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
folders: [
|
||||||
|
{
|
||||||
|
_id: 'test-subfolder-id',
|
||||||
|
name: 'test-subfolder',
|
||||||
|
docs: [
|
||||||
|
{
|
||||||
|
_id: 'test-doc-in-subfolder',
|
||||||
|
name: 'nested-example.tex',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
fileRefs: [
|
||||||
|
{
|
||||||
|
_id: 'test-file-in-subfolder',
|
||||||
|
name: 'nested-example.png',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
folders: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
fileRefs: [
|
||||||
|
{
|
||||||
|
_id: 'test-image-file',
|
||||||
|
name: 'frog.jpg',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
_id: 'uppercase-extension-image-file',
|
||||||
|
name: 'frog.JPG',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('pathInFolder', function () {
|
||||||
|
it('gets null path for non-existent entity', function () {
|
||||||
|
const retrieved = pathInFolder(rootFolder, 'non-existent.tex')
|
||||||
|
expect(retrieved).to.be.null
|
||||||
|
})
|
||||||
|
|
||||||
|
it('gets correct path for document in the root', function () {
|
||||||
|
const retrieved = pathInFolder(rootFolder, docId)
|
||||||
|
expect(retrieved).to.equal('main.tex')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('gets correct path for document in a folder', function () {
|
||||||
|
const retrieved = pathInFolder(rootFolder, 'test-doc-in-folder')
|
||||||
|
expect(retrieved).to.equal('test-folder/example.tex')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('gets correct path for document in a nested folder', function () {
|
||||||
|
const retrieved = pathInFolder(rootFolder, 'test-doc-in-subfolder')
|
||||||
|
expect(retrieved).to.equal(
|
||||||
|
'test-folder/test-subfolder/nested-example.tex'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('gets correct path for file in a nested folder', function () {
|
||||||
|
const retrieved = pathInFolder(rootFolder, 'test-file-in-subfolder')
|
||||||
|
expect(retrieved).to.equal(
|
||||||
|
'test-folder/test-subfolder/nested-example.png'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('gets correct path for file in a nested folder relative to folder', function () {
|
||||||
|
const retrieved = pathInFolder(
|
||||||
|
rootFolder.folders[0],
|
||||||
|
'test-file-in-subfolder'
|
||||||
|
)
|
||||||
|
expect(retrieved).to.equal('test-subfolder/nested-example.png')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('findEntityByPath', function () {
|
||||||
|
it('returns null for a non-existent path', function () {
|
||||||
|
const retrieved = findEntityByPath(rootFolder, 'not-a-real-document.tex')
|
||||||
|
expect(retrieved).to.be.null
|
||||||
|
})
|
||||||
|
|
||||||
|
it('finds a document in the root', function () {
|
||||||
|
const retrieved = findEntityByPath(rootFolder, 'main.tex')
|
||||||
|
expect(retrieved?.entity._id).to.equal(docId)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('finds a document in a folder', function () {
|
||||||
|
const retrieved = findEntityByPath(rootFolder, 'test-folder/example.tex')
|
||||||
|
expect(retrieved?.entity._id).to.equal('test-doc-in-folder')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('finds a document in a nested folder', function () {
|
||||||
|
const retrieved = findEntityByPath(
|
||||||
|
rootFolder,
|
||||||
|
'test-folder/test-subfolder/nested-example.tex'
|
||||||
|
)
|
||||||
|
expect(retrieved?.entity._id).to.equal('test-doc-in-subfolder')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('finds a file in a nested folder', function () {
|
||||||
|
const retrieved = findEntityByPath(
|
||||||
|
rootFolder,
|
||||||
|
'test-folder/test-subfolder/nested-example.png'
|
||||||
|
)
|
||||||
|
expect(retrieved?.entity._id).to.equal('test-file-in-subfolder')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('previewByPath', function () {
|
||||||
|
it('returns extension without preceding dot', function () {
|
||||||
|
const preview = previewByPath(
|
||||||
|
rootFolder,
|
||||||
|
'test-project-id',
|
||||||
|
'test-folder/example.png'
|
||||||
|
)
|
||||||
|
expect(preview).to.deep.equal({
|
||||||
|
url: '/project/test-project-id/file/test-file-in-folder',
|
||||||
|
extension: 'png',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in a new issue