diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-09-11 21:51:54 +0200 |
---|---|---|
committer | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-10-17 17:54:13 +0200 |
commit | 7b78a64fa877074cf44dda59c6c11e99ea194722 (patch) | |
tree | f11564a666dd6ba9093509ae9fd592b013711780 /apps/files/src | |
parent | c7d22f9773e52a26253bc7b00b547b40e819bb97 (diff) | |
download | nextcloud-server-backport/47905/stable30.tar.gz nextcloud-server-backport/47905/stable30.zip |
test(files): Add tests for path handlingbackport/47905/stable30
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Diffstat (limited to 'apps/files/src')
-rw-r--r-- | apps/files/src/store/files.ts | 6 | ||||
-rw-r--r-- | apps/files/src/store/paths.spec.ts | 130 | ||||
-rw-r--r-- | apps/files/src/store/paths.ts | 5 |
3 files changed, 137 insertions, 4 deletions
diff --git a/apps/files/src/store/files.ts b/apps/files/src/store/files.ts index 1af675e67ce..5a701f52b3c 100644 --- a/apps/files/src/store/files.ts +++ b/apps/files/src/store/files.ts @@ -3,7 +3,7 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -import type { FilesStore, RootsStore, RootOptions, Service, FilesState, FileSource } from '../types' +import type { FilesStore, RootsStore, RootOptions, Service, FileSource } from '../types' import type { FileStat, ResponseDataDetailed } from 'webdav' import type { Folder, Node } from '@nextcloud/files' @@ -27,9 +27,10 @@ const fetchNode = async (node: Node): Promise<Node> => { export const useFilesStore = function(...args) { const store = defineStore('files', { - state: (): FilesState => ({ + state: () => ({ files: {} as FilesStore, roots: {} as RootsStore, + _initialized: false, }), getters: { @@ -86,6 +87,7 @@ export const useFilesStore = function(...args) { } // If we found a cache entry and the cache entry was already loaded (has children) then use it + // @ts-expect-error The _children prop is undocumented - we need to make this official return (folder?._children ?? []) .map((source: string) => this.getNode(source)) .filter(Boolean) diff --git a/apps/files/src/store/paths.spec.ts b/apps/files/src/store/paths.spec.ts new file mode 100644 index 00000000000..fab621f2915 --- /dev/null +++ b/apps/files/src/store/paths.spec.ts @@ -0,0 +1,130 @@ +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import { emit } from '@nextcloud/event-bus' +import { describe, expect, test } from '@jest/globals' +import { File, Folder } from '@nextcloud/files' +import { setActivePinia, createPinia } from 'pinia' +import { usePathsStore } from './paths.ts' +import { useFilesStore } from './files.ts' + +describe('Path store', () => { + + let store: ReturnType<typeof usePathsStore> + let files: ReturnType<typeof useFilesStore> + let root: Folder & { _children?: string[] } + + beforeEach(() => { + setActivePinia(createPinia()) + + root = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/', id: 1 }) + files = useFilesStore() + files.setRoot({ service: 'files', root }) + + store = usePathsStore() + }) + + test('Folder is created', () => { + // no defined paths + expect(store.paths).toEqual({}) + + // create the folder + const node = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 }) + emit('files:node:created', node) + + // see that the path is added + expect(store.paths).toEqual({ files: { [node.path]: node.source } }) + + // see that the node is added + expect(root._children).toEqual([node.source]) + }) + + test('File is created', () => { + // no defined paths + expect(store.paths).toEqual({}) + + // create the file + const node = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' }) + emit('files:node:created', node) + + // see that there are still no paths + expect(store.paths).toEqual({}) + + // see that the node is added + expect(root._children).toEqual([node.source]) + }) + + test('Existing file is created', () => { + // no defined paths + expect(store.paths).toEqual({}) + + // create the file + const node1 = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' }) + emit('files:node:created', node1) + + // see that there are still no paths + expect(store.paths).toEqual({}) + + // see that the node is added + expect(root._children).toEqual([node1.source]) + + // create the same named file again + const node2 = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' }) + emit('files:node:created', node2) + + // see that there are still no paths and the children are not duplicated + expect(store.paths).toEqual({}) + expect(root._children).toEqual([node1.source]) + + }) + + test('Existing folder is created', () => { + // no defined paths + expect(store.paths).toEqual({}) + + // create the file + const node1 = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 }) + emit('files:node:created', node1) + + // see the path is added + expect(store.paths).toEqual({ files: { [node1.path]: node1.source } }) + + // see that the node is added + expect(root._children).toEqual([node1.source]) + + // create the same named file again + const node2 = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 }) + emit('files:node:created', node2) + + // see that there is still only one paths and the children are not duplicated + expect(store.paths).toEqual({ files: { [node1.path]: node1.source } }) + expect(root._children).toEqual([node1.source]) + }) + + test('Folder is deleted', () => { + const node = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 }) + emit('files:node:created', node) + // see that the path is added and the children are set-up + expect(store.paths).toEqual({ files: { [node.path]: node.source } }) + expect(root._children).toEqual([node.source]) + + emit('files:node:deleted', node) + // See the path is removed + expect(store.paths).toEqual({ files: {} }) + // See the child is removed + expect(root._children).toEqual([]) + }) + + test('File is deleted', () => { + const node = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' }) + emit('files:node:created', node) + // see that the children are set-up + expect(root._children).toEqual([node.source]) + + emit('files:node:deleted', node) + // See the child is removed + expect(root._children).toEqual([]) + }) +}) diff --git a/apps/files/src/store/paths.ts b/apps/files/src/store/paths.ts index 8c197400eac..bf197a0c6df 100644 --- a/apps/files/src/store/paths.ts +++ b/apps/files/src/store/paths.ts @@ -2,7 +2,7 @@ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ -import type { FileSource, PathsStore, PathOptions, ServicesState, Service } from '../types' +import type { FileSource, PathOptions, ServicesState, Service } from '../types' import { defineStore } from 'pinia' import { FileType, Folder, Node, getNavigation } from '@nextcloud/files' import { subscribe } from '@nextcloud/event-bus' @@ -17,7 +17,8 @@ export const usePathsStore = function(...args) { const store = defineStore('paths', { state: () => ({ paths: {} as ServicesState, - } as PathsStore), + _initialized: false, + }), getters: { getPath: (state) => { |