|  | 
|  | 1 | +/** | 
|  | 2 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | 
|  | 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later | 
|  | 4 | + */ | 
|  | 5 | + | 
|  | 6 | +import { Folder } from '@nextcloud/files' | 
|  | 7 | +import { beforeEach, describe, expect, it, vi } from 'vitest' | 
|  | 8 | +import * as ncEventBus from '@nextcloud/event-bus' | 
|  | 9 | +import isSvg from 'is-svg' | 
|  | 10 | + | 
|  | 11 | +import { trashbinView } from '../files_views/trashbinView.ts' | 
|  | 12 | +import { restoreAction } from './restoreAction.ts' | 
|  | 13 | +import { PERMISSION_ALL, PERMISSION_NONE } from '../../../../core/src/OC/constants.js' | 
|  | 14 | + | 
|  | 15 | +const axiosMock = vi.hoisted(() => ({ | 
|  | 16 | +	request: vi.fn(), | 
|  | 17 | +})) | 
|  | 18 | +vi.mock('@nextcloud/axios', () => ({ default: axiosMock })) | 
|  | 19 | +vi.mock('@nextcloud/auth') | 
|  | 20 | + | 
|  | 21 | +describe('files_trashbin: file actions - restore action', () => { | 
|  | 22 | +	it('has id set', () => { | 
|  | 23 | +		expect(restoreAction.id).toBe('restore') | 
|  | 24 | +	}) | 
|  | 25 | + | 
|  | 26 | +	it('has order set', () => { | 
|  | 27 | +		// very high priority! | 
|  | 28 | +		expect(restoreAction.order).toBe(1) | 
|  | 29 | +	}) | 
|  | 30 | + | 
|  | 31 | +	it('is an inline action', () => { | 
|  | 32 | +		const node = new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/folder', root: '/trashbin/test/' }) | 
|  | 33 | + | 
|  | 34 | +		expect(restoreAction.inline).toBeTypeOf('function') | 
|  | 35 | +		expect(restoreAction.inline!(node, trashbinView)).toBe(true) | 
|  | 36 | +	}) | 
|  | 37 | + | 
|  | 38 | +	it('has the display name set', () => { | 
|  | 39 | +		const node = new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/folder', root: '/trashbin/test/' }) | 
|  | 40 | + | 
|  | 41 | +		expect(restoreAction.displayName([node], trashbinView)).toBe('Restore') | 
|  | 42 | +	}) | 
|  | 43 | + | 
|  | 44 | +	it('has an icon set', () => { | 
|  | 45 | +		const node = new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/folder', root: '/trashbin/test/' }) | 
|  | 46 | + | 
|  | 47 | +		const icon = restoreAction.iconSvgInline([node], trashbinView) | 
|  | 48 | +		expect(icon).toBeTypeOf('string') | 
|  | 49 | +		expect(isSvg(icon)).toBe(true) | 
|  | 50 | +	}) | 
|  | 51 | + | 
|  | 52 | +	it('is enabled for trashbin view', () => { | 
|  | 53 | +		const nodes = [ | 
|  | 54 | +			new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/folder', root: '/trashbin/test/', permissions: PERMISSION_ALL }), | 
|  | 55 | +		] | 
|  | 56 | + | 
|  | 57 | +		expect(restoreAction.enabled).toBeTypeOf('function') | 
|  | 58 | +		expect(restoreAction.enabled!(nodes, trashbinView)).toBe(true) | 
|  | 59 | +	}) | 
|  | 60 | + | 
|  | 61 | +	it('is not enabled when permissions are missing', () => { | 
|  | 62 | +		const nodes = [ | 
|  | 63 | +			new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/folder', root: '/trashbin/test/', permissions: PERMISSION_NONE }), | 
|  | 64 | +		] | 
|  | 65 | + | 
|  | 66 | +		expect(restoreAction.enabled).toBeTypeOf('function') | 
|  | 67 | +		expect(restoreAction.enabled!(nodes, trashbinView)).toBe(false) | 
|  | 68 | +	}) | 
|  | 69 | + | 
|  | 70 | +	it('is not enabled when no nodes are selected', () => { | 
|  | 71 | +		expect(restoreAction.enabled).toBeTypeOf('function') | 
|  | 72 | +		expect(restoreAction.enabled!([], trashbinView)).toBe(false) | 
|  | 73 | +	}) | 
|  | 74 | + | 
|  | 75 | +	it('is not enabled for other views', () => { | 
|  | 76 | +		const nodes = [ | 
|  | 77 | +			new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/folder', root: '/trashbin/test/', permissions: PERMISSION_ALL }), | 
|  | 78 | +		] | 
|  | 79 | + | 
|  | 80 | +		const otherView = new Proxy(trashbinView, { | 
|  | 81 | +			get(target, p) { | 
|  | 82 | +				if (p === 'id') { | 
|  | 83 | +					return 'other-view' | 
|  | 84 | +				} | 
|  | 85 | +				return target[p] | 
|  | 86 | +			}, | 
|  | 87 | +		}) | 
|  | 88 | + | 
|  | 89 | +		expect(restoreAction.enabled).toBeTypeOf('function') | 
|  | 90 | +		expect(restoreAction.enabled!(nodes, otherView)).toBe(false) | 
|  | 91 | +	}) | 
|  | 92 | + | 
|  | 93 | +	describe('execute', () => { | 
|  | 94 | +		beforeEach(() => { | 
|  | 95 | +			axiosMock.request.mockReset() | 
|  | 96 | +		}) | 
|  | 97 | + | 
|  | 98 | +		it('send restore request', async () => { | 
|  | 99 | +			const node = new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/folder', root: '/trashbin/test/', permissions: PERMISSION_ALL }) | 
|  | 100 | + | 
|  | 101 | +			expect(await restoreAction.exec(node, trashbinView, '/')).toBe(true) | 
|  | 102 | +			expect(axiosMock.request).toBeCalled() | 
|  | 103 | +			expect(axiosMock.request.mock.calls[0][0].method).toBe('MOVE') | 
|  | 104 | +			expect(axiosMock.request.mock.calls[0][0].url).toBe(node.encodedSource) | 
|  | 105 | +			expect(axiosMock.request.mock.calls[0][0].headers.destination).toContain('/restore/') | 
|  | 106 | +		}) | 
|  | 107 | + | 
|  | 108 | +		it('deletes node from current view after successfull request', async () => { | 
|  | 109 | +			const node = new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/folder', root: '/trashbin/test/', permissions: PERMISSION_ALL }) | 
|  | 110 | + | 
|  | 111 | +			const emitSpy = vi.spyOn(ncEventBus, 'emit') | 
|  | 112 | + | 
|  | 113 | +			expect(await restoreAction.exec(node, trashbinView, '/')).toBe(true) | 
|  | 114 | +			expect(axiosMock.request).toBeCalled() | 
|  | 115 | +			expect(emitSpy).toBeCalled() | 
|  | 116 | +			expect(emitSpy).toBeCalledWith('files:node:deleted', node) | 
|  | 117 | +		}) | 
|  | 118 | + | 
|  | 119 | +		it('does not delete node from view if reuest failed', async () => { | 
|  | 120 | +			const node = new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/folder', root: '/trashbin/test/', permissions: PERMISSION_ALL }) | 
|  | 121 | + | 
|  | 122 | +			axiosMock.request.mockImplementationOnce(() => { throw new Error() }) | 
|  | 123 | +			const emitSpy = vi.spyOn(ncEventBus, 'emit') | 
|  | 124 | + | 
|  | 125 | +			expect(await restoreAction.exec(node, trashbinView, '/')).toBe(false) | 
|  | 126 | +			expect(axiosMock.request).toBeCalled() | 
|  | 127 | +			expect(emitSpy).not.toBeCalled() | 
|  | 128 | +		}) | 
|  | 129 | + | 
|  | 130 | +		it('batch: only returns success if all requests worked', async () => { | 
|  | 131 | +			const node = new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/folder', root: '/trashbin/test/', permissions: PERMISSION_ALL }) | 
|  | 132 | + | 
|  | 133 | +			expect(await restoreAction.execBatch!([node, node], trashbinView, '/')).toStrictEqual([true, true]) | 
|  | 134 | +			expect(axiosMock.request).toBeCalledTimes(2) | 
|  | 135 | +		}) | 
|  | 136 | + | 
|  | 137 | +		it('batch: only returns success if all requests worked - one failed', async () => { | 
|  | 138 | +			const node = new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/folder', root: '/trashbin/test/', permissions: PERMISSION_ALL }) | 
|  | 139 | + | 
|  | 140 | +			axiosMock.request.mockImplementationOnce(() => { throw new Error() }) | 
|  | 141 | +			expect(await restoreAction.execBatch!([node, node], trashbinView, '/')).toStrictEqual([false, true]) | 
|  | 142 | +			expect(axiosMock.request).toBeCalledTimes(2) | 
|  | 143 | +		}) | 
|  | 144 | +	}) | 
|  | 145 | +}) | 
0 commit comments