aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/views/favorites.spec.ts
blob: dfced698cf1283e80f571d9d4e2a51dbabee2cef (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
217
218
219
220
221
222
223
/* 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 { basename } from 'path'
import { expect } from '@jest/globals'
import { Folder, Navigation, getNavigation } from '@nextcloud/files'
import { CancelablePromise } from 'cancelable-promise'
import eventBus, { emit } from '@nextcloud/event-bus'
import * as initialState from '@nextcloud/initial-state'

import { action } from '../actions/favoriteAction'
import * as favoritesService from '../services/Favorites'
import registerFavoritesView from './favorites'

jest.mock('webdav/dist/node/request.js', () => ({
	request: jest.fn(),
}))

global.window.OC = {
	TAG_FAVORITE: '_$!<Favorite>!$_',
}

declare global {
	interface Window {
		_nc_navigation?: Navigation
	}
}

describe('Favorites view definition', () => {
	let Navigation
	beforeEach(() => {
		Navigation = getNavigation()
		expect(window._nc_navigation).toBeDefined()
	})

	afterEach(() => {
		delete window._nc_navigation
	})

	test('Default empty favorite view', () => {
		jest.spyOn(eventBus, 'subscribe')
		jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))

		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).toBe('<svg>SvgMock</svg>')
		expect(favoritesView?.order).toBe(15)
		expect(favoritesView?.columns).toStrictEqual([])
		expect(favoritesView?.getContents).toBeDefined()
	})

	test('Default with favorites', () => {
		const favoriteFolders = [
			{ fileid: 1, path: '/foo' },
			{ fileid: 2, path: '/bar' },
			{ fileid: 3, path: '/foo/bar' },
		]
		jest.spyOn(initialState, 'loadState').mockReturnValue(favoriteFolders)
		jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))

		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).toBe('<svg>SvgMock</svg>')
			expect(favoriteView?.order).toBe(index)
			expect(favoriteView?.params).toStrictEqual({
				dir: folder.path,
				fileid: folder.fileid.toString(),
				view: 'favorites',
			})
			expect(favoriteView?.parent).toBe('favorites')
			expect(favoriteView?.columns).toStrictEqual([])
			expect(favoriteView?.getContents).toBeDefined()
		})
	})
})

describe('Dynamic update of favourite folders', () => {
	let Navigation
	beforeEach(() => {
		Navigation = getNavigation()
	})

	afterEach(() => {
		delete window._nc_navigation
	})

	test('Add a favorite folder creates a new entry in the navigation', async () => {
		jest.spyOn(eventBus, 'emit')
		jest.spyOn(initialState, 'loadState').mockReturnValue([])
		jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))

		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://localhost/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 () => {
		jest.spyOn(eventBus, 'emit')
		jest.spyOn(eventBus, 'subscribe')
		jest.spyOn(initialState, 'loadState').mockReturnValue([{ fileid: 42, path: '/Foo/Bar' }])
		jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))

		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://localhost/remote.php/dav/files/admin/Foo/Bar',
			owner: 'admin',
			root: '/files/admin',
			attributes: {
				favorite: 1,
			},
		})

		// Exec the action
		await action.exec(folder, favoritesView, '/')

		expect(eventBus.emit).toHaveBeenCalledTimes(1)
		expect(eventBus.emit).toHaveBeenCalledWith('files:favorites:removed', folder)

		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 () => {
		jest.spyOn(eventBus, 'emit')
		jest.spyOn(initialState, 'loadState').mockReturnValue([])
		jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))

		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://localhost/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://localhost/remote.php/dav/files/admin/Foo/Bar.renamed',
			owner: 'admin',
		})

		// Exec the rename action
		emit('files:node:renamed', renamedFolder)
		expect(eventBus.emit).toHaveBeenNthCalledWith(2, 'files:node:renamed', renamedFolder)
	})
})