Skip to content

Commit b3f5443

Browse files
committed
test: add coverage for tab sidebar entrypoint
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 780575c commit b3f5443

1 file changed

Lines changed: 129 additions & 0 deletions

File tree

src/tests/tab.spec.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2026 LibreSign contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
7+
8+
const mockLoadState = vi.fn(() => true)
9+
const mockRegisterSidebarTab = vi.fn()
10+
const mockCreatePinia = vi.fn(() => ({ _id: 'pinia' }))
11+
12+
const mockMountedInstance = {
13+
update: vi.fn(),
14+
}
15+
16+
const mockVueApp = {
17+
config: { globalProperties: {} as Record<string, unknown> },
18+
use: vi.fn().mockReturnThis(),
19+
mount: vi.fn(() => mockMountedInstance),
20+
unmount: vi.fn(),
21+
}
22+
23+
const mockCreateApp = vi.fn(() => mockVueApp)
24+
const appFilesTabModuleLoaded = vi.fn(() => ({
25+
default: { name: 'AppFilesTabStub', template: '<div />' },
26+
}))
27+
28+
vi.mock('@nextcloud/initial-state', () => ({
29+
loadState: mockLoadState,
30+
}))
31+
32+
vi.mock('@nextcloud/l10n', () => ({
33+
t: (_app: string, text: string) => text,
34+
n: (_app: string, singular: string, _plural: string, _count: number) => singular,
35+
}))
36+
37+
vi.mock('@nextcloud/files', () => ({
38+
FileType: { Folder: 'dir' },
39+
registerSidebarTab: mockRegisterSidebarTab,
40+
}))
41+
42+
vi.mock('pinia', () => ({
43+
createPinia: mockCreatePinia,
44+
}))
45+
46+
vi.mock('vue', () => ({
47+
createApp: mockCreateApp,
48+
}))
49+
50+
vi.mock('../components/RightSidebar/AppFilesTab.vue', () => appFilesTabModuleLoaded())
51+
vi.mock('../../img/app-dark.svg?raw', () => ({ default: '<svg />' }))
52+
vi.mock('../style/icons.scss', () => ({}))
53+
54+
beforeAll(async () => {
55+
await import('../tab')
56+
})
57+
58+
beforeEach(() => {
59+
vi.clearAllMocks()
60+
window.OCA = window.OCA ?? {}
61+
window.OCA.Libresign = {}
62+
})
63+
64+
describe('tab.ts', () => {
65+
it('registers LibreSign sidebar tab on DOMContentLoaded', () => {
66+
window.dispatchEvent(new Event('DOMContentLoaded'))
67+
68+
expect(mockRegisterSidebarTab).toHaveBeenCalledOnce()
69+
const tabConfig = mockRegisterSidebarTab.mock.calls[0][0] as { id: string; tagName: string }
70+
expect(tabConfig.id).toBe('libresign')
71+
expect(tabConfig.tagName).toBe('libresign-files-sidebar-tab')
72+
})
73+
74+
it('enabled() returns false when certificate is not configured', () => {
75+
mockLoadState.mockReturnValue(false)
76+
window.dispatchEvent(new Event('DOMContentLoaded'))
77+
const tabConfig = mockRegisterSidebarTab.mock.calls[0][0] as {
78+
enabled: (context: { node: Record<string, unknown> }) => boolean
79+
}
80+
81+
expect(tabConfig.enabled({ node: { type: 'file', mimetype: 'application/pdf' } })).toBe(false)
82+
})
83+
84+
it('enabled() accepts signed folders and maps file info into OCA.Libresign', () => {
85+
mockLoadState.mockReturnValue(true)
86+
window.dispatchEvent(new Event('DOMContentLoaded'))
87+
const tabConfig = mockRegisterSidebarTab.mock.calls[0][0] as {
88+
enabled: (context: { node: Record<string, unknown> }) => boolean
89+
}
90+
91+
const enabled = tabConfig.enabled({
92+
node: {
93+
fileid: 101,
94+
basename: 'Signed',
95+
dirname: '/Documents',
96+
type: 'dir',
97+
attributes: {
98+
'libresign-signature-status': 'completed',
99+
},
100+
},
101+
})
102+
103+
expect(enabled).toBe(true)
104+
expect(window.OCA.Libresign.fileInfo).toMatchObject({
105+
id: 101,
106+
name: 'Signed',
107+
path: '/Documents',
108+
})
109+
})
110+
111+
it('lazy mounts Vue only when custom element is connected and unmounts on disconnect', async () => {
112+
window.dispatchEvent(new Event('DOMContentLoaded'))
113+
114+
const TabElement = window.customElements.get('libresign-files-sidebar-tab')
115+
expect(TabElement).toBeDefined()
116+
expect(mockCreateApp).not.toHaveBeenCalled()
117+
expect(appFilesTabModuleLoaded).not.toHaveBeenCalled()
118+
119+
const element = document.createElement('libresign-files-sidebar-tab')
120+
document.body.appendChild(element)
121+
122+
await vi.waitFor(() => expect(appFilesTabModuleLoaded).toHaveBeenCalledOnce())
123+
expect(mockCreateApp).toHaveBeenCalledOnce()
124+
expect(mockVueApp.mount).toHaveBeenCalledOnce()
125+
126+
element.remove()
127+
expect(mockVueApp.unmount).toHaveBeenCalledOnce()
128+
})
129+
})

0 commit comments

Comments
 (0)