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
|
/* eslint-disable import/no-named-as-default-member */
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { Folder as CFolder, Navigation } from '@nextcloud/files'
import * as filesUtils from '@nextcloud/files'
import { CancelablePromise } from 'cancelable-promise'
import { basename } from 'path'
import { beforeEach, describe, expect, test, vi } from 'vitest'
import * as eventBus from '@nextcloud/event-bus'
import { action } from '../actions/favoriteAction'
import * as favoritesService from '../services/Favorites'
import { registerFavoritesView } from './favorites'
const { Folder, getNavigation } = filesUtils
vi.mock('@nextcloud/axios')
window.OC = {
...window.OC,
TAG_FAVORITE: '_$!<Favorite>!$_',
}
declare global {
interface Window {
_nc_navigation?: Navigation
}
}
describe('Favorites view definition', () => {
let Navigation
beforeEach(() => {
vi.resetAllMocks()
delete window._nc_navigation
Navigation = getNavigation()
expect(window._nc_navigation).toBeDefined()
})
test('Default empty favorite view', async () => {
vi.spyOn(eventBus, 'subscribe')
vi.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
vi.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
await registerFavoritesView()
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
const favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
expect(eventBus.subscribe).toHaveBeenCalledTimes(3)
expect(eventBus.subscribe).toHaveBeenNthCalledWith(1, 'files:favorites:added', expect.anything())
expect(eventBus.subscribe).toHaveBeenNthCalledWith(2, 'files:favorites:removed', expect.anything())
expect(eventBus.subscribe).toHaveBeenNthCalledWith(3, 'files:node:renamed', expect.anything())
// one main view and no children
expect(Navigation.views.length).toBe(1)
expect(favoritesView).toBeDefined()
expect(favoriteFoldersViews.length).toBe(0)
expect(favoritesView?.id).toBe('favorites')
expect(favoritesView?.name).toBe('Favorites')
expect(favoritesView?.caption).toBeDefined()
expect(favoritesView?.icon).toMatch(/<svg.+<\/svg>/)
expect(favoritesView?.order).toBe(15)
expect(favoritesView?.columns).toStrictEqual([])
expect(favoritesView?.getContents).toBeDefined()
})
test('Default with favorites', async () => {
const favoriteFolders = [
new Folder({
id: 1,
root: '/files/admin',
source: 'http://nextcloud.local/remote.php/dav/files/admin/foo',
owner: 'admin',
}),
new Folder({
id: 2,
root: '/files/admin',
source: 'http://nextcloud.local/remote.php/dav/files/admin/bar',
owner: 'admin',
}),
new Folder({
id: 3,
root: '/files/admin',
source: 'http://nextcloud.local/remote.php/dav/files/admin/foo/bar',
owner: 'admin',
}),
]
vi.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve(favoriteFolders))
vi.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
await registerFavoritesView()
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
const favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
// one main view and 3 children
expect(Navigation.views.length).toBe(4)
expect(favoritesView).toBeDefined()
expect(favoriteFoldersViews.length).toBe(3)
favoriteFolders.forEach((folder, index) => {
const favoriteView = favoriteFoldersViews[index]
expect(favoriteView).toBeDefined()
expect(favoriteView?.id).toBeDefined()
expect(favoriteView?.name).toBe(basename(folder.path))
expect(favoriteView?.icon).toMatch(/<svg.+<\/svg>/)
expect(favoriteView?.order).toBe(index)
expect(favoriteView?.params).toStrictEqual({
dir: folder.path,
fileid: String(folder.fileid),
view: 'favorites',
})
expect(favoriteView?.parent).toBe('favorites')
expect(favoriteView?.columns).toStrictEqual([])
expect(favoriteView?.getContents).toBeDefined()
})
})
})
describe('Dynamic update of favorite folders', () => {
let Navigation
beforeEach(() => {
vi.restoreAllMocks()
delete window._nc_navigation
Navigation = getNavigation()
})
test('Add a favorite folder creates a new entry in the navigation', async () => {
vi.spyOn(eventBus, 'emit')
vi.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
vi.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
await registerFavoritesView()
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
const favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
// one main view and no children
expect(Navigation.views.length).toBe(1)
expect(favoritesView).toBeDefined()
expect(favoriteFoldersViews.length).toBe(0)
// Create new folder to favorite
const folder = new Folder({
id: 1,
source: 'http://nextcloud.local/remote.php/dav/files/admin/Foo/Bar',
owner: 'admin',
})
// Exec the action
await action.exec(folder, favoritesView, '/')
expect(eventBus.emit).toHaveBeenCalledTimes(1)
expect(eventBus.emit).toHaveBeenCalledWith('files:favorites:added', folder)
})
test('Remove a favorite folder remove the entry from the navigation column', async () => {
vi.spyOn(eventBus, 'emit')
vi.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([
new Folder({
id: 42,
root: '/files/admin',
source: 'http://nextcloud.local/remote.php/dav/files/admin/Foo/Bar',
owner: 'admin',
}),
]))
vi.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
await registerFavoritesView()
let favoritesView = Navigation.views.find(view => view.id === 'favorites')
let favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
// one main view and no children
expect(Navigation.views.length).toBe(2)
expect(favoritesView).toBeDefined()
expect(favoriteFoldersViews.length).toBe(1)
// Create new folder to favorite
const folder = new Folder({
id: 1,
source: 'http://nextcloud.local/remote.php/dav/files/admin/Foo/Bar',
owner: 'admin',
root: '/files/admin',
attributes: {
favorite: 1,
},
})
const fo = vi.fn()
eventBus.subscribe('files:favorites:removed', fo)
// Exec the action
await action.exec(folder, favoritesView, '/')
expect(eventBus.emit).toHaveBeenCalledTimes(1)
expect(eventBus.emit).toHaveBeenCalledWith('files:favorites:removed', folder)
expect(fo).toHaveBeenCalled()
favoritesView = Navigation.views.find(view => view.id === 'favorites')
favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
// one main view and no children
expect(Navigation.views.length).toBe(1)
expect(favoritesView).toBeDefined()
expect(favoriteFoldersViews.length).toBe(0)
})
test('Renaming a favorite folder updates the navigation', async () => {
vi.spyOn(eventBus, 'emit')
vi.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
vi.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
await registerFavoritesView()
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
const favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
// one main view and no children
expect(Navigation.views.length).toBe(1)
expect(favoritesView).toBeDefined()
expect(favoriteFoldersViews.length).toBe(0)
// expect(eventBus.emit).toHaveBeenCalledTimes(2)
// Create new folder to favorite
const folder = new Folder({
id: 1,
source: 'http://nextcloud.local/remote.php/dav/files/admin/Foo/Bar',
owner: 'admin',
})
// Exec the action
await action.exec(folder, favoritesView, '/')
expect(eventBus.emit).toHaveBeenNthCalledWith(1, 'files:favorites:added', folder)
// Create a folder with the same id but renamed
const renamedFolder = new Folder({
id: 1,
source: 'http://nextcloud.local/remote.php/dav/files/admin/Foo/Bar.renamed',
owner: 'admin',
})
// Exec the rename action
eventBus.emit('files:node:renamed', renamedFolder)
expect(eventBus.emit).toHaveBeenNthCalledWith(2, 'files:node:renamed', renamedFolder)
})
})
|