|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import type { IFolder, INode, IView } from '../../lib/index.ts' |
| 7 | + |
| 8 | +import { beforeEach, describe, expect, it, Mock, vi } from 'vitest' |
| 9 | +import { getSidebarTabs, ISidebarTab, registerSidebarTab } from '../../lib/sidebar' |
| 10 | +// missing in JSDom but supported by every browser! |
| 11 | +import 'css.escape' |
| 12 | + |
| 13 | +class SidebarTabMock extends HTMLElement { |
| 14 | + |
| 15 | + node?: INode |
| 16 | + folder?: IFolder |
| 17 | + view?: IView |
| 18 | + |
| 19 | + public setActive(active: boolean) { |
| 20 | + console.log('setActive', active) |
| 21 | + } |
| 22 | + |
| 23 | +} |
| 24 | + |
| 25 | +describe('Sidebar tabs', () => { |
| 26 | + let getCustomElementsSpy: Mock |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + vi.restoreAllMocks() |
| 30 | + getCustomElementsSpy = vi.spyOn(window.customElements, 'get') |
| 31 | + .mockImplementation(() => SidebarTabMock) |
| 32 | + delete window._nc_files_sidebar_tabs |
| 33 | + }) |
| 34 | + |
| 35 | + it('can register a tab', () => { |
| 36 | + const tab = getExampleTab() |
| 37 | + |
| 38 | + registerSidebarTab(tab) |
| 39 | + expect(window._nc_files_sidebar_tabs).toBeInstanceOf(Map) |
| 40 | + expect(window._nc_files_sidebar_tabs!.has(tab.id)).toBe(true) |
| 41 | + expect(window._nc_files_sidebar_tabs!.get(tab.id)).toBe(tab) |
| 42 | + }) |
| 43 | + |
| 44 | + it('can fetch empty list of sidebar tabs', () => { |
| 45 | + expect(getSidebarTabs()).toBeInstanceOf(Array) |
| 46 | + expect(getSidebarTabs()).toHaveLength(0) |
| 47 | + }) |
| 48 | + |
| 49 | + it('can fetch list of sidebar tabs', () => { |
| 50 | + registerSidebarTab(getExampleTab()) |
| 51 | + registerSidebarTab({ ...getExampleTab(), id: 'another-example' }) |
| 52 | + |
| 53 | + expect(getSidebarTabs()).toBeInstanceOf(Array) |
| 54 | + expect(getSidebarTabs()).toHaveLength(2) |
| 55 | + }) |
| 56 | + |
| 57 | + it('only registeres same id once', () => { |
| 58 | + const consoleSpy = vi.spyOn(console, 'warn') |
| 59 | + consoleSpy.mockImplementationOnce(() => {}) |
| 60 | + |
| 61 | + registerSidebarTab(getExampleTab()) |
| 62 | + registerSidebarTab(getExampleTab()) |
| 63 | + expect(consoleSpy).toHaveBeenCalledOnce() |
| 64 | + expect(getSidebarTabs()).toHaveLength(1) |
| 65 | + }) |
| 66 | + |
| 67 | + describe('Tab validation', () => { |
| 68 | + it('fails with an invalid parameter', () => { |
| 69 | + expect( |
| 70 | + // @ts-expect-error mocking for testing |
| 71 | + () => registerSidebarTab(getExampleTab), |
| 72 | + ).toThrowErrorMatchingInlineSnapshot('[Error: Sidebar tab is not an object]') |
| 73 | + }) |
| 74 | + |
| 75 | + it('fails with missing id', () => { |
| 76 | + expect( |
| 77 | + // @ts-expect-error mocking for testing |
| 78 | + () => registerSidebarTab({ ...getExampleTab(), id: undefined }), |
| 79 | + ).toThrowErrorMatchingInlineSnapshot('[Error: Sidebar tabs need to have an id conforming to the HTML id attribute specifications]') |
| 80 | + }) |
| 81 | + |
| 82 | + it('fails with non conforming id', () => { |
| 83 | + expect( |
| 84 | + () => registerSidebarTab({ ...getExampleTab(), id: 'this is invalid' }), |
| 85 | + ).toThrowErrorMatchingInlineSnapshot('[Error: Sidebar tabs need to have an id conforming to the HTML id attribute specifications]') |
| 86 | + }) |
| 87 | + |
| 88 | + it('fails with missing tagName name', () => { |
| 89 | + expect( |
| 90 | + // @ts-expect-error mocking for testing |
| 91 | + () => registerSidebarTab({ ...getExampleTab(), tagName: undefined }), |
| 92 | + ).toThrowErrorMatchingInlineSnapshot('[Error: Sidebar tabs need to have the tagName name set]') |
| 93 | + }) |
| 94 | + |
| 95 | + it('fails with invalid tagName name', () => { |
| 96 | + expect(() => registerSidebarTab({ ...getExampleTab(), tagName: 'MyAppSidebarTab' })) |
| 97 | + .toThrowErrorMatchingInlineSnapshot('[Error: Sidebar tabs tagName name is invalid]') |
| 98 | + }) |
| 99 | + |
| 100 | + it('fails with non registered element', () => { |
| 101 | + getCustomElementsSpy.mockImplementationOnce(() => undefined) |
| 102 | + expect(() => registerSidebarTab(getExampleTab())).toThrowErrorMatchingInlineSnapshot('[Error: Sidebar tab element not registered]') |
| 103 | + }) |
| 104 | + |
| 105 | + it('fails with invalid custom element', () => { |
| 106 | + getCustomElementsSpy.mockImplementationOnce(() => HTMLElement) |
| 107 | + expect(() => registerSidebarTab(getExampleTab())).toThrowErrorMatchingInlineSnapshot('[Error: Sidebar tab elements must have the `setActive` method]') |
| 108 | + }) |
| 109 | + |
| 110 | + it('fails with missing name', () => { |
| 111 | + expect( |
| 112 | + // @ts-expect-error mocking for testing |
| 113 | + () => registerSidebarTab({ ...getExampleTab(), displayName: undefined }), |
| 114 | + ).toThrowErrorMatchingInlineSnapshot('[Error: Sidebar tabs need to have a name set]') |
| 115 | + }) |
| 116 | + |
| 117 | + it('fails with invalid name', () => { |
| 118 | + expect( |
| 119 | + // @ts-expect-error mocking for testing |
| 120 | + () => registerSidebarTab({ ...getExampleTab(), displayName: 1234 }), |
| 121 | + ).toThrowErrorMatchingInlineSnapshot('[Error: Sidebar tabs need to have a name set]') |
| 122 | + }) |
| 123 | + |
| 124 | + it('fails with missing icon', () => { |
| 125 | + expect( |
| 126 | + // @ts-expect-error mocking for testing |
| 127 | + () => registerSidebarTab({ ...getExampleTab(), iconSvgInline: undefined }), |
| 128 | + ).toThrowErrorMatchingInlineSnapshot('[Error: Sidebar tabs need to have an valid SVG icon]') |
| 129 | + }) |
| 130 | + |
| 131 | + it('fails with invalid SVG icon', () => { |
| 132 | + expect( |
| 133 | + () => registerSidebarTab({ ...getExampleTab(), iconSvgInline: 'icon-group' }), |
| 134 | + ).toThrowErrorMatchingInlineSnapshot('[Error: Sidebar tabs need to have an valid SVG icon]') |
| 135 | + }) |
| 136 | + |
| 137 | + it('fails with missing order', () => { |
| 138 | + expect( |
| 139 | + // @ts-expect-error mocking for testing |
| 140 | + () => registerSidebarTab({ ...getExampleTab(), order: undefined }), |
| 141 | + ).toThrowErrorMatchingInlineSnapshot('[Error: Sidebar tabs need to have a numeric order set]') |
| 142 | + }) |
| 143 | + |
| 144 | + it('fails with invalid order', () => { |
| 145 | + expect( |
| 146 | + // @ts-expect-error mocking for testing |
| 147 | + () => registerSidebarTab({ ...getExampleTab(), order: '3' }), |
| 148 | + ).toThrowErrorMatchingInlineSnapshot('[Error: Sidebar tabs need to have a numeric order set]') |
| 149 | + }) |
| 150 | + |
| 151 | + it('fails with missing "enabled" method', () => { |
| 152 | + expect( |
| 153 | + // @ts-expect-error mocking for testing |
| 154 | + () => registerSidebarTab({ ...getExampleTab(), enabled: undefined }), |
| 155 | + ).toThrowErrorMatchingInlineSnapshot('[Error: Sidebar tabs need to have an "enabled" method]') |
| 156 | + }) |
| 157 | + }) |
| 158 | +}) |
| 159 | + |
| 160 | +/** |
| 161 | + * Get a very basic mock of a sidebar tab |
| 162 | + */ |
| 163 | +function getExampleTab(): ISidebarTab { |
| 164 | + return { |
| 165 | + id: 'example-tab', |
| 166 | + displayName: 'Example', |
| 167 | + tagName: 'example_app-files-sidebar-tab', |
| 168 | + enabled: vi.fn(), |
| 169 | + iconSvgInline: '<svg><circle r="45" cx="50" cy="50" fill="red" /></svg>', |
| 170 | + order: 0, |
| 171 | + } |
| 172 | +} |
0 commit comments