Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow registering actions for a file listing #1097

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions __tests__/fileListAction.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { beforeEach, describe, expect, test, vi } from 'vitest'

import type { View } from '../lib/navigation/view.ts'

import { getFileListActions, registerFileListAction, FileListAction } from '../lib/fileListAction.ts'
import logger from '../lib/utils/logger.ts'

const mockAction = (id: string) => new FileListAction({
id,
displayName: () => 'Test',
iconSvgInline: () => '<svg></svg>',
order: 0,
exec: async () => {},
})

describe('FileListActions init', () => {
beforeEach(() => {
delete window._nc_filelistactions
})

test('Uninitialized file list actions', () => {
expect(window._nc_filelistactions).toBe(undefined)
const actions = getFileListActions()
expect(actions).toHaveLength(0)
})

test('Register a single file list action', () => {
const actions = getFileListActions()
expect(actions).toHaveLength(0)

const testAction = mockAction('test')

expect(testAction.id).toBe('test')
expect(testAction.displayName({} as unknown as View)).toBe('Test')
expect(testAction.iconSvgInline({} as unknown as View)).toBe('<svg></svg>')

registerFileListAction(testAction)
expect(actions).toHaveLength(1)
expect(actions.at(0)).toStrictEqual(testAction)
})

test('Register multiple file list actions', () => {
const actions = getFileListActions()
expect(actions).toHaveLength(0)

const fooAction = mockAction('foo')
const barAction = mockAction('bar')
const bazAction = mockAction('baz')

registerFileListAction(fooAction)
registerFileListAction(barAction)
registerFileListAction(bazAction)
expect(actions).toHaveLength(3)

expect(actions.find(action => action.id === 'foo')).toStrictEqual(fooAction)
expect(actions.find(action => action.id === 'bar')).toStrictEqual(barAction)
expect(actions.find(action => action.id === 'baz')).toStrictEqual(bazAction)
})

test('Register an action with a duplicate id', () => {
logger.error = vi.fn()

const actions = getFileListActions()
expect(actions).toHaveLength(0)

const testActionA = mockAction('test')
const testActionB = mockAction('test')

registerFileListAction(testActionA)
expect(actions).toHaveLength(1)

registerFileListAction(testActionB)
expect(logger.error).toHaveBeenCalledWith('FileListAction with id "test" is already registered', { action: testActionB })
expect(actions).toHaveLength(1)
})
})

describe('Invalid FileListAction creation', () => {
test('Invalid id', () => {
expect(() => new FileListAction({
displayName: () => 'Test',
iconSvgInline: () => '<svg></svg>',
order: 0,
exec: async () => {},
} as unknown as FileListAction),
).toThrowError('Invalid id')
})
test('Invalid displayName', () => {
expect(() => new FileListAction({
id: 'test',
iconSvgInline: () => '<svg></svg>',
order: 0,
exec: async () => {},
} as unknown as FileListAction),
).toThrowError('Invalid displayName function')
})
test('Invalid iconSvgInline', () => {
expect(() => new FileListAction({
id: 'test',
displayName: () => 'Test',
order: 0,
exec: async () => {},
} as unknown as FileListAction),
).toThrowError('Invalid iconSvgInline function')
})
test('Invalid order', () => {
expect(() => new FileListAction({
id: 'test',
displayName: () => 'Test',
iconSvgInline: () => '<svg></svg>',
order: null,
exec: async () => {},
} as unknown as FileListAction),
).toThrowError('Invalid order')
})
test('Invalid enabled', () => {
expect(() => new FileListAction({
id: 'test',
displayName: () => 'Test',
iconSvgInline: () => '<svg></svg>',
order: 0,
enabled: null,
exec: async () => {},
} as unknown as FileListAction),
).toThrowError('Invalid enabled function')
})
test('Invalid exec', () => {
expect(() => new FileListAction({
id: 'test',
displayName: () => 'Test',
iconSvgInline: () => '<svg></svg>',
exec: null,
} as unknown as FileListAction),
).toThrowError('Invalid exec function')
})
})

describe('FileListAction creation', () => {
test('Create a FileListAction', async () => {
const testAction = new FileListAction({
id: 'test',
displayName: () => 'Test',
iconSvgInline: () => '<svg></svg>',
order: 0,
enabled: () => true,
exec: async () => {},
})

expect(testAction.id).toBe('test')
expect(testAction.displayName({} as unknown as View)).toBe('Test')
expect(testAction.iconSvgInline({} as unknown as View)).toBe('<svg></svg>')
expect(testAction.order).toBe(0)
expect(testAction.enabled?.({} as unknown as View, [])).toBe(true)
await expect(testAction.exec({} as unknown as View, [])).resolves.toBe(undefined)
})
})
189 changes: 189 additions & 0 deletions __tests__/view.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { describe, expect, test } from 'vitest'

import { View } from '../lib/navigation/view.ts'
import { Folder } from '../lib/index.ts'

describe('Invalid View creation', () => {
test('Invalid id', () => {
expect(() => new View({
name: 'Test',
order: 1,
icon: '<svg></svg>',
getContents: () => Promise.reject(new Error()),
} as unknown as View),
).toThrowError('View id is required and must be a string')
})
test('Invalid name', () => {
expect(() => new View({
id: 'test',
order: 1,
icon: '<svg></svg>',
getContents: () => Promise.reject(new Error()),
} as unknown as View),
).toThrowError('View name is required and must be a string')
})
test('Invalid caption', () => {
expect(() => new View({
id: 'test',
name: 'Test',
order: 1,
caption: null,
icon: '<svg></svg>',
getContents: () => Promise.reject(new Error()),
} as unknown as View),
).toThrowError('View caption must be a string')
})
test('Invalid getContents', () => {
expect(() => new View({
id: 'test',
name: 'Test',
order: 1,
icon: '<svg></svg>',
getContents: null,
} as unknown as View),
).toThrowError('View getContents is required and must be a function')
})
test('Invalid icon', () => {
expect(() => new View({
id: 'test',
name: 'Test',
order: 1,
icon: '',
getContents: () => Promise.reject(new Error()),
} as unknown as View),
).toThrowError('View icon is required and must be a valid svg string')
})
test('Invalid order', () => {
expect(() => new View({
id: 'test',
name: 'Test',
order: null,
icon: '<svg></svg>',
getContents: () => Promise.reject(new Error()),
} as unknown as View),
).toThrowError('View order must be a number')
})
test('Invalid columns', () => {
expect(() => new View({
id: 'test',
name: 'Test',
order: 1,
icon: '<svg></svg>',
getContents: () => Promise.reject(new Error()),
columns: [null],
} as unknown as View),
).toThrowError('View columns must be an array of Column. Invalid column found')
})
test('Invalid emptyView', () => {
expect(() => new View({
id: 'test',
name: 'Test',
order: 1,
icon: '<svg></svg>',
getContents: () => Promise.reject(new Error()),
emptyView: true,
} as unknown as View),
).toThrowError('View emptyView must be a function')
})
test('Invalid parent', () => {
expect(() => new View({
id: 'test',
name: 'Test',
order: 1,
icon: '<svg></svg>',
getContents: () => Promise.reject(new Error()),
parent: 1,
} as unknown as View),
).toThrowError('View parent must be a string')
})
test('Invalid sticky', () => {
expect(() => new View({
id: 'test',
name: 'Test',
order: 1,
icon: '<svg></svg>',
getContents: () => Promise.reject(new Error()),
sticky: null,
} as unknown as View),
).toThrowError('View sticky must be a boolean')
})
test('Invalid expanded', () => {
expect(() => new View({
id: 'test',
name: 'Test',
order: 1,
icon: '<svg></svg>',
getContents: () => Promise.reject(new Error()),
expanded: null,
} as unknown as View),
).toThrowError('View expanded must be a boolean')
})
test('Invalid defaultSortKey', () => {
expect(() => new View({
id: 'test',
name: 'Test',
order: 1,
icon: '<svg></svg>',
getContents: () => Promise.reject(new Error()),
defaultSortKey: 1,
} as unknown as View),
).toThrowError('View defaultSortKey must be a string')
})
test('Invalid loadChildViews', () => {
expect(() => new View({
id: 'test',
name: 'Test',
order: 1,
icon: '<svg></svg>',
getContents: () => Promise.reject(new Error()),
loadChildViews: true,
} as unknown as View),
).toThrowError('View loadChildViews must be a function')
})
})

describe('View creation', () => {
test('Create a View', async () => {
const folder = new Folder({ source: 'https://example.org', owner: 'admin' })
const view = new View({
id: 'test',
name: 'Test',
caption: 'Test caption',
emptyTitle: 'Test empty title',
emptyCaption: 'Test empty caption',
getContents: () => Promise.resolve({ folder, contents: [] }),
icon: '<svg></svg>',
order: 1,
params: {},
columns: [],
emptyView: () => {},
parent: 'parent',
sticky: false,
expanded: false,
defaultSortKey: 'key',
loadChildViews: async () => {},
})

expect(view.id).toBe('test')
expect(view.name).toBe('Test')
expect(view.caption).toBe('Test caption')
expect(view.emptyTitle).toBe('Test empty title')
expect(view.emptyCaption).toBe('Test empty caption')
await expect(view.getContents('/')).resolves.toStrictEqual({ folder, contents: [] })
expect(view.icon).toBe('<svg></svg>')
expect(view.order).toBe(1)
expect(view.params).toStrictEqual({})
expect(view.columns).toStrictEqual([])
expect(view.emptyView?.({} as unknown as HTMLDivElement)).toBe(undefined)
expect(view.parent).toBe('parent')
expect(view.sticky).toBe(false)
expect(view.expanded).toBe(false)
expect(view.defaultSortKey).toBe('key')
await expect(view.loadChildViews?.({} as unknown as View)).resolves.toBe(undefined)
})
})
Loading
Loading