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