Skip to content
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
5 changes: 3 additions & 2 deletions __tests__/actions/fileAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import type { Folder, Node } from '../../lib/node/index.ts'

import { beforeEach, describe, expect, test, vi } from 'vitest'
import { DefaultType, getFileActions, registerFileAction } from '../../lib/actions/index.ts'
import { scopedGlobals } from '../../lib/globalScope.ts'
import { getRegistry } from '../../lib/registry.ts'
import logger from '../../lib/utils/logger.ts'

const folder = {} as Folder
const view = {} as View
describe('FileActions init', () => {
beforeEach(() => {
delete window._nc_fileactions
delete scopedGlobals.fileActions
})

test('Getting empty uninitialized FileActions', () => {
Expand All @@ -39,7 +40,7 @@ describe('FileActions init', () => {

registerFileAction(action)

expect(window._nc_fileactions).toHaveLength(1)
expect(scopedGlobals.fileActions).toHaveLength(1)
expect(getFileActions()).toHaveLength(1)
expect(getFileActions()[0]).toStrictEqual(action)
})
Expand Down
5 changes: 3 additions & 2 deletions __tests__/actions/fileListAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { Folder } from '../../lib/node/index.ts'

import { beforeEach, describe, expect, test, vi } from 'vitest'
import { getFileListActions, registerFileListAction } from '../../lib/actions/fileListAction.ts'
import { scopedGlobals } from '../../lib/globalScope.ts'
import { getRegistry } from '../../lib/registry.ts'
import logger from '../../lib/utils/logger.ts'

Expand All @@ -28,11 +29,11 @@ function mockAction(id: string): IFileListAction {

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

test('Uninitialized file list actions', () => {
expect(window._nc_filelistactions).toBe(undefined)
expect(scopedGlobals.fileListActions).toBe(undefined)
const actions = getFileListActions()
expect(actions).toHaveLength(0)
})
Expand Down
25 changes: 13 additions & 12 deletions __tests__/dav/davProperties.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@ import {
getRecentSearch,
registerDavProperty,
} from '../../lib/dav/davProperties.ts'
import { scopedGlobals } from '../../lib/globalScope.ts'
import logger from '../../lib/utils/logger.ts'

describe('DAV Properties', () => {
beforeEach(() => {
delete window._nc_dav_properties
delete window._nc_dav_namespaces
delete scopedGlobals.davNamespaces
delete scopedGlobals.davProperties

logger.error = vi.fn()
logger.warn = vi.fn()
})

test('getDavNameSpaces fall back to defaults', () => {
expect(window._nc_dav_namespaces).toBeUndefined()
expect(scopedGlobals.davNamespaces).toBeUndefined()
const namespace = getDavNameSpaces()
expect(namespace).toBeTruthy()
Object.keys(defaultDavNamespaces).forEach((n) => expect(namespace.includes(n) && namespace.includes(defaultDavNamespaces[n])).toBe(true))
})

test('getDavProperties fall back to defaults', () => {
expect(window._nc_dav_properties).toBeUndefined()
expect(scopedGlobals.davProperties).toBeUndefined()
const props = getDavProperties()
expect(props).toBeTruthy()
defaultDavProperties.forEach((p) => expect(props.includes(p)).toBe(true))
Expand All @@ -56,8 +57,8 @@ describe('DAV Properties', () => {
})

test('registerDavProperty registers successfully', () => {
expect(window._nc_dav_namespaces).toBeUndefined()
expect(window._nc_dav_properties).toBeUndefined()
expect(scopedGlobals.davNamespaces).toBeUndefined()
expect(scopedGlobals.davProperties).toBeUndefined()

expect(registerDavProperty('my:prop', { my: 'https://example.com/ns' })).toBe(true)
expect(logger.warn).not.toBeCalled()
Expand All @@ -67,8 +68,8 @@ describe('DAV Properties', () => {
})

test('registerDavProperty fails when registered multiple times', () => {
expect(window._nc_dav_namespaces).toBeUndefined()
expect(window._nc_dav_properties).toBeUndefined()
expect(scopedGlobals.davNamespaces).toBeUndefined()
expect(scopedGlobals.davProperties).toBeUndefined()

expect(registerDavProperty('my:prop', { my: 'https://example.com/ns' })).toBe(true)
expect(registerDavProperty('my:prop')).toBe(false)
Expand All @@ -80,8 +81,8 @@ describe('DAV Properties', () => {
})

test('registerDavProperty fails with invalid props', () => {
expect(window._nc_dav_namespaces).toBeUndefined()
expect(window._nc_dav_properties).toBeUndefined()
expect(scopedGlobals.davNamespaces).toBeUndefined()
expect(scopedGlobals.davProperties).toBeUndefined()

expect(registerDavProperty('my:prop:invalid', { my: 'https://example.com/ns' })).toBe(false)
expect(logger.error).toBeCalled()
Expand All @@ -95,8 +96,8 @@ describe('DAV Properties', () => {
})

test('registerDavProperty fails with missing namespace', () => {
expect(window._nc_dav_namespaces).toBeUndefined()
expect(window._nc_dav_properties).toBeUndefined()
expect(scopedGlobals.davNamespaces).toBeUndefined()
expect(scopedGlobals.davProperties).toBeUndefined()

expect(registerDavProperty('my:prop', { other: 'https://example.com/ns' })).toBe(false)
expect(logger.error).toBeCalled()
Expand Down
23 changes: 12 additions & 11 deletions __tests__/filters/listFilter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
Expand All @@ -7,6 +7,7 @@ import type { IFileListFilterChip } from '../../lib/filters/index.ts'

import { beforeEach, describe, expect, test, vi } from 'vitest'
import { FileListFilter, getFileListFilters, registerFileListFilter, unregisterFileListFilter } from '../../lib/filters/index.ts'
import { scopedGlobals } from '../../lib/globalScope.ts'
import { getRegistry } from '../../lib/registry.ts'

class TestFilter extends FileListFilter {
Expand Down Expand Up @@ -83,16 +84,16 @@ describe('File list filter class', () => {

describe('File list filter functions', () => {
beforeEach(() => {
delete window._nc_filelist_filters
delete scopedGlobals.fileListFilters
})

test('can register a filter', () => {
const filter = new FileListFilter('my:id', 50)

registerFileListFilter(filter)
expect(window._nc_filelist_filters).toBeTypeOf('object')
expect(window._nc_filelist_filters!.has(filter.id)).toBe(true)
expect(window._nc_filelist_filters!.get(filter.id)).toBe(filter)
expect(scopedGlobals.fileListFilters).toBeTypeOf('object')
expect(scopedGlobals.fileListFilters!.has(filter.id)).toBe(true)
expect(scopedGlobals.fileListFilters!.get(filter.id)).toBe(filter)
})

test('register a filter emits event', () => {
Expand All @@ -101,7 +102,7 @@ describe('File list filter functions', () => {

getRegistry().addEventListener('register:listFilter', spy)

expect(window._nc_filelist_filters).toBe(undefined)
expect(scopedGlobals.fileListFilters).toBe(undefined)

registerFileListFilter(filter)
expect(spy).toHaveBeenCalled()
Expand All @@ -121,22 +122,22 @@ describe('File list filter functions', () => {
const filter = new FileListFilter('my:id')

registerFileListFilter(filter)
expect(window._nc_filelist_filters!.has(filter.id)).toBe(true)
expect(scopedGlobals.fileListFilters!.has(filter.id)).toBe(true)

// test
unregisterFileListFilter(filter.id)
expect(window._nc_filelist_filters!.has(filter.id)).toBe(false)
expect(scopedGlobals.fileListFilters!.has(filter.id)).toBe(false)
})

test('unregister a filter twice does not throw', () => {
const filter = new FileListFilter('my:id')

registerFileListFilter(filter)
expect(window._nc_filelist_filters!.has(filter.id)).toBe(true)
expect(scopedGlobals.fileListFilters!.has(filter.id)).toBe(true)

// test
unregisterFileListFilter(filter.id)
expect(window._nc_filelist_filters!.has(filter.id)).toBe(false)
expect(scopedGlobals.fileListFilters!.has(filter.id)).toBe(false)
expect(() => unregisterFileListFilter(filter.id)).not.toThrow()
})

Expand All @@ -150,7 +151,7 @@ describe('File list filter functions', () => {
unregisterFileListFilter(filter.id)
expect(spy).toHaveBeenCalled()
expect(spy.mock.calls[0][0]).toBeInstanceOf(CustomEvent)
expect(spy.mock.calls[0][0].detail).toBe(filter)
expect(spy.mock.calls[0][0].detail).toBe(filter.id)
})

test('can get registered filters', () => {
Expand Down
33 changes: 24 additions & 9 deletions __tests__/headers/listHeaders.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
/**
/*
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { IFileListHeader, IFolder, IView } from '../../lib/index.ts'

import { beforeEach, describe, expect, test, vi } from 'vitest'
import { scopedGlobals } from '../../lib/globalScope.ts'
import { getFileListHeaders, registerFileListHeader } from '../../lib/headers/index.ts'
import { getRegistry } from '../../lib/registry.ts'
import logger from '../../lib/utils/logger.ts'

describe('FileListHeader init', () => {
beforeEach(() => {
delete window._nc_filelistheader
delete scopedGlobals.fileListHeaders
})

test('Getting empty uninitialized FileListHeader', () => {
const headers = getFileListHeaders()
expect(window._nc_filelistheader).toBeDefined()
expect(Array.isArray(headers)).toBe(true)
expect(headers).toHaveLength(0)
})

Expand All @@ -29,14 +30,9 @@ describe('FileListHeader init', () => {
render: () => {},
updated: () => {},
}

expect(header.id).toBe('test')
expect(header.order).toBe(1)
expect(header.enabled!({} as IFolder, {} as IView)).toBe(true)

registerFileListHeader(header)

expect(window._nc_filelistheader).toHaveLength(1)
expect(scopedGlobals.fileListHeaders).toHaveLength(1)
expect(getFileListHeaders()).toHaveLength(1)
expect(getFileListHeaders()[0]).toStrictEqual(header)
})
Expand All @@ -60,6 +56,25 @@ describe('FileListHeader init', () => {
expect(callback.mock.calls[0][0].detail).toBe(header)
})

test('getFileListHeaders() returns array', () => {
expect(getFileListHeaders()).toHaveLength(0)

const header: IFileListHeader = {
id: 'test',
order: 1,
enabled: () => true,
render: () => {},
updated: () => {},
}

registerFileListHeader(header)

const headers = getFileListHeaders()
expect(Array.isArray(headers)).toBe(true)
expect(headers).toHaveLength(1)
expect(headers[0]).toStrictEqual(header)
})

test('Duplicate Header gets rejected', () => {
logger.error = vi.fn()
const header: IFileListHeader = {
Expand Down
38 changes: 0 additions & 38 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { NewMenuEntry } from '../lib/newMenu/NewMenu.ts'

import { describe, expect, test } from 'vitest'
import { getFileActions, registerFileAction } from '../lib/actions/fileAction.ts'
import {
Expand All @@ -18,7 +16,6 @@ import {
Permission,
removeNewFileMenuEntry,
} from '../lib/index.ts'
import { NewMenu } from '../lib/newMenu/NewMenu.ts'

describe('Exports checks', () => {
test('formatFileSize', () => {
Expand Down Expand Up @@ -76,38 +73,3 @@ describe('Exports checks', () => {
expect(typeof Node).toBe('function')
})
})

describe('NewFileMenu methods', () => {
const entry = {
id: 'empty-file',
displayName: 'Create empty file',
templateName: 'New file.txt',
iconSvgInline: '<svg></svg>',
handler: () => {},
} as NewMenuEntry

test('Init NewFileMenu', () => {
expect(window._nc_newfilemenu).toBeUndefined()

const menuEntries = getNewFileMenuEntries()
expect(menuEntries).toHaveLength(0)

expect(window._nc_newfilemenu).toBeDefined()
expect(window._nc_newfilemenu).toBeInstanceOf(NewMenu)
})

test('Use existing initialized NewMenu', () => {
expect(window._nc_newfilemenu).toBeDefined()
expect(window._nc_newfilemenu).toBeInstanceOf(NewMenu)

addNewFileMenuEntry(entry)

expect(window._nc_newfilemenu).toBeDefined()
expect(window._nc_newfilemenu).toBeInstanceOf(NewMenu)

removeNewFileMenuEntry(entry)

expect(window._nc_newfilemenu).toBeDefined()
expect(window._nc_newfilemenu).toBeInstanceOf(NewMenu)
})
})
15 changes: 5 additions & 10 deletions __tests__/navigation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/**
/*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { describe, expect, it, vi } from 'vitest'
import { scopedGlobals } from '../lib/globalScope.ts'
import { View } from '../lib/index.ts'
import { getNavigation, Navigation } from '../lib/navigation/navigation.ts'
import { mockView } from './fixtures/view.ts'
Expand All @@ -18,21 +19,15 @@ describe('getNavigation', () => {
})

it('stores the navigation globally', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
delete window._nc_navigation
delete scopedGlobals.navigation
const navigation = getNavigation()
expect(navigation).toBeInstanceOf(Navigation)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(window._nc_navigation).toBeInstanceOf(Navigation)
expect(scopedGlobals.navigation).toBeInstanceOf(Navigation)
})

it('reuses an existing navigation', () => {
const navigation = new Navigation()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window._nc_navigation = navigation
scopedGlobals.navigation = navigation
expect(getNavigation()).toBe(navigation)
})
})
Expand Down
Loading