diff options
author | John Molakvoæ <skjnldsv@protonmail.com> | 2023-06-22 14:58:01 +0200 |
---|---|---|
committer | John Molakvoæ <skjnldsv@protonmail.com> | 2023-07-05 16:20:33 +0200 |
commit | 0c407d28af26180ad362f1aa9c1e2f3895d1a19e (patch) | |
tree | 77eb913cbd038cfb98b65da5548037805940473b /apps/files | |
parent | 97d69c356f8bbd3cba75a3aa36ef8559b621e94d (diff) | |
download | nextcloud-server-0c407d28af26180ad362f1aa9c1e2f3895d1a19e.tar.gz nextcloud-server-0c407d28af26180ad362f1aa9c1e2f3895d1a19e.zip |
feat: add view in folder action with tests
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
Diffstat (limited to 'apps/files')
-rw-r--r-- | apps/files/src/actions/viewInFolderAction.spec.ts | 161 | ||||
-rw-r--r-- | apps/files/src/actions/viewInFolderAction.ts | 68 | ||||
-rw-r--r-- | apps/files/src/main.ts | 1 |
3 files changed, 230 insertions, 0 deletions
diff --git a/apps/files/src/actions/viewInFolderAction.spec.ts b/apps/files/src/actions/viewInFolderAction.spec.ts new file mode 100644 index 00000000000..b16f2663f33 --- /dev/null +++ b/apps/files/src/actions/viewInFolderAction.spec.ts @@ -0,0 +1,161 @@ +/** + * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +import { action } from './viewInFolderAction' +import { expect } from '@jest/globals' +import { File, Folder, Node, Permission } from '@nextcloud/files' +import { FileAction } from '../services/FileAction' +import type { Navigation } from '../services/Navigation' + +const view = { + id: 'files', + name: 'Files', +} as Navigation + +describe('View in folder action conditions tests', () => { + test('Default values', () => { + expect(action).toBeInstanceOf(FileAction) + expect(action.id).toBe('view-in-folder') + expect(action.displayName([], view)).toBe('View in folder') + expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>') + expect(action.default).toBeUndefined() + expect(action.order).toBe(80) + }) +}) + +describe('View in folder action enabled tests', () => { + test('Enabled for files', () => { + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file], view)).toBe(true) + }) + + test('Disabled for non-dav ressources', () => { + const file = new File({ + id: 1, + source: 'https://domain.com/foobar.txt', + owner: 'admin', + mime: 'text/plain', + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file], view)).toBe(false) + }) + + test('Disabled if more than one node', () => { + const file1 = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt', + owner: 'admin', + mime: 'text/plain', + }) + const file2 = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt', + owner: 'admin', + mime: 'text/plain', + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file1, file2], view)).toBe(false) + }) + + test('Disabled for folders', () => { + const folder = new Folder({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/FooBar/', + owner: 'admin', + permissions: Permission.READ, + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([folder], view)).toBe(false) + }) +}) + +describe('View in folder action execute tests', () => { + test('View in folder', async () => { + const goToRouteMock = jest.fn() + window.OCP = { Files: { Router: { goToRoute: goToRouteMock } } } + + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + }) + + const exec = await action.exec(file, view, '/') + // Silent action + expect(exec).toBe(null) + expect(goToRouteMock).toBeCalledTimes(1) + expect(goToRouteMock).toBeCalledWith(null, { view: 'files' }, { dir: '/' }) + }) + + test('View in (sub) folder', async () => { + const goToRouteMock = jest.fn() + window.OCP = { Files: { Router: { goToRoute: goToRouteMock } } } + + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/Bar/foobar.txt', + root: '/files/admin', + owner: 'admin', + mime: 'text/plain', + }) + + const exec = await action.exec(file, view, '/') + // Silent action + expect(exec).toBe(null) + expect(goToRouteMock).toBeCalledTimes(1) + expect(goToRouteMock).toBeCalledWith(null, { view: 'files' }, { dir: '/Foo/Bar' }) + }) + + test('View in folder fails without node', async () => { + const goToRouteMock = jest.fn() + window.OCP = { Files: { Router: { goToRoute: goToRouteMock } } } + + const exec = await action.exec(null as unknown as Node, view, '/') + expect(exec).toBe(false) + expect(goToRouteMock).toBeCalledTimes(0) + }) + + test('View in folder fails without File', async () => { + const goToRouteMock = jest.fn() + window.OCP = { Files: { Router: { goToRoute: goToRouteMock } } } + + const folder = new Folder({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/', + owner: 'admin', + }) + + const exec = await action.exec(folder, view, '/') + expect(exec).toBe(false) + expect(goToRouteMock).toBeCalledTimes(0) + }) +}) diff --git a/apps/files/src/actions/viewInFolderAction.ts b/apps/files/src/actions/viewInFolderAction.ts new file mode 100644 index 00000000000..20fd62a37b8 --- /dev/null +++ b/apps/files/src/actions/viewInFolderAction.ts @@ -0,0 +1,68 @@ +/** + * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +import { Node, FileType } from '@nextcloud/files' +import { translate as t } from '@nextcloud/l10n' +import FolderMove from '@mdi/svg/svg/folder-move.svg?raw' + +import type { Navigation } from '../services/Navigation' +import { join } from 'path' +import { registerFileAction, FileAction } from '../services/FileAction' + +export const action = new FileAction({ + id: 'view-in-folder', + displayName() { + return t('files', 'View in folder') + }, + iconSvgInline: () => FolderMove, + + enabled(nodes: Node[]) { + // Only works on single node + if (nodes.length !== 1) { + return false + } + + const node = nodes[0] + + if (!node.isDavRessource) { + return false + } + + return node.type === FileType.File + }, + + async exec(node: Node, view: Navigation, dir: string) { + if (!node || node.type !== FileType.File) { + return false + } + + window.OCP.Files.Router.goToRoute( + null, + { view: 'files', fileid: node.fileid }, + { dir: node.dirname, fileid: node.fileid }, + ) + return null + }, + + order: 80, +}) + +registerFileAction(action) diff --git a/apps/files/src/main.ts b/apps/files/src/main.ts index f442c0d775a..2c0fa532570 100644 --- a/apps/files/src/main.ts +++ b/apps/files/src/main.ts @@ -8,6 +8,7 @@ import './actions/favoriteAction' import './actions/openFolderAction' import './actions/renameAction' import './actions/sidebarAction' +import './actions/viewInFolderAction' import Vue from 'vue' import { createPinia, PiniaVuePlugin } from 'pinia' |