Skip to content

Commit 43f133f

Browse files
test(vue3): add File card coverage
1 parent 8ae2d12 commit 43f133f

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { beforeEach, describe, expect, it, vi } from 'vitest'
7+
import { mount } from '@vue/test-utils'
8+
9+
import File from '../../../components/File/File.vue'
10+
11+
const filesStoreMock = {
12+
selectedFileId: 7,
13+
files: {
14+
7: {
15+
id: 13,
16+
nodeId: 99,
17+
name: 'contract.pdf',
18+
status: 3,
19+
statusText: 'Signed',
20+
},
21+
},
22+
selectFile: vi.fn(),
23+
}
24+
25+
const sidebarStoreMock = {
26+
activeRequestSignatureTab: vi.fn(),
27+
}
28+
29+
vi.mock('@nextcloud/l10n', () => ({
30+
t: vi.fn((_app: string, text: string) => text),
31+
}))
32+
33+
vi.mock('@nextcloud/router', () => ({
34+
generateOcsUrl: vi.fn((path: string, params?: Record<string, string | number>) => {
35+
if (path.includes('{nodeId}')) {
36+
return `https://cloud.example${path.replace('{nodeId}', String(params?.nodeId ?? ''))}`
37+
}
38+
return `https://cloud.example${path.replace('{fileId}', String(params?.fileId ?? ''))}`
39+
}),
40+
generateUrl: vi.fn((path: string, params?: Record<string, string | number>) => path.replace('{fileid}', String(params?.fileid ?? ''))),
41+
}))
42+
43+
vi.mock('../../../store/files.js', () => ({
44+
useFilesStore: vi.fn(() => filesStoreMock),
45+
}))
46+
47+
vi.mock('../../../store/sidebar.js', () => ({
48+
useSidebarStore: vi.fn(() => sidebarStoreMock),
49+
}))
50+
51+
vi.mock('@nextcloud/vue/components/NcIconSvgWrapper', () => ({
52+
default: {
53+
name: 'NcIconSvgWrapper',
54+
template: '<i class="nc-icon-svg-wrapper-stub" />',
55+
},
56+
}))
57+
58+
describe('File.vue', () => {
59+
const createWrapper = () => mount(File)
60+
61+
beforeEach(() => {
62+
filesStoreMock.selectedFileId = 7
63+
filesStoreMock.files = {
64+
7: {
65+
id: 13,
66+
nodeId: 99,
67+
name: 'contract.pdf',
68+
status: 3,
69+
statusText: 'Signed',
70+
},
71+
}
72+
filesStoreMock.selectFile.mockReset()
73+
sidebarStoreMock.activeRequestSignatureTab.mockReset()
74+
})
75+
76+
it('renders the selected file preview using the node id thumbnail endpoint', () => {
77+
const wrapper = createWrapper()
78+
79+
const image = wrapper.find('img')
80+
81+
expect(image.exists()).toBe(true)
82+
expect(wrapper.find('h1').text()).toBe('contract.pdf')
83+
expect(image.attributes('src')).toContain('/apps/libresign/api/v1/file/thumbnail/99')
84+
expect(image.attributes('src')).toContain('x=128')
85+
expect(image.attributes('src')).toContain('y=128')
86+
expect(image.attributes('src')).toContain('mimeFallback=true')
87+
expect(image.attributes('src')).toContain('a=0')
88+
})
89+
90+
it('falls back to the file id thumbnail endpoint when node id is absent', () => {
91+
filesStoreMock.files[7] = {
92+
id: 13,
93+
name: 'fallback.pdf',
94+
status: 1,
95+
statusText: 'Pending',
96+
}
97+
98+
const wrapper = createWrapper()
99+
100+
expect(wrapper.find('img').attributes('src')).toContain('/apps/libresign/api/v1/file/thumbnail/file_id/13')
101+
})
102+
103+
it('selects the file and opens the request signature sidebar on click', async () => {
104+
const wrapper = createWrapper()
105+
106+
await wrapper.get('.content-file').trigger('click')
107+
108+
expect(filesStoreMock.selectFile).toHaveBeenCalledWith(7)
109+
expect(sidebarStoreMock.activeRequestSignatureTab).toHaveBeenCalledTimes(1)
110+
})
111+
112+
it('maps file status values to the expected CSS classes', () => {
113+
const wrapper = createWrapper()
114+
115+
expect(wrapper.vm.statusToClass(0)).toBe('no-signers')
116+
expect(wrapper.vm.statusToClass(1)).toBe('pending')
117+
expect(wrapper.vm.statusToClass(2)).toBe('pending')
118+
expect(wrapper.vm.statusToClass(3)).toBe('signed')
119+
expect(wrapper.vm.statusToClass(999)).toBe('')
120+
})
121+
})

0 commit comments

Comments
 (0)