feat: add favorites view testing

Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
pull/38950/head
John Molakvoæ 2023-06-30 10:47:30 +07:00
parent 18f88a7042
commit 047218b5b0
No known key found for this signature in database
GPG Key ID: 60C25B8C072916CF
13 changed files with 249 additions and 19 deletions

@ -19,4 +19,4 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
*/ */
export default 'SvgMock' export default '<svg>SvgMock</svg>'

@ -0,0 +1,27 @@
/**
* @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
export const createClient = () => {}
export const getPatcher = () => {
return {
patch: () => {}
}
}

@ -43,7 +43,7 @@ describe('Delete action conditions tests', () => {
expect(action).toBeInstanceOf(FileAction) expect(action).toBeInstanceOf(FileAction)
expect(action.id).toBe('delete') expect(action.id).toBe('delete')
expect(action.displayName([], view)).toBe('Delete') expect(action.displayName([], view)).toBe('Delete')
expect(action.iconSvgInline([], view)).toBe('SvgMock') expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>')
expect(action.default).toBeUndefined() expect(action.default).toBeUndefined()
expect(action.order).toBe(100) expect(action.order).toBe(100)
}) })

@ -38,7 +38,7 @@ describe('Download action conditions tests', () => {
expect(action).toBeInstanceOf(FileAction) expect(action).toBeInstanceOf(FileAction)
expect(action.id).toBe('download') expect(action.id).toBe('download')
expect(action.displayName([], view)).toBe('Download') expect(action.displayName([], view)).toBe('Download')
expect(action.iconSvgInline([], view)).toBe('SvgMock') expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>')
expect(action.default).toBeUndefined() expect(action.default).toBeUndefined()
expect(action.order).toBe(30) expect(action.order).toBe(30)
}) })

@ -37,7 +37,7 @@ describe('Edit locally action conditions tests', () => {
expect(action).toBeInstanceOf(FileAction) expect(action).toBeInstanceOf(FileAction)
expect(action.id).toBe('edit-locally') expect(action.id).toBe('edit-locally')
expect(action.displayName([], view)).toBe('Edit locally') expect(action.displayName([], view)).toBe('Edit locally')
expect(action.iconSvgInline([], view)).toBe('SvgMock') expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>')
expect(action.default).toBe(DefaultType.DEFAULT) expect(action.default).toBe(DefaultType.DEFAULT)
expect(action.order).toBe(25) expect(action.order).toBe(25)
}) })

@ -55,7 +55,7 @@ describe('Favorite action conditions tests', () => {
expect(action).toBeInstanceOf(FileAction) expect(action).toBeInstanceOf(FileAction)
expect(action.id).toBe('favorite') expect(action.id).toBe('favorite')
expect(action.displayName([file], view)).toBe('Add to favorites') expect(action.displayName([file], view)).toBe('Add to favorites')
expect(action.iconSvgInline([], view)).toBe('SvgMock') expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>')
expect(action.default).toBeUndefined() expect(action.default).toBeUndefined()
expect(action.order).toBe(-50) expect(action.order).toBe(-50)
}) })

@ -42,7 +42,7 @@ describe('Open folder action conditions tests', () => {
expect(action).toBeInstanceOf(FileAction) expect(action).toBeInstanceOf(FileAction)
expect(action.id).toBe('open-folder') expect(action.id).toBe('open-folder')
expect(action.displayName([folder], view)).toBe('Open folder FooBar') expect(action.displayName([folder], view)).toBe('Open folder FooBar')
expect(action.iconSvgInline([], view)).toBe('SvgMock') expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>')
expect(action.default).toBe(DefaultType.HIDDEN) expect(action.default).toBe(DefaultType.HIDDEN)
expect(action.order).toBe(-100) expect(action.order).toBe(-100)
}) })

@ -36,7 +36,7 @@ describe('Rename action conditions tests', () => {
expect(action).toBeInstanceOf(FileAction) expect(action).toBeInstanceOf(FileAction)
expect(action.id).toBe('rename') expect(action.id).toBe('rename')
expect(action.displayName([], view)).toBe('Rename') expect(action.displayName([], view)).toBe('Rename')
expect(action.iconSvgInline([], view)).toBe('SvgMock') expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>')
expect(action.default).toBeUndefined() expect(action.default).toBeUndefined()
expect(action.order).toBe(10) expect(action.order).toBe(10)
}) })

@ -36,7 +36,7 @@ describe('Open sidebar action conditions tests', () => {
expect(action).toBeInstanceOf(FileAction) expect(action).toBeInstanceOf(FileAction)
expect(action.id).toBe('details') expect(action.id).toBe('details')
expect(action.displayName([], view)).toBe('Open details') expect(action.displayName([], view)).toBe('Open details')
expect(action.iconSvgInline([], view)).toBe('SvgMock') expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>')
expect(action.default).toBe(DefaultType.DEFAULT) expect(action.default).toBe(DefaultType.DEFAULT)
expect(action.order).toBe(-50) expect(action.order).toBe(-50)
}) })

@ -63,6 +63,8 @@ const defaultDavNamespaces = {
/** /**
* TODO: remove and move to @nextcloud/files * TODO: remove and move to @nextcloud/files
* @param prop
* @param namespace
*/ */
export const registerDavProperty = function(prop: string, namespace: DavProperty = { nc: 'http://nextcloud.org/ns' }): void { export const registerDavProperty = function(prop: string, namespace: DavProperty = { nc: 'http://nextcloud.org/ns' }): void {
if (typeof window._nc_dav_properties === 'undefined') { if (typeof window._nc_dav_properties === 'undefined') {

@ -25,7 +25,7 @@ import { getClient, rootPath } from './WebdavClient'
import { getCurrentUser } from '@nextcloud/auth' import { getCurrentUser } from '@nextcloud/auth'
import { getDavNameSpaces, getDavProperties, getDefaultPropfind } from './DavProperties' import { getDavNameSpaces, getDavProperties, getDefaultPropfind } from './DavProperties'
import type { ContentsWithRoot } from './Navigation' import type { ContentsWithRoot } from './Navigation'
import type { FileStat, ResponseDataDetailed } from 'webdav' import type { FileStat, ResponseDataDetailed, DAVResultResponseProps } from 'webdav'
const client = getClient() const client = getClient()
@ -39,23 +39,30 @@ const reportPayload = `<?xml version="1.0"?>
</oc:filter-rules> </oc:filter-rules>
</oc:filter-files>` </oc:filter-files>`
interface ResponseProps extends DAVResultResponseProps {
permissions: string,
fileid: number,
size: number,
}
const resultToNode = function(node: FileStat): File | Folder { const resultToNode = function(node: FileStat): File | Folder {
const permissions = parseWebdavPermissions(node.props?.permissions) const props = node.props as ResponseProps
const permissions = parseWebdavPermissions(props?.permissions)
const owner = getCurrentUser()?.uid as string const owner = getCurrentUser()?.uid as string
const previewUrl = generateUrl('/core/preview?fileId={fileid}&x=32&y=32&forceIcon=0', node.props) const previewUrl = generateUrl('/core/preview?fileId={fileid}&x=32&y=32&forceIcon=0', props)
const nodeData = { const nodeData = {
id: node.props?.fileid as number || 0, id: props?.fileid as number || 0,
source: generateRemoteUrl('dav' + rootPath + node.filename), source: generateRemoteUrl('dav' + rootPath + node.filename),
mtime: new Date(node.lastmod), mtime: new Date(node.lastmod),
mime: node.mime as string, mime: node.mime as string,
size: node.props?.size as number || 0, size: props?.size as number || 0,
permissions, permissions,
owner, owner,
root: rootPath, root: rootPath,
attributes: { attributes: {
...node, ...node,
...node.props, ...props,
previewUrl, previewUrl,
}, },
} }

@ -0,0 +1,193 @@
/**
* @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { expect } from '@jest/globals'
import * as initialState from '@nextcloud/initial-state'
import { Folder } from '@nextcloud/files'
import { basename } from 'path'
import * as eventBus from '@nextcloud/event-bus'
import { action } from '../actions/favoriteAction'
import * as favoritesService from '../services/Favorites'
import NavigationService from '../services/Navigation'
import registerFavoritesView from './favorites'
jest.mock('webdav/dist/node/request.js', () => ({
request: jest.fn(),
}))
global.window.OC = {
TAG_FAVORITE: '_$!<Favorite>!$_',
}
describe('Favorites view definition', () => {
let Navigation
beforeEach(() => {
Navigation = new NavigationService()
window.OCP = { Files: { Navigation } }
})
afterAll(() => {
delete window.OCP
})
test('Default empty favorite view', () => {
jest.spyOn(eventBus, 'subscribe')
jest.spyOn(favoritesService, 'getContents').mockReturnValue(Promise.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(2)
expect(eventBus.subscribe).toHaveBeenNthCalledWith(1, 'files:favorites:added', expect.anything())
expect(eventBus.subscribe).toHaveBeenNthCalledWith(2, 'files:favorites:removed', 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).toBe('List of favorites files and folders.')
expect(favoritesView?.icon).toBe('<svg>SvgMock</svg>')
expect(favoritesView?.order).toBe(5)
expect(favoritesView?.columns).toStrictEqual([])
expect(favoritesView?.getContents).toBeDefined()
})
test('Default with favorites', () => {
const favoriteFolders = [
'/foo',
'/bar',
'/foo/bar',
]
jest.spyOn(initialState, 'loadState').mockReturnValue(favoriteFolders)
jest.spyOn(favoritesService, 'getContents').mockReturnValue(Promise.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))
expect(favoriteView?.icon).toBe('<svg>SvgMock</svg>')
expect(favoriteView?.order).toBe(index)
expect(favoriteView?.params).toStrictEqual({
dir: folder,
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 = new NavigationService()
window.OCP = { Files: { Navigation } }
})
afterAll(() => {
delete window.OCP
})
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(Promise.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(['/Foo/Bar'])
jest.spyOn(favoritesService, 'getContents').mockReturnValue(Promise.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)
})
})

@ -33,7 +33,7 @@ import { Node, FileType } from '@nextcloud/files'
import { subscribe } from '@nextcloud/event-bus' import { subscribe } from '@nextcloud/event-bus'
import logger from '../logger' import logger from '../logger'
const generateFolderView = function(folder: string, index = 0): Navigation { export const generateFolderView = function(folder: string, index = 0): Navigation {
return { return {
id: generateIdFromPath(folder), id: generateIdFromPath(folder),
name: basename(folder), name: basename(folder),
@ -53,14 +53,15 @@ const generateFolderView = function(folder: string, index = 0): Navigation {
} as Navigation } as Navigation
} }
const generateIdFromPath = function(path: string): string { export const generateIdFromPath = function(path: string): string {
return `favorite-${hashCode(path)}` return `favorite-${hashCode(path)}`
} }
const favoriteFolders = loadState('files', 'favoriteFolders', []) as string[]
const favoriteFoldersViews = favoriteFolders.map((folder, index) => generateFolderView(folder, index))
export default () => { export default () => {
// Load state in function for mock testing purposes
const favoriteFolders = loadState('files', 'favoriteFolders', []) as string[]
const favoriteFoldersViews = favoriteFolders.map((folder, index) => generateFolderView(folder, index))
const Navigation = window.OCP.Files.Navigation as NavigationService const Navigation = window.OCP.Files.Navigation as NavigationService
Navigation.register({ Navigation.register({
id: 'favorites', id: 'favorites',