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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { action } from './favoriteAction'
import { expect } from '@jest/globals'
import { File, Permission, View, FileAction } from '@nextcloud/files'
import axios from '@nextcloud/axios'
import eventBus from '@nextcloud/event-bus'
import * as favoriteAction from './favoriteAction'
import logger from '../logger'
const view = {
id: 'files',
name: 'Files',
} as View
const favoriteView = {
id: 'favorites',
name: 'Favorites',
} as View
window.OC = {
...window.OC,
TAG_FAVORITE: '_$!<Favorite>!$_',
}
// Mock webroot variable
beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any)._oc_webroot = ''
})
describe('Favorite action conditions tests', () => {
test('Default values', () => {
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).toBeInstanceOf(FileAction)
expect(action.id).toBe('favorite')
expect(action.displayName([file], view)).toBe('Add to favorites')
expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>')
expect(action.default).toBeUndefined()
expect(action.order).toBe(-50)
})
test('Display name is Remove from favorites if already in favorites', () => {
const file = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
attributes: {
favorite: 1,
},
})
expect(action.displayName([file], view)).toBe('Remove from favorites')
})
test('Display name for multiple state files', () => {
const file1 = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
attributes: {
favorite: 1,
},
})
const file2 = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
attributes: {
favorite: 0,
},
})
const file3 = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
attributes: {
favorite: 1,
},
})
expect(action.displayName([file1, file2, file3], view)).toBe('Add to favorites')
expect(action.displayName([file1, file2], view)).toBe('Add to favorites')
expect(action.displayName([file2, file3], view)).toBe('Add to favorites')
expect(action.displayName([file1, file3], view)).toBe('Remove from favorites')
})
})
describe('Favorite action enabled tests', () => {
test('Enabled for dav file', () => {
const file = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
})
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/data/foobar.txt',
owner: 'admin',
mime: 'text/plain',
})
expect(action.enabled).toBeDefined()
expect(action.enabled!([file], view)).toBe(false)
})
})
describe('Favorite action execute tests', () => {
afterEach(() => {
jest.spyOn(axios, 'post').mockRestore()
})
test('Favorite triggers tag addition', async () => {
jest.spyOn(axios, 'post')
jest.spyOn(eventBus, 'emit')
const file = new File({
id: 1,
source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
})
const exec = await action.exec(file, view, '/')
expect(exec).toBe(true)
// Check POST request
expect(axios.post).toBeCalledTimes(1)
expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: ['_$!<Favorite>!$_'] })
// Check node change propagation
expect(file.attributes.favorite).toBe(1)
expect(eventBus.emit).toBeCalledTimes(1)
expect(eventBus.emit).toBeCalledWith('files:favorites:added', file)
})
test('Favorite triggers tag removal', async () => {
jest.spyOn(axios, 'post')
jest.spyOn(eventBus, 'emit')
const file = new File({
id: 1,
source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
attributes: {
favorite: 1,
},
})
const exec = await action.exec(file, view, '/')
expect(exec).toBe(true)
// Check POST request
expect(axios.post).toBeCalledTimes(1)
expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: [] })
// Check node change propagation
expect(file.attributes.favorite).toBe(0)
expect(eventBus.emit).toBeCalledTimes(1)
expect(eventBus.emit).toBeCalledWith('files:favorites:removed', file)
})
test('Favorite triggers node removal if favorite view and root dir', async () => {
jest.spyOn(axios, 'post')
jest.spyOn(eventBus, 'emit')
const file = new File({
id: 1,
source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
attributes: {
favorite: 1,
},
})
const exec = await action.exec(file, favoriteView, '/')
expect(exec).toBe(true)
// Check POST request
expect(axios.post).toBeCalledTimes(1)
expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: [] })
// Check node change propagation
expect(file.attributes.favorite).toBe(0)
expect(eventBus.emit).toBeCalledTimes(2)
expect(eventBus.emit).toHaveBeenNthCalledWith(1, 'files:node:deleted', file)
expect(eventBus.emit).toHaveBeenNthCalledWith(2, 'files:favorites:removed', file)
})
test('Favorite does NOT triggers node removal if favorite view but NOT root dir', async () => {
jest.spyOn(axios, 'post')
jest.spyOn(eventBus, 'emit')
const file = new File({
id: 1,
source: 'http://localhost/remote.php/dav/files/admin/Foo/Bar/foobar.txt',
root: '/files/admin',
owner: 'admin',
mime: 'text/plain',
attributes: {
favorite: 1,
},
})
const exec = await action.exec(file, favoriteView, '/')
expect(exec).toBe(true)
// Check POST request
expect(axios.post).toBeCalledTimes(1)
expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/Foo/Bar/foobar.txt', { tags: [] })
// Check node change propagation
expect(file.attributes.favorite).toBe(0)
expect(eventBus.emit).toBeCalledTimes(1)
expect(eventBus.emit).toBeCalledWith('files:favorites:removed', file)
})
test('Favorite fails and show error', async () => {
const error = new Error('Mock error')
jest.spyOn(axios, 'post').mockImplementation(() => { throw new Error('Mock error') })
jest.spyOn(logger, 'error').mockImplementation(() => jest.fn())
const file = new File({
id: 1,
source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
attributes: {
favorite: 0,
},
})
const exec = await action.exec(file, view, '/')
expect(exec).toBe(false)
// Check POST request
expect(axios.post).toBeCalledTimes(1)
expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: ['_$!<Favorite>!$_'] })
// Check node change propagation
expect(logger.error).toBeCalledTimes(1)
expect(logger.error).toBeCalledWith('Error while adding a file to favourites', { error, source: file.source, node: file })
expect(file.attributes.favorite).toBe(0)
expect(eventBus.emit).toBeCalledTimes(0)
})
test('Removing from favorites fails and show error', async () => {
const error = new Error('Mock error')
jest.spyOn(axios, 'post').mockImplementation(() => { throw error })
jest.spyOn(logger, 'error').mockImplementation(() => jest.fn())
const file = new File({
id: 1,
source: 'http://localhost/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
attributes: {
favorite: 1,
},
})
const exec = await action.exec(file, view, '/')
expect(exec).toBe(false)
// Check POST request
expect(axios.post).toBeCalledTimes(1)
expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: [] })
// Check node change propagation
expect(logger.error).toBeCalledTimes(1)
expect(logger.error).toBeCalledWith('Error while removing a file from favourites', { error, source: file.source, node: file })
expect(file.attributes.favorite).toBe(1)
expect(eventBus.emit).toBeCalledTimes(0)
})
})
describe('Favorite action batch execute tests', () => {
test('Favorite action batch execute with mixed files', async () => {
jest.spyOn(favoriteAction, 'favoriteNode')
jest.spyOn(axios, 'post')
const file1 = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
attributes: {
favorite: 1,
},
})
const file2 = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
attributes: {
favorite: 0,
},
})
// Mixed states triggers favorite action
const exec = await action.execBatch!([file1, file2], view, '/')
expect(exec).toStrictEqual([true, true])
expect([file1, file2].every(file => file.attributes.favorite === 1)).toBe(true)
expect(favoriteAction.favoriteNode).toBeCalledTimes(2)
expect(axios.post).toBeCalledTimes(2)
expect(axios.post).toHaveBeenNthCalledWith(1, '/index.php/apps/files/api/v1/files/foo.txt', { tags: ['_$!<Favorite>!$_'] })
expect(axios.post).toHaveBeenNthCalledWith(2, '/index.php/apps/files/api/v1/files/bar.txt', { tags: ['_$!<Favorite>!$_'] })
})
test('Remove from favorite action batch execute with favorites only files', async () => {
jest.spyOn(favoriteAction, 'favoriteNode')
jest.spyOn(axios, 'post')
const file1 = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
attributes: {
favorite: 1,
},
})
const file2 = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
attributes: {
favorite: 1,
},
})
// Mixed states triggers favorite action
const exec = await action.execBatch!([file1, file2], view, '/')
expect(exec).toStrictEqual([true, true])
expect([file1, file2].every(file => file.attributes.favorite === 0)).toBe(true)
expect(favoriteAction.favoriteNode).toBeCalledTimes(2)
expect(axios.post).toBeCalledTimes(2)
expect(axios.post).toHaveBeenNthCalledWith(1, '/index.php/apps/files/api/v1/files/foo.txt', { tags: [] })
expect(axios.post).toHaveBeenNthCalledWith(2, '/index.php/apps/files/api/v1/files/bar.txt', { tags: [] })
})
})
|