aboutsummaryrefslogtreecommitdiffstats
path: root/cypress/e2e/files/files-actions.cy.ts
blob: a7febbda45ad480ff83cf7834f9fe981b144e4eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import type { User } from '@nextcloud/cypress'
import { FileAction } from '@nextcloud/files'

import { getActionButtonForFileId, getActionEntryForFileId, getRowForFile, getSelectionActionButton, getSelectionActionEntry, selectRowForFile } from './FilesUtils'
import { ACTION_COPY_MOVE } from '../../../apps/files/src/actions/moveOrCopyAction'
import { ACTION_DELETE } from '../../../apps/files/src/actions/deleteAction'
import { ACTION_DETAILS } from '../../../apps/files/src/actions/sidebarAction'
import { ACTION_SHARING_STATUS } from '../../../apps/files_sharing/src/files_actions/sharingStatusAction'

declare global {
	interface Window {
		_nc_fileactions: FileAction[]
	}
}

// Those two arrays doesn't represent the full list of actions
// the goal is to test a few, we're not trying to match the full feature set
const expectedDefaultActionsIDs = [
	ACTION_COPY_MOVE,
	ACTION_DELETE,
	ACTION_DETAILS,
	ACTION_SHARING_STATUS,
]
const expectedDefaultSelectionActionsIDs = [
	ACTION_COPY_MOVE,
	ACTION_DELETE,
]

describe('Files: Actions', { testIsolation: true }, () => {
	let user: User
	let fileId: number = 0

	beforeEach(() => cy.createRandomUser().then(($user) => {
		user = $user

		cy.uploadContent(user, new Blob([]), 'image/jpeg', '/image.jpg').then((response) => {
			fileId = Number.parseInt(response.headers['oc-fileid'] ?? '0')
		})
		cy.login(user)
	}))

	it('Show some standard actions', () => {
		cy.visit('/apps/files')
		getRowForFile('image.jpg').should('be.visible')

		expectedDefaultActionsIDs.forEach((actionId) => {
			// Open the menu
			getActionButtonForFileId(fileId).click({ force: true })
			// Check the action is visible
			getActionEntryForFileId(fileId, actionId).should('be.visible')
			// Close the menu
			cy.get('body').click({ force: true })
		})
	})

	it('Show some nested actions', () => {
		const parent = new FileAction({
			id: 'nested-action',
			displayName: () => 'Nested Action',
			exec: cy.spy(),
			iconSvgInline: () => '<svg></svg>',
		})

		const child1 = new FileAction({
			id: 'nested-child-1',
			displayName: () => 'Nested Child 1',
			exec: cy.spy(),
			iconSvgInline: () => '<svg></svg>',
			parent: 'nested-action',
		})

		const child2 = new FileAction({
			id: 'nested-child-2',
			displayName: () => 'Nested Child 2',
			exec: cy.spy(),
			iconSvgInline: () => '<svg></svg>',
			parent: 'nested-action',
		})

		cy.visit('/apps/files', {
			// Cannot use registerFileAction here
			onBeforeLoad: (win) => {
				if (!win._nc_fileactions) win._nc_fileactions = []
				// Cannot use registerFileAction here
				win._nc_fileactions.push(parent)
				win._nc_fileactions.push(child1)
				win._nc_fileactions.push(child2)
			}
		})

		// Open the menu
		getActionButtonForFileId(fileId).click({ force: true })

		// Check we have the parent action but not the children
		getActionEntryForFileId(fileId, 'nested-action').should('be.visible')
		getActionEntryForFileId(fileId, 'menu-back').should('not.exist')
		getActionEntryForFileId(fileId, 'nested-child-1').should('not.exist')
		getActionEntryForFileId(fileId, 'nested-child-2').should('not.exist')

		// Click on the parent action
		getActionEntryForFileId(fileId, 'nested-action')
			.find('button').last()
			.should('exist').click({ force: true })

		// Check we have the children and the back button but not the parent
		getActionEntryForFileId(fileId, 'nested-action').should('not.exist')
		getActionEntryForFileId(fileId, 'menu-back').should('be.visible')
		getActionEntryForFileId(fileId, 'nested-child-1').should('be.visible')
		getActionEntryForFileId(fileId, 'nested-child-2').should('be.visible')

		// Click on the back button
		getActionEntryForFileId(fileId, 'menu-back')
			.find('button').last()
			.should('exist').click({ force: true })

		// Check we have the parent action but not the children
		getActionEntryForFileId(fileId, 'nested-action').should('be.visible')
		getActionEntryForFileId(fileId, 'menu-back').should('not.exist')
		getActionEntryForFileId(fileId, 'nested-child-1').should('not.exist')
		getActionEntryForFileId(fileId, 'nested-child-2').should('not.exist')
	})

	it('Show some actions for a selection', () => {
		cy.visit('/apps/files')
		getRowForFile('image.jpg').should('be.visible')

		selectRowForFile('image.jpg')

		cy.get('[data-cy-files-list-selection-actions]').should('be.visible')
		getSelectionActionButton().should('be.visible')

		// Open the menu
		getSelectionActionButton().click({ force: true })

		// Check the action is visible
		expectedDefaultSelectionActionsIDs.forEach((actionId) => {
			getSelectionActionEntry(actionId).should('be.visible')
		})
	})

	it('Show some nested actions for a selection', () => {
		const parent = new FileAction({
			id: 'nested-action',
			displayName: () => 'Nested Action',
			exec: cy.spy(),
			iconSvgInline: () => '<svg></svg>',
		})

		const child1 = new FileAction({
			id: 'nested-child-1',
			displayName: () => 'Nested Child 1',
			exec: cy.spy(),
			execBatch: cy.spy(),
			iconSvgInline: () => '<svg></svg>',
			parent: 'nested-action',
		})

		const child2 = new FileAction({
			id: 'nested-child-2',
			displayName: () => 'Nested Child 2',
			exec: cy.spy(),
			execBatch: cy.spy(),
			iconSvgInline: () => '<svg></svg>',
			parent: 'nested-action',
		})

		cy.visit('/apps/files', {
			// Cannot use registerFileAction here
			onBeforeLoad: (win) => {
				if (!win._nc_fileactions) win._nc_fileactions = []
				// Cannot use registerFileAction here
				win._nc_fileactions.push(parent)
				win._nc_fileactions.push(child1)
				win._nc_fileactions.push(child2)
			}
		})

		selectRowForFile('image.jpg')

		// Open the menu
		getSelectionActionButton().click({ force: true })

		// Check we have the parent action but not the children
		getSelectionActionEntry('nested-action').should('be.visible')
		getSelectionActionEntry('menu-back').should('not.exist')
		getSelectionActionEntry('nested-child-1').should('not.exist')
		getSelectionActionEntry('nested-child-2').should('not.exist')

		// Click on the parent action
		getSelectionActionEntry('nested-action')
			.find('button').last()
			.should('exist').click({ force: true })

		// Check we have the children and the back button but not the parent
		getSelectionActionEntry('nested-action').should('not.exist')
		getSelectionActionEntry('menu-back').should('be.visible')
		getSelectionActionEntry('nested-child-1').should('be.visible')
		getSelectionActionEntry('nested-child-2').should('be.visible')

		// Click on the back button
		getSelectionActionEntry('menu-back')
			.find('button').last()
			.should('exist').click({ force: true })

		// Check we have the parent action but not the children
		getSelectionActionEntry('nested-action').should('be.visible')
		getSelectionActionEntry('menu-back').should('not.exist')
		getSelectionActionEntry('nested-child-1').should('not.exist')
		getSelectionActionEntry('nested-child-2').should('not.exist')
	})
})