Skip to content

Commit 56aec79

Browse files
committed
refactor: adjust to new @nextcloud/paths version
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent ab799c8 commit 56aec79

File tree

4 files changed

+47
-47
lines changed

4 files changed

+47
-47
lines changed

lib/composables/dav.spec.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import { defineComponent, h, nextTick, ref, toRef } from 'vue'
1111
import { useDAVFiles } from './dav.ts'
1212

1313
const nextcloudFiles = vi.hoisted(() => ({
14-
davGetClient: vi.fn(),
15-
davRootPath: '/root/uid',
16-
davRemoteURL: 'https://localhost/remote.php/dav',
17-
davResultToNode: vi.fn(),
18-
davGetDefaultPropfind: vi.fn(),
19-
davGetRecentSearch: (time: number) => `recent ${time}`,
14+
getClient: vi.fn(),
15+
defaultRootPath: '/root/uid',
16+
defaultRemoteURL: 'https://localhost/remote.php/dav',
17+
resultToNode: vi.fn(),
18+
getDefaultPropfind: vi.fn(),
19+
getRecentSearch: (time: number) => `recent ${time}`,
2020
getFavoriteNodes: vi.fn(),
2121
}))
22-
vi.mock('@nextcloud/files', () => nextcloudFiles)
22+
vi.mock('@nextcloud/files/dav', () => nextcloudFiles)
2323

2424
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
2525
function waitLoaded(vm: ComponentPublicInstance<{}, { isLoading: boolean }>) {
@@ -68,7 +68,7 @@ describe('dav composable', () => {
6868
const client = {
6969
getDirectoryContents: vi.fn(() => ({ data: [] })),
7070
}
71-
nextcloudFiles.davGetClient.mockImplementationOnce(() => client)
71+
nextcloudFiles.getClient.mockImplementationOnce(() => client)
7272

7373
const vue = shallowMount(TestComponent, {
7474
props: {
@@ -80,7 +80,7 @@ describe('dav composable', () => {
8080
// Loading is set to true
8181
expect(vue.vm.isLoading).toBe(true)
8282
// Dav client for dav remote url is gathered
83-
expect(nextcloudFiles.davGetClient).toBeCalled()
83+
expect(nextcloudFiles.getClient).toBeCalled()
8484
// files is an empty array
8585
expect(Array.isArray(vue.vm.files)).toBe(true)
8686
expect(vue.vm.files.length).toBe(0)
@@ -92,8 +92,8 @@ describe('dav composable', () => {
9292
const client = {
9393
getDirectoryContents: vi.fn(() => ({ data: ['1', '2'] })),
9494
}
95-
nextcloudFiles.davGetClient.mockImplementationOnce(() => client)
96-
nextcloudFiles.davResultToNode.mockImplementation((v) => `node ${v}`)
95+
nextcloudFiles.getClient.mockImplementationOnce(() => client)
96+
nextcloudFiles.resultToNode.mockImplementation((v) => `node ${v}`)
9797

9898
const vue = shallowMount(TestComponent, {
9999
props: {
@@ -114,7 +114,7 @@ describe('dav composable', () => {
114114
// eslint-disable-next-line @typescript-eslint/no-unused-vars
115115
getDirectoryContents: vi.fn((_path: string) => ({ data: [] })),
116116
}
117-
nextcloudFiles.davGetClient.mockImplementationOnce(() => client)
117+
nextcloudFiles.getClient.mockImplementationOnce(() => client)
118118

119119
const vue = shallowMount(TestComponent, {
120120
props: {
@@ -128,13 +128,13 @@ describe('dav composable', () => {
128128
await waitLoaded(vue.vm)
129129

130130
expect(client.getDirectoryContents).toBeCalledTimes(1)
131-
expect(client.getDirectoryContents.mock.calls[0]![0]).toBe(`${nextcloudFiles.davRootPath}/`)
131+
expect(client.getDirectoryContents.mock.calls[0]![0]).toBe(`${nextcloudFiles.defaultRootPath}/`)
132132

133133
vue.setProps({ currentPath: '/other' })
134134
await waitLoaded(vue.vm)
135135

136136
expect(client.getDirectoryContents).toBeCalledTimes(2)
137-
expect(client.getDirectoryContents.mock.calls[1]![0]).toBe(`${nextcloudFiles.davRootPath}/other`)
137+
expect(client.getDirectoryContents.mock.calls[1]![0]).toBe(`${nextcloudFiles.defaultRootPath}/other`)
138138
})
139139

140140
it('reloads on view change', async () => {
@@ -143,7 +143,7 @@ describe('dav composable', () => {
143143
getDirectoryContents: vi.fn((_path: string) => ({ data: [] })),
144144
search: vi.fn(() => ({ data: { results: [], truncated: false } })),
145145
}
146-
nextcloudFiles.davGetClient.mockImplementationOnce(() => client)
146+
nextcloudFiles.getClient.mockImplementationOnce(() => client)
147147

148148
const vue = shallowMount(TestComponent, {
149149
props: {
@@ -158,7 +158,7 @@ describe('dav composable', () => {
158158

159159
expect(client.search).not.toBeCalled()
160160
expect(client.getDirectoryContents).toBeCalledTimes(1)
161-
expect(client.getDirectoryContents.mock.calls[0]![0]).toBe(`${nextcloudFiles.davRootPath}/`)
161+
expect(client.getDirectoryContents.mock.calls[0]![0]).toBe(`${nextcloudFiles.defaultRootPath}/`)
162162

163163
vue.setProps({ currentView: 'recent' })
164164
await waitLoaded(vue.vm)
@@ -173,15 +173,15 @@ describe('dav composable', () => {
173173
stat: vi.fn((v) => ({ data: { path: v } })),
174174
createDirectory: vi.fn(() => {}),
175175
}
176-
nextcloudFiles.davGetClient.mockImplementation(() => client)
177-
nextcloudFiles.davResultToNode.mockImplementation((v) => v)
176+
nextcloudFiles.getClient.mockImplementation(() => client)
177+
nextcloudFiles.resultToNode.mockImplementation((v) => v)
178178

179179
const { createDirectory } = useDAVFiles(ref('files'), ref('/foo/'))
180180

181181
const node = await createDirectory('my-name')
182-
expect(node).toEqual({ path: `${nextcloudFiles.davRootPath}/foo/my-name` })
183-
expect(client.stat).toBeCalledWith(`${nextcloudFiles.davRootPath}/foo/my-name`, { details: true })
184-
expect(client.createDirectory).toBeCalledWith(`${nextcloudFiles.davRootPath}/foo/my-name`)
182+
expect(node).toEqual({ path: `${nextcloudFiles.defaultRootPath}/foo/my-name` })
183+
expect(client.stat).toBeCalledWith(`${nextcloudFiles.defaultRootPath}/foo/my-name`, { details: true })
184+
expect(client.createDirectory).toBeCalledWith(`${nextcloudFiles.defaultRootPath}/foo/my-name`)
185185
})
186186

187187
it('loadFiles work', async () => {
@@ -190,8 +190,8 @@ describe('dav composable', () => {
190190
getDirectoryContents: vi.fn(() => ({ data: [] })),
191191
search: vi.fn(() => ({ data: { results: [], truncated: false } })),
192192
}
193-
nextcloudFiles.davGetClient.mockImplementationOnce(() => client)
194-
nextcloudFiles.davResultToNode.mockImplementationOnce((v) => v)
193+
nextcloudFiles.getClient.mockImplementationOnce(() => client)
194+
nextcloudFiles.resultToNode.mockImplementationOnce((v) => v)
195195
nextcloudFiles.getFavoriteNodes.mockImplementationOnce(() => Promise.resolve([]))
196196

197197
const view = ref<'files' | 'recent' | 'favorites'>('files')
@@ -201,7 +201,7 @@ describe('dav composable', () => {
201201
expect(isLoading.value).toBe(true)
202202
await loadFiles()
203203
expect(isLoading.value).toBe(false)
204-
expect(client.getDirectoryContents).toBeCalledWith(`${nextcloudFiles.davRootPath}/`, expect.objectContaining({ details: true }))
204+
expect(client.getDirectoryContents).toBeCalledWith(`${nextcloudFiles.defaultRootPath}/`, expect.objectContaining({ details: true }))
205205

206206
view.value = 'recent'
207207
await waitRefLoaded(isLoading)
@@ -218,8 +218,8 @@ describe('dav composable', () => {
218218
getDirectoryContents: vi.fn(() => ({ data: [] })),
219219
search: vi.fn(() => ({ data: { results: [], truncated: false } })),
220220
}
221-
nextcloudFiles.davGetClient.mockImplementationOnce(() => client)
222-
nextcloudFiles.davResultToNode.mockImplementationOnce((v) => v)
221+
nextcloudFiles.getClient.mockImplementationOnce(() => client)
222+
nextcloudFiles.resultToNode.mockImplementationOnce((v) => v)
223223

224224
const view = ref<'files' | 'recent' | 'favorites'>('files')
225225
const path = ref('/')

lib/composables/dav.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import type { ContentsWithRoot, Folder, Node } from '@nextcloud/files'
66
import type { CancelablePromise } from 'cancelable-promise'
77
import type { ComputedRef, Ref } from 'vue'
88

9-
import { davGetClient, davRootPath, getFavoriteNodes } from '@nextcloud/files'
10-
import { joinPaths as join } from '@nextcloud/paths'
9+
import { defaultRootPath, getClient, getFavoriteNodes } from '@nextcloud/files/dav'
10+
import { join } from '@nextcloud/paths'
1111
import { onMounted, ref, shallowRef, watch } from 'vue'
1212
import { getFile, getNodes, getRecentNodes } from '../utils/dav.ts'
1313

@@ -24,7 +24,7 @@ export function useDAVFiles(
2424
/**
2525
* The WebDAV client
2626
*/
27-
const client = davGetClient()
27+
const client = getClient()
2828

2929
/**
3030
* All files in current view and path
@@ -56,7 +56,7 @@ export function useDAVFiles(
5656
async function createDirectory(name: string): Promise<Folder> {
5757
const path = join(currentPath.value, name)
5858

59-
await client.createDirectory(join(davRootPath, path))
59+
await client.createDirectory(join(defaultRootPath, path))
6060
const directory = await getFile(client, path) as Folder
6161
files.value = [...files.value, directory]
6262
return directory

lib/utils/dav.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import type { WebDAVClient } from 'webdav'
88
import { beforeEach, describe, expect, it, vi } from 'vitest'
99

1010
const nextcloudFiles = vi.hoisted(() => ({
11-
davResultToNode: vi.fn((v) => v),
12-
davGetDefaultPropfind: vi.fn(() => 'propfind content'),
13-
davRootPath: '/root/path',
11+
resultToNode: vi.fn((v) => v),
12+
getDefaultPropfind: vi.fn(() => 'propfind content'),
13+
defaultRootPath: '/root/path',
1414
}))
15-
vi.mock('@nextcloud/files', () => nextcloudFiles)
15+
vi.mock('@nextcloud/files/dav', () => nextcloudFiles)
1616

1717
describe('DAV utils', () => {
1818
beforeEach(() => {
@@ -28,9 +28,9 @@ describe('DAV utils', () => {
2828
const { getFile } = await import('./dav.ts')
2929

3030
const node = await getFile(client, '/some/path/file.ext')
31-
expect(node).toEqual({ path: `${nextcloudFiles.davRootPath}/some/path/file.ext` })
31+
expect(node).toEqual({ path: `${nextcloudFiles.defaultRootPath}/some/path/file.ext` })
3232
// Check mock usage
33-
expect(client.stat).toBeCalledWith(`${nextcloudFiles.davRootPath}/some/path/file.ext`, { details: true, data: 'propfind content' })
34-
expect(nextcloudFiles.davResultToNode).toBeCalledWith({ path: `${nextcloudFiles.davRootPath}/some/path/file.ext` })
33+
expect(client.stat).toBeCalledWith(`${nextcloudFiles.defaultRootPath}/some/path/file.ext`, { details: true, data: 'propfind content' })
34+
expect(nextcloudFiles.resultToNode).toBeCalledWith({ path: `${nextcloudFiles.defaultRootPath}/some/path/file.ext` })
3535
})
3636
})

lib/utils/dav.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import type { ContentsWithRoot, Folder, Node } from '@nextcloud/files'
77
import type { FileStat, ResponseDataDetailed, SearchResult, WebDAVClient } from 'webdav'
88

9-
import { davGetDefaultPropfind, davGetRecentSearch, davResultToNode, davRootPath } from '@nextcloud/files'
10-
import { joinPaths as join } from '@nextcloud/paths'
9+
import { defaultRootPath, getDefaultPropfind, getRecentSearch, resultToNode } from '@nextcloud/files/dav'
10+
import { join } from '@nextcloud/paths'
1111
import { CancelablePromise } from 'cancelable-promise'
1212

1313
/**
@@ -25,9 +25,9 @@ export function getRecentNodes(client: WebDAVClient): CancelablePromise<Node[]>
2525
const { data } = await client.search('/', {
2626
signal: controller.signal,
2727
details: true,
28-
data: davGetRecentSearch(lastTwoWeek),
28+
data: getRecentSearch(lastTwoWeek),
2929
}) as ResponseDataDetailed<SearchResult>
30-
const nodes = data.results.map((result: FileStat) => davResultToNode(result))
30+
const nodes = data.results.map((result: FileStat) => resultToNode(result))
3131
resolve(nodes)
3232
} catch (error) {
3333
reject(error)
@@ -46,13 +46,13 @@ export function getNodes(client: WebDAVClient, directoryPath: string): Cancelabl
4646
return new CancelablePromise(async (resolve, reject, onCancel) => {
4747
onCancel(() => controller.abort())
4848
try {
49-
const results = await client.getDirectoryContents(join(davRootPath, directoryPath), {
49+
const results = await client.getDirectoryContents(join(defaultRootPath, directoryPath), {
5050
signal: controller.signal,
5151
details: true,
5252
includeSelf: true,
53-
data: davGetDefaultPropfind(),
53+
data: getDefaultPropfind(),
5454
}) as ResponseDataDetailed<FileStat[]>
55-
const nodes = results.data.map((result: FileStat) => davResultToNode(result))
55+
const nodes = results.data.map((result: FileStat) => resultToNode(result))
5656
resolve({
5757
contents: nodes.filter(({ path }) => path !== directoryPath),
5858
folder: nodes.find(({ path }) => path === directoryPath) as Folder,
@@ -70,9 +70,9 @@ export function getNodes(client: WebDAVClient, directoryPath: string): Cancelabl
7070
* @param path The path of the file or folder
7171
*/
7272
export async function getFile(client: WebDAVClient, path: string) {
73-
const { data } = await client.stat(join(davRootPath, path), {
73+
const { data } = await client.stat(join(defaultRootPath, path), {
7474
details: true,
75-
data: davGetDefaultPropfind(),
75+
data: getDefaultPropfind(),
7676
}) as ResponseDataDetailed<FileStat>
77-
return davResultToNode(data)
77+
return resultToNode(data)
7878
}

0 commit comments

Comments
 (0)