aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/views/favorites.spec.ts
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files/src/views/favorites.spec.ts')
-rw-r--r--apps/files/src/views/favorites.spec.ts172
1 files changed, 125 insertions, 47 deletions
diff --git a/apps/files/src/views/favorites.spec.ts b/apps/files/src/views/favorites.spec.ts
index 7dbb0dbc551..f793eb9f54c 100644
--- a/apps/files/src/views/favorites.spec.ts
+++ b/apps/files/src/views/favorites.spec.ts
@@ -3,22 +3,27 @@
* 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 type { Folder as CFolder, Navigation } from '@nextcloud/files'
+
+import * as filesUtils from '@nextcloud/files'
+import * as filesDavUtils from '@nextcloud/files/dav'
import { CancelablePromise } from 'cancelable-promise'
-import eventBus from '@nextcloud/event-bus'
-import * as initialState from '@nextcloud/initial-state'
+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'
+import { registerFavoritesView } from './favorites'
+
+// eslint-disable-next-line import/namespace
+const { Folder, getNavigation } = filesUtils
-jest.mock('webdav/dist/node/request.js', () => ({
- request: jest.fn(),
-}))
+vi.mock('@nextcloud/axios')
-global.window.OC = {
+window.OC = {
+ ...window.OC,
TAG_FAVORITE: '_$!<Favorite>!$_',
}
@@ -31,25 +36,26 @@ declare global {
describe('Favorites view definition', () => {
let Navigation
beforeEach(() => {
- Navigation = getNavigation()
- expect(window._nc_navigation).toBeDefined()
- })
+ vi.resetAllMocks()
- afterEach(() => {
delete window._nc_navigation
+ Navigation = getNavigation()
+ expect(window._nc_navigation).toBeDefined()
})
- test('Default empty favorite view', () => {
- jest.spyOn(eventBus, 'subscribe')
- jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))
+ test('Default empty favorite view', async () => {
+ vi.spyOn(eventBus, 'subscribe')
+ vi.spyOn(filesDavUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
+ vi.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
- registerFavoritesView()
+ await registerFavoritesView()
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
const favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
- expect(eventBus.subscribe).toHaveBeenCalledTimes(2)
+ 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)
@@ -59,40 +65,64 @@ describe('Favorites view definition', () => {
expect(favoritesView?.id).toBe('favorites')
expect(favoritesView?.name).toBe('Favorites')
expect(favoritesView?.caption).toBeDefined()
- expect(favoritesView?.icon).toBe('<svg>SvgMock</svg>')
+ expect(favoritesView?.icon).toMatch(/<svg.+<\/svg>/)
expect(favoritesView?.order).toBe(15)
expect(favoritesView?.columns).toStrictEqual([])
expect(favoritesView?.getContents).toBeDefined()
})
- test('Default with favorites', () => {
+ test('Default with favorites', async () => {
const favoriteFolders = [
- { fileid: 1, path: '/foo' },
- { fileid: 2, path: '/bar' },
- { fileid: 3, path: '/foo/bar' },
+ 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',
+ }),
+ new Folder({
+ id: 4,
+ root: '/files/admin',
+ source: 'http://nextcloud.local/remote.php/dav/files/admin/foo/bar/yabadaba',
+ owner: 'admin',
+ }),
]
- jest.spyOn(initialState, 'loadState').mockReturnValue(favoriteFolders)
- jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))
+ vi.spyOn(filesDavUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve(favoriteFolders))
+ vi.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
- registerFavoritesView()
+ 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(Navigation.views.length).toBe(5)
expect(favoritesView).toBeDefined()
- expect(favoriteFoldersViews.length).toBe(3)
+ expect(favoriteFoldersViews.length).toBe(4)
+
+ // Sorted by basename: bar, bar, foo
+ const expectedOrder = [2, 0, 1, 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?.icon).toMatch(/<svg.+<\/svg>/)
+ expect(favoriteView?.order).toBe(expectedOrder[index])
expect(favoriteView?.params).toStrictEqual({
dir: folder.path,
- fileid: folder.fileid.toString(),
+ fileid: String(folder.fileid),
view: 'favorites',
})
expect(favoriteView?.parent).toBe('favorites')
@@ -102,22 +132,21 @@ describe('Favorites view definition', () => {
})
})
-describe('Dynamic update of favourite folders', () => {
+describe('Dynamic update of favorite folders', () => {
let Navigation
beforeEach(() => {
- Navigation = getNavigation()
- })
+ vi.restoreAllMocks()
- afterEach(() => {
delete window._nc_navigation
+ Navigation = getNavigation()
})
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: [] }))
+ vi.spyOn(eventBus, 'emit')
+ vi.spyOn(filesDavUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
+ vi.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
- registerFavoritesView()
+ await registerFavoritesView()
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
const favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
@@ -129,7 +158,7 @@ describe('Dynamic update of favourite folders', () => {
// Create new folder to favorite
const folder = new Folder({
id: 1,
- source: 'http://localhost/remote.php/dav/files/admin/Foo/Bar',
+ source: 'http://nextcloud.local/remote.php/dav/files/admin/Foo/Bar',
owner: 'admin',
})
@@ -141,12 +170,18 @@ describe('Dynamic update of favourite folders', () => {
})
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: [] }))
+ vi.spyOn(eventBus, 'emit')
+ vi.spyOn(filesDavUtils, '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: [] }))
- registerFavoritesView()
+ await registerFavoritesView()
let favoritesView = Navigation.views.find(view => view.id === 'favorites')
let favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
@@ -158,7 +193,7 @@ describe('Dynamic update of favourite folders', () => {
// Create new folder to favorite
const folder = new Folder({
id: 1,
- source: 'http://localhost/remote.php/dav/files/admin/Foo/Bar',
+ source: 'http://nextcloud.local/remote.php/dav/files/admin/Foo/Bar',
owner: 'admin',
root: '/files/admin',
attributes: {
@@ -166,11 +201,15 @@ describe('Dynamic update of favourite folders', () => {
},
})
+ 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')
@@ -180,4 +219,43 @@ describe('Dynamic update of favourite folders', () => {
expect(favoritesView).toBeDefined()
expect(favoriteFoldersViews.length).toBe(0)
})
+
+ test('Renaming a favorite folder updates the navigation', async () => {
+ vi.spyOn(eventBus, 'emit')
+ vi.spyOn(filesDavUtils, '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)
+ })
})