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