-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Christopher Ng <[email protected]>
- Loading branch information
Showing
1 changed file
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import { beforeEach, describe, expect, test } from 'vitest' | ||
|
||
import { getNavigation } from '../lib/navigation/navigation.ts' | ||
import { View } from '../lib/navigation/view.ts' | ||
import { ViewAction } from '../lib/viewAction.ts' | ||
|
||
const mockView = (actions: undefined | ViewAction[] = undefined) => new View({ | ||
id: 'view', | ||
name: 'View', | ||
order: 1, | ||
icon: '<svg></svg>', | ||
getContents: () => Promise.reject(new Error()), | ||
actions, | ||
}) | ||
|
||
const mockAction = (id: string = 'test') => new ViewAction({ | ||
id, | ||
displayName: 'Test', | ||
iconSvgInline: '<svg></svg>', | ||
exec: async () => {}, | ||
}) | ||
|
||
describe('ViewActions init', () => { | ||
beforeEach(() => { | ||
delete window._nc_navigation | ||
}) | ||
|
||
test('Uninitialized view actions', () => { | ||
const view = mockView() | ||
const viewActions = view.actions | ||
expect(viewActions).toBe(undefined) | ||
}) | ||
|
||
test('Initialize a single view action', () => { | ||
const viewAction = mockAction() | ||
|
||
expect(viewAction.id).toBe('test') | ||
expect(viewAction.displayName).toBe('Test') | ||
expect(viewAction.iconSvgInline).toBe('<svg></svg>') | ||
|
||
const view = mockView([viewAction]) | ||
const navigation = getNavigation() | ||
navigation.register(view) | ||
|
||
expect(navigation.views).toHaveLength(1) | ||
expect(navigation.views.at(0)?.actions).toHaveLength(1) | ||
expect(navigation.views.at(0)?.actions?.at(0)).toStrictEqual(viewAction) | ||
}) | ||
|
||
test('Initialize multiple view actions', () => { | ||
const fooAction = mockAction('foo') | ||
const barAction = mockAction('bar') | ||
const bazAction = mockAction('baz') | ||
|
||
const view = mockView([ | ||
fooAction, | ||
barAction, | ||
bazAction, | ||
]) | ||
const navigation = getNavigation() | ||
navigation.register(view) | ||
|
||
expect(navigation.views).toHaveLength(1) | ||
expect(navigation.views.at(0)?.actions).toHaveLength(3) | ||
expect(navigation.views.at(0)?.actions?.at(0)).toStrictEqual(fooAction) | ||
expect(navigation.views.at(0)?.actions?.at(1)).toStrictEqual(barAction) | ||
expect(navigation.views.at(0)?.actions?.at(2)).toStrictEqual(bazAction) | ||
}) | ||
}) | ||
|
||
describe('Invalid ViewAction creation', () => { | ||
test('Invalid id', () => { | ||
expect(() => new ViewAction({ | ||
displayName: 'Test', | ||
iconSvgInline: '<svg></svg>', | ||
exec: async () => {}, | ||
} as unknown as ViewAction), | ||
).toThrowError('Invalid id') | ||
}) | ||
test('Invalid displayName', () => { | ||
expect(() => new ViewAction({ | ||
id: 'test', | ||
iconSvgInline: () => '<svg></svg>', | ||
exec: async () => {}, | ||
} as unknown as ViewAction), | ||
).toThrowError('Invalid displayName') | ||
}) | ||
test('Invalid title', () => { | ||
expect(() => new ViewAction({ | ||
id: 'test', | ||
displayName: 'Test', | ||
title: null, | ||
iconSvgInline: () => '<svg></svg>', | ||
exec: async () => {}, | ||
} as unknown as ViewAction), | ||
).toThrowError('Invalid title') | ||
}) | ||
test('Invalid iconSvgInline', () => { | ||
expect(() => new ViewAction({ | ||
id: 'test', | ||
displayName: 'Test', | ||
exec: async () => {}, | ||
} as unknown as ViewAction), | ||
).toThrowError('Invalid iconSvgInline') | ||
}) | ||
test('Invalid show', () => { | ||
expect(() => new ViewAction({ | ||
id: 'test', | ||
displayName: 'Test', | ||
iconSvgInline: '<svg></svg>', | ||
show: null, | ||
exec: async () => {}, | ||
} as unknown as ViewAction), | ||
).toThrowError('Invalid show function') | ||
}) | ||
test('Invalid disabled', () => { | ||
expect(() => new ViewAction({ | ||
id: 'test', | ||
displayName: 'Test', | ||
iconSvgInline: '<svg></svg>', | ||
disabled: null, | ||
exec: async () => {}, | ||
} as unknown as ViewAction), | ||
).toThrowError('Invalid disabled function') | ||
}) | ||
test('Invalid exec', () => { | ||
expect(() => new ViewAction({ | ||
id: 'test', | ||
displayName: 'Test', | ||
iconSvgInline: '<svg></svg>', | ||
exec: null, | ||
} as unknown as ViewAction), | ||
).toThrowError('Invalid exec function') | ||
}) | ||
}) | ||
|
||
describe('ViewAction creation', () => { | ||
test('Create a ViewAction', async () => { | ||
const viewAction = new ViewAction({ | ||
id: 'test', | ||
displayName: 'Test', | ||
title: 'Test title', | ||
iconSvgInline: '<svg></svg>', | ||
show: () => true, | ||
disabled: () => false, | ||
exec: async () => {}, | ||
}) | ||
|
||
expect(viewAction.id).toBe('test') | ||
expect(viewAction.displayName).toBe('Test') | ||
expect(viewAction.title).toBe('Test title') | ||
expect(viewAction.iconSvgInline).toBe('<svg></svg>') | ||
expect(viewAction.show?.({} as unknown as View, [])).toBe(true) | ||
expect(viewAction.disabled?.({} as unknown as View, [])).toBe(false) | ||
await expect(viewAction.exec({} as unknown as View, [])).resolves.toBe(undefined) | ||
}) | ||
}) |