Skip to content

Commit

Permalink
fix: Clean path only for files, not directory
Browse files Browse the repository at this point in the history
  • Loading branch information
paultranvan committed Nov 13, 2024
1 parent 431c37b commit ba015d9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import CozyClient from 'cozy-client'
import { IOCozyContact, IOCozyFile } from 'cozy-client/types/types'
import { IOCozyFile } from 'cozy-client/types/types'

import {
addFilePaths,
computeFileFullpath,
normalizeFileWithFolders,
shouldKeepFile
} from './normalizeFile'
import { cleanFilePath } from './normalizeSearchResult'
import { FILES_DOCTYPE } from '../consts'
import { queryDocById } from '../queries'
import { CozyDoc } from '../types'

Expand Down Expand Up @@ -295,35 +293,3 @@ describe('computeFileFullpath', () => {
expect(res.path).toEqual('ROOT/MYDIR/file3')
})
})

describe('cleanFilePath', () => {
it('should return the document unchanged if it is not an IOCozyFile', () => {
const doc = { fullname: 'name' } as IOCozyContact
expect(cleanFilePath(doc)).toEqual(doc)
})

it('should return the document unchanged if path is undefined', () => {
const doc = { _type: FILES_DOCTYPE, name: 'name' } as IOCozyFile
expect(cleanFilePath(doc)).toEqual(doc)
})

it('should remove name from path if path ends with name', () => {
const doc = {
_type: FILES_DOCTYPE,
path: '/the/path/myname',
name: 'myname'
} as IOCozyFile
const expected = { ...doc, path: '/the/path' }

expect(cleanFilePath(doc)).toEqual(expected)
})

it('should return the document unchanged if path does not end with name', () => {
const doc = {
_type: FILES_DOCTYPE,
path: '/the/path/othername',
name: 'name'
} as IOCozyFile
expect(cleanFilePath(doc)).toEqual(doc)
})
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import CozyClient from 'cozy-client'
import { IOCozyContact, IOCozyFile } from 'cozy-client/types/types'

import { normalizeSearchResult } from './normalizeSearchResult'
import { cleanFilePath, normalizeSearchResult } from './normalizeSearchResult'
import { FILES_DOCTYPE, TYPE_FILE } from '../consts'
import { RawSearchResult } from '../types'

const fakeFlatDomainClient = {
Expand Down Expand Up @@ -341,3 +343,46 @@ describe('Should normalize unknown doctypes', () => {
})
})
})

describe('cleanFilePath', () => {
it('should return the document unchanged if it is not an IOCozyFile', () => {
const doc = { fullname: 'name' } as IOCozyContact
expect(cleanFilePath(doc)).toEqual(doc)
})

it('should return the document unchanged if path is undefined', () => {
const doc = { _type: FILES_DOCTYPE, name: 'name' } as IOCozyFile
expect(cleanFilePath(doc)).toEqual(doc)
})

it('should return the document unchanged if not a file', () => {
const doc = {
_type: FILES_DOCTYPE,
name: 'name',
type: TYPE_FILE
} as IOCozyFile
expect(cleanFilePath(doc)).toEqual(doc)
})

it('should remove name from path if path ends with name', () => {
const doc = {
_type: FILES_DOCTYPE,
type: TYPE_FILE,
path: '/the/path/myname',
name: 'myname'
} as IOCozyFile
const expected = { ...doc, path: '/the/path' }

expect(cleanFilePath(doc)).toEqual(expected)
})

it('should return the document unchanged if path does not end with name', () => {
const doc = {
_type: FILES_DOCTYPE,
type: TYPE_FILE,
path: '/the/path/othername',
name: 'name'
} as IOCozyFile
expect(cleanFilePath(doc)).toEqual(doc)
})
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import CozyClient, { generateWebLink, models } from 'cozy-client'
import { IOCozyContact } from 'cozy-client/types/types'

import { APPS_DOCTYPE, TYPE_DIRECTORY } from '../consts'
import { APPS_DOCTYPE, TYPE_DIRECTORY, TYPE_FILE } from '../consts'
import {
CozyDoc,
RawSearchResult,
Expand Down Expand Up @@ -34,8 +34,8 @@ export const cleanFilePath = (doc: CozyDoc): CozyDoc => {
if (!isIOCozyFile(doc)) {
return doc
}
const { path, name } = doc
if (!path) {
const { path, name, type } = doc
if (!path || type !== TYPE_FILE) {
return doc
}
let newPath = path
Expand Down

0 comments on commit ba015d9

Please sign in to comment.