|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { describe, beforeEach, test, expect } from 'vitest' |
| 7 | +import { setActivePinia, createPinia } from 'pinia' |
| 8 | +import { usePathsStore } from './paths.ts' |
| 9 | +import { emit } from '@nextcloud/event-bus' |
| 10 | +import { File, Folder } from '@nextcloud/files' |
| 11 | +import { useFilesStore } from './files.ts' |
| 12 | + |
| 13 | +describe('Path store', () => { |
| 14 | + |
| 15 | + let store: ReturnType<typeof usePathsStore> |
| 16 | + let files: ReturnType<typeof useFilesStore> |
| 17 | + let root: Folder & { _children?: string[] } |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + setActivePinia(createPinia()) |
| 21 | + |
| 22 | + root = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/', id: 1 }) |
| 23 | + files = useFilesStore() |
| 24 | + files.setRoot({ service: 'files', root }) |
| 25 | + |
| 26 | + store = usePathsStore() |
| 27 | + }) |
| 28 | + |
| 29 | + test('Folder is created', () => { |
| 30 | + // no defined paths |
| 31 | + expect(store.paths).toEqual({}) |
| 32 | + |
| 33 | + // create the folder |
| 34 | + const node = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 }) |
| 35 | + emit('files:node:created', node) |
| 36 | + |
| 37 | + // see that the path is added |
| 38 | + expect(store.paths).toEqual({ files: { [node.path]: node.source } }) |
| 39 | + |
| 40 | + // see that the node is added |
| 41 | + expect(root._children).toEqual([node.source]) |
| 42 | + }) |
| 43 | + |
| 44 | + test('File is created', () => { |
| 45 | + // no defined paths |
| 46 | + expect(store.paths).toEqual({}) |
| 47 | + |
| 48 | + // create the file |
| 49 | + const node = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' }) |
| 50 | + emit('files:node:created', node) |
| 51 | + |
| 52 | + // see that there are still no paths |
| 53 | + expect(store.paths).toEqual({}) |
| 54 | + |
| 55 | + // see that the node is added |
| 56 | + expect(root._children).toEqual([node.source]) |
| 57 | + }) |
| 58 | + |
| 59 | + test('Existing file is created', () => { |
| 60 | + // no defined paths |
| 61 | + expect(store.paths).toEqual({}) |
| 62 | + |
| 63 | + // create the file |
| 64 | + const node1 = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' }) |
| 65 | + emit('files:node:created', node1) |
| 66 | + |
| 67 | + // see that there are still no paths |
| 68 | + expect(store.paths).toEqual({}) |
| 69 | + |
| 70 | + // see that the node is added |
| 71 | + expect(root._children).toEqual([node1.source]) |
| 72 | + |
| 73 | + // create the same named file again |
| 74 | + const node2 = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' }) |
| 75 | + emit('files:node:created', node2) |
| 76 | + |
| 77 | + // see that there are still no paths and the children are not duplicated |
| 78 | + expect(store.paths).toEqual({}) |
| 79 | + expect(root._children).toEqual([node1.source]) |
| 80 | + |
| 81 | + }) |
| 82 | + |
| 83 | + test('Existing folder is created', () => { |
| 84 | + // no defined paths |
| 85 | + expect(store.paths).toEqual({}) |
| 86 | + |
| 87 | + // create the file |
| 88 | + const node1 = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 }) |
| 89 | + emit('files:node:created', node1) |
| 90 | + |
| 91 | + // see the path is added |
| 92 | + expect(store.paths).toEqual({ files: { [node1.path]: node1.source } }) |
| 93 | + |
| 94 | + // see that the node is added |
| 95 | + expect(root._children).toEqual([node1.source]) |
| 96 | + |
| 97 | + // create the same named file again |
| 98 | + const node2 = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 }) |
| 99 | + emit('files:node:created', node2) |
| 100 | + |
| 101 | + // see that there is still only one paths and the children are not duplicated |
| 102 | + expect(store.paths).toEqual({ files: { [node1.path]: node1.source } }) |
| 103 | + expect(root._children).toEqual([node1.source]) |
| 104 | + }) |
| 105 | + |
| 106 | + test('Folder is deleted', () => { |
| 107 | + const node = new Folder({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/folder', id: 2 }) |
| 108 | + emit('files:node:created', node) |
| 109 | + // see that the path is added and the children are set-up |
| 110 | + expect(store.paths).toEqual({ files: { [node.path]: node.source } }) |
| 111 | + expect(root._children).toEqual([node.source]) |
| 112 | + |
| 113 | + emit('files:node:deleted', node) |
| 114 | + // See the path is removed |
| 115 | + expect(store.paths).toEqual({ files: {} }) |
| 116 | + // See the child is removed |
| 117 | + expect(root._children).toEqual([]) |
| 118 | + }) |
| 119 | + |
| 120 | + test('File is deleted', () => { |
| 121 | + const node = new File({ owner: 'test', source: 'http://example.com/remote.php/dav/files/test/file.txt', id: 2, mime: 'text/plain' }) |
| 122 | + emit('files:node:created', node) |
| 123 | + // see that the children are set-up |
| 124 | + expect(root._children).toEqual([node.source]) |
| 125 | + |
| 126 | + emit('files:node:deleted', node) |
| 127 | + // See the child is removed |
| 128 | + expect(root._children).toEqual([]) |
| 129 | + }) |
| 130 | +}) |
0 commit comments