-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathRequestPicker.spec.ts
More file actions
822 lines (753 loc) · 24.2 KB
/
RequestPicker.spec.ts
File metadata and controls
822 lines (753 loc) · 24.2 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'
import type { MockedFunction } from 'vitest'
import { mount } from '@vue/test-utils'
import { loadState } from '@nextcloud/initial-state'
import { getCapabilities } from '@nextcloud/capabilities'
import { getFilePickerBuilder, showError } from '@nextcloud/dialogs'
import RequestPicker from '../../../components/Request/RequestPicker.vue'
import { useActionsMenuStore } from '../../../store/actionsmenu.js'
import { useFilesStore } from '../../../store/files.js'
import { useSidebarStore } from '../../../store/sidebar.js'
type FilePickerBuilder = {
setMultiSelect: (value: boolean) => FilePickerBuilder
setMimeTypeFilter: (value: string[]) => FilePickerBuilder
addButton: (label: string, handler: () => void) => FilePickerBuilder
build: () => { pick: () => Promise<unknown[]> }
}
type TranslationFn = (domain: string, key: string, params?: Record<string, string>) => string
const tSimple: TranslationFn = (_domain, key) => key
const tWithParams: TranslationFn = (_domain, key, params) => {
const replacements = { ...(params ?? {}) }
let result = key
Object.entries(replacements).forEach(([k, v]) => {
result = result.replace(`{${k}}`, v)
})
return result
}
const tWithMaxUploads: TranslationFn = (_domain, key, params) => {
if (key.includes('You can upload')) {
return `You can upload at most ${params?.max} files at once.`
}
return key
}
let filePickerBuilder: FilePickerBuilder
const filePickerPick = vi.fn()
vi.mock('@nextcloud/initial-state')
vi.mock('@nextcloud/capabilities')
vi.mock('@nextcloud/dialogs', () => ({
showError: vi.fn(),
getFilePickerBuilder: vi.fn(() => filePickerBuilder),
}))
vi.mock('../../../store/actionsmenu.js')
vi.mock('../../../store/files.js')
vi.mock('../../../store/sidebar.js')
describe('RequestPicker component rules', () => {
const loadStateMock = loadState as MockedFunction<typeof loadState>
const getCapabilitiesMock = getCapabilities as MockedFunction<typeof getCapabilities>
const useActionsMenuStoreMock = vi.mocked(useActionsMenuStore)
const useFilesStoreMock = vi.mocked(useFilesStore)
const useSidebarStoreMock = vi.mocked(useSidebarStore)
type FilesStoreMock = {
upload: MockedFunction<(
formData: FormData | { file: { url: string } },
config?: { onUploadProgress?: (event: { loaded: number; total: number }) => void },
) => Promise<number>>
selectFile: MockedFunction<(id?: number) => void>
}
type SidebarStoreMock = {
activeRequestSignatureTab: MockedFunction<() => void>
}
type ActionsMenuStoreMock = {
opened: boolean
}
let wrapper: ReturnType<typeof mount>
let filesStore: FilesStoreMock
let sidebarStore: SidebarStoreMock
let actionsMenuStore: ActionsMenuStoreMock
beforeEach(() => {
vi.clearAllMocks()
filePickerPick.mockResolvedValue([])
filePickerBuilder = {
setMultiSelect: vi.fn().mockReturnThis(),
setMimeTypeFilter: vi.fn().mockReturnThis(),
addButton: vi.fn().mockReturnThis(),
build: vi.fn(() => ({ pick: filePickerPick })),
}
loadStateMock.mockReturnValue(true)
getCapabilitiesMock.mockReturnValue({
libresign: {
config: {
envelope: { 'is-available': false },
upload: { 'max-file-uploads': 20 },
},
},
})
filesStore = {
upload: vi.fn(),
selectFile: vi.fn(),
}
sidebarStore = {
activeRequestSignatureTab: vi.fn(),
}
actionsMenuStore = {
opened: false,
}
useActionsMenuStoreMock.mockReturnValue(actionsMenuStore)
useFilesStoreMock.mockReturnValue(filesStore)
useSidebarStoreMock.mockReturnValue(sidebarStore)
wrapper = mount(RequestPicker, {
global: {
stubs: {
NcActions: true,
NcActionButton: true,
NcButton: true,
NcDialog: true,
NcTextField: true,
NcLoadingIcon: true,
NcNoteCard: true,
UploadProgress: true,
FilePicker: true,
LinkIcon: true,
FolderIcon: true,
UploadIcon: true,
PlusIcon: true,
CloudUploadIcon: true,
},
mocks: {
t: tWithParams,
},
},
})
})
describe('canRequestSign visibility', () => {
it('hides component when canRequestSign is false', () => {
loadStateMock.mockReturnValue(false)
const newWrapper = mount(RequestPicker, {
global: {
stubs: {
NcActions: true,
NcActionButton: true,
NcButton: true,
NcDialog: true,
NcTextField: true,
NcLoadingIcon: true,
NcNoteCard: true,
UploadProgress: true,
FilePicker: true,
LinkIcon: true,
FolderIcon: true,
UploadIcon: true,
PlusIcon: true,
CloudUploadIcon: true,
},
mocks: {
t: tSimple,
},
},
})
expect(newWrapper.find('div').exists()).toBe(false)
})
})
describe('variant prop', () => {
const mountWithVariantStub = (props = {}) => mount(RequestPicker, {
props,
global: {
stubs: {
NcActions: {
name: 'NcActions',
props: ['variant'],
template: '<div class="nc-actions-stub" :data-variant="variant"><slot /></div>',
},
NcActionButton: true,
NcButton: true,
NcDialog: true,
NcTextField: true,
NcLoadingIcon: true,
NcNoteCard: true,
UploadProgress: true,
},
mocks: { t: tSimple },
},
})
it('defaults variant to tertiary', () => {
const w = mountWithVariantStub()
expect(w.find('.nc-actions-stub').attributes('data-variant')).toBe('tertiary')
})
it('passes custom variant to NcActions', () => {
const w = mountWithVariantStub({ variant: 'primary' })
expect(w.find('.nc-actions-stub').attributes('data-variant')).toBe('primary')
})
})
describe('envelope support', () => {
it('enables envelope mode when capabilities indicate is-available true', () => {
getCapabilitiesMock.mockReturnValue({
libresign: {
config: {
envelope: { 'is-available': true },
upload: { 'max-file-uploads': 20 },
},
},
})
const newWrapper = mount(RequestPicker, {
global: {
stubs: {
NcActions: true,
NcActionButton: true,
NcButton: true,
NcDialog: true,
NcTextField: true,
NcLoadingIcon: true,
NcNoteCard: true,
UploadProgress: true,
FilePicker: true,
LinkIcon: true,
FolderIcon: true,
UploadIcon: true,
PlusIcon: true,
CloudUploadIcon: true,
},
mocks: {
t: tSimple,
},
},
})
expect(newWrapper.vm.envelopeEnabled).toBe(true)
})
it('disables envelope mode when capabilities are missing', () => {
getCapabilitiesMock.mockReturnValue({})
const newWrapper = mount(RequestPicker, {
global: {
stubs: {
NcActions: true,
NcActionButton: true,
NcButton: true,
NcDialog: true,
NcTextField: true,
NcLoadingIcon: true,
NcNoteCard: true,
UploadProgress: true,
FilePicker: true,
LinkIcon: true,
FolderIcon: true,
UploadIcon: true,
PlusIcon: true,
CloudUploadIcon: true,
},
mocks: {
t: tSimple,
},
},
})
expect(newWrapper.vm.envelopeEnabled).toBe(false)
})
})
describe('file picker', () => {
it('builds picker with choose button and closes menu', async () => {
wrapper.vm.openedMenu = true
await wrapper.vm.openFilePicker()
expect(getFilePickerBuilder).toHaveBeenCalledWith('Select your file')
expect(filePickerBuilder.setMultiSelect).toHaveBeenCalledWith(false)
expect(filePickerBuilder.setMimeTypeFilter).toHaveBeenCalledWith(['application/pdf'])
expect(filePickerBuilder.addButton).toHaveBeenCalledWith(expect.objectContaining({
label: 'Choose',
type: 'primary',
callback: expect.any(Function),
}))
expect(filePickerBuilder.build).toHaveBeenCalled()
expect(filePickerPick).toHaveBeenCalled()
expect(wrapper.vm.openedMenu).toBe(false)
})
it('uses multiselect and title when envelope enabled', async () => {
getCapabilitiesMock.mockReturnValue({
libresign: {
config: {
envelope: { 'is-available': true },
upload: { 'max-file-uploads': 20 },
},
},
})
await wrapper.vm.openFilePicker()
expect(getFilePickerBuilder).toHaveBeenCalledWith('Select your files')
expect(filePickerBuilder.setMultiSelect).toHaveBeenCalledWith(true)
})
it('passes picker results to handleFileChoose', async () => {
const nodes = [{ path: '/Documents/file.pdf' }]
filePickerPick.mockResolvedValue(nodes)
const handleSpy = vi.spyOn(wrapper.vm, 'handleFileChoose').mockResolvedValue()
await wrapper.vm.openFilePicker()
expect(handleSpy).toHaveBeenCalledWith(nodes)
})
})
describe('URL validation for upload', () => {
it('returns false for invalid URL when not loading', () => {
wrapper.vm.pdfUrl = 'not a url'
wrapper.vm.loading = false
expect(wrapper.vm.canUploadFronUrl).toBe(false)
})
it('returns true for valid URL when not loading', () => {
wrapper.vm.pdfUrl = 'https://example.com/file.pdf'
wrapper.vm.loading = false
expect(wrapper.vm.canUploadFronUrl).toBe(true)
})
it('returns false when loading regardless of URL validity', () => {
wrapper.vm.pdfUrl = 'https://example.com/file.pdf'
wrapper.vm.loading = true
expect(wrapper.vm.canUploadFronUrl).toBe(false)
})
})
describe('max file uploads configuration', () => {
it('returns configured max-file-uploads when finite and positive', () => {
getCapabilitiesMock.mockReturnValue({
libresign: {
config: {
envelope: { 'is-available': false },
upload: { 'max-file-uploads': 15 },
},
},
})
const maxUploads = wrapper.vm.getMaxFileUploads()
expect(maxUploads).toBe(15)
})
it('returns 20 as default when max-file-uploads not configured', () => {
getCapabilitiesMock.mockReturnValue({
libresign: {
config: {
envelope: { 'is-available': false },
upload: {},
},
},
})
const maxUploads = wrapper.vm.getMaxFileUploads()
expect(maxUploads).toBe(20)
})
it('returns 20 as default when max-file-uploads is non-finite', () => {
getCapabilitiesMock.mockReturnValue({
libresign: {
config: {
envelope: { 'is-available': false },
upload: { 'max-file-uploads': Infinity },
},
},
})
const maxUploads = wrapper.vm.getMaxFileUploads()
expect(maxUploads).toBe(20)
})
it('returns 20 as default when max-file-uploads is zero or negative', () => {
getCapabilitiesMock.mockReturnValue({
libresign: {
config: {
envelope: { 'is-available': false },
upload: { 'max-file-uploads': -5 },
},
},
})
const maxUploads = wrapper.vm.getMaxFileUploads()
expect(maxUploads).toBe(20)
})
it('floors decimal max-file-uploads values', () => {
getCapabilitiesMock.mockReturnValue({
libresign: {
config: {
envelope: { 'is-available': false },
upload: { 'max-file-uploads': 15.7 },
},
},
})
const maxUploads = wrapper.vm.getMaxFileUploads()
expect(maxUploads).toBe(15)
})
})
describe('file upload validation', () => {
it('shows error when exceeding max file uploads limit', () => {
getCapabilitiesMock.mockReturnValue({
libresign: {
config: {
envelope: { 'is-available': false },
upload: { 'max-file-uploads': 5 },
},
},
})
const newWrapper = mount(RequestPicker, {
global: {
stubs: {
NcActions: true,
NcActionButton: true,
NcButton: true,
NcDialog: true,
NcTextField: true,
NcLoadingIcon: true,
NcNoteCard: true,
UploadProgress: true,
FilePicker: true,
LinkIcon: true,
FolderIcon: true,
UploadIcon: true,
PlusIcon: true,
CloudUploadIcon: true,
},
mocks: {
t: tWithMaxUploads,
},
},
})
const result = newWrapper.vm.validateMaxFileUploads(10)
expect(result).toBe(false)
expect(showError).toHaveBeenCalled()
})
it('allows upload when within max file uploads limit', () => {
getCapabilitiesMock.mockReturnValue({
libresign: {
config: {
envelope: { 'is-available': false },
upload: { 'max-file-uploads': 20 },
},
},
})
const result = wrapper.vm.validateMaxFileUploads(15)
expect(result).toBe(true)
})
})
describe('envelope name dialog submission', () => {
it('creates envelope with trimmed name when minimum length met', async () => {
filesStore.upload.mockResolvedValue(1)
wrapper.vm.envelopeNameInput = ' Test Envelope '
wrapper.vm.pendingFiles = [{ name: 'file1.pdf' }]
await wrapper.vm.handleEnvelopeNameSubmit()
expect(wrapper.vm.showEnvelopeNameDialog).toBe(false)
expect(wrapper.vm.pendingFiles).toEqual([])
expect(wrapper.vm.envelopeNameInput).toBe('')
})
it('closes dialog without upload when name too short', async () => {
wrapper.vm.envelopeNameInput = ' '
wrapper.vm.pendingFiles = [{ name: 'file1.pdf' }]
wrapper.vm.showEnvelopeNameDialog = true
await wrapper.vm.handleEnvelopeNameSubmit()
expect(wrapper.vm.showEnvelopeNameDialog).toBe(true)
})
it('closes dialog without upload when no pending files', async () => {
wrapper.vm.envelopeNameInput = 'Valid Name'
wrapper.vm.pendingFiles = []
wrapper.vm.showEnvelopeNameDialog = true
await wrapper.vm.handleEnvelopeNameSubmit()
expect(wrapper.vm.showEnvelopeNameDialog).toBe(true)
})
it('clears state when closing dialog', () => {
wrapper.vm.showEnvelopeNameDialog = true
wrapper.vm.pendingFiles = [{ name: 'file1.pdf' }]
wrapper.vm.envelopeNameInput = 'Some Name'
wrapper.vm.closeEnvelopeNameDialog()
expect(wrapper.vm.showEnvelopeNameDialog).toBe(false)
expect(wrapper.vm.pendingFiles).toEqual([])
expect(wrapper.vm.envelopeNameInput).toBe('')
})
})
describe('URL upload dialog management', () => {
it('opens URL upload modal and closes actions menu', () => {
wrapper.vm.actionsMenuStore.opened = true
wrapper.vm.openedMenu = true
wrapper.vm.showModalUploadFromUrl()
expect(wrapper.vm.modalUploadFromUrl).toBe(true)
expect(wrapper.vm.openedMenu).toBe(false)
expect(wrapper.vm.loading).toBe(false)
})
it('clears errors and URL when closing modal', () => {
wrapper.vm.modalUploadFromUrl = true
wrapper.vm.uploadUrlErrors = ['error1', 'error2']
wrapper.vm.pdfUrl = 'https://example.com/file.pdf'
wrapper.vm.loading = true
wrapper.vm.closeModalUploadFromUrl()
expect(wrapper.vm.modalUploadFromUrl).toBe(false)
expect(wrapper.vm.uploadUrlErrors).toEqual([])
expect(wrapper.vm.pdfUrl).toBe('')
expect(wrapper.vm.loading).toBe(false)
})
})
describe('single file upload', () => {
it('extracts filename without .pdf extension for single file', async () => {
filesStore.upload.mockResolvedValue(1)
const files = [{ name: 'document.pdf', size: 1000 }]
await wrapper.vm.upload(files)
expect(filesStore.upload).toHaveBeenCalled()
const [formData] = filesStore.upload.mock.calls[0] as [FormData, { onUploadProgress?: (event: { loaded: number; total: number }) => void } | undefined]
expect(formData.get('name')).toBe('document')
})
it('sets upload progress when receiving upload events', async () => {
filesStore.upload.mockImplementation((formData, config) => {
config?.onUploadProgress?.({ loaded: 500, total: 1000 })
return Promise.resolve(1)
})
const files = [{ name: 'document.pdf', size: 1000 }]
await wrapper.vm.upload(files)
expect(wrapper.vm.uploadProgress).toBe(50)
expect(wrapper.vm.uploadedBytes).toBe(500)
})
it('selects file and activates signature tab after successful upload', async () => {
filesStore.upload.mockResolvedValue(42)
const files = [{ name: 'document.pdf', size: 1000 }]
await wrapper.vm.upload(files)
expect(filesStore.selectFile).toHaveBeenCalledWith(42)
expect(sidebarStore.activeRequestSignatureTab).toHaveBeenCalled()
})
it('shows error message when upload fails', async () => {
filesStore.upload.mockRejectedValue({
response: {
data: {
ocs: {
data: {
message: 'Upload failed due to invalid file',
},
},
},
},
})
const files = [{ name: 'document.pdf', size: 1000 }]
await wrapper.vm.upload(files)
expect(showError).toHaveBeenCalledWith('Upload failed due to invalid file')
})
it('handles cancel error without showing error message', async () => {
filesStore.upload.mockRejectedValue({ code: 'ERR_CANCELED' })
const files = [{ name: 'document.pdf', size: 1000 }]
await wrapper.vm.upload(files)
expect(showError).not.toHaveBeenCalled()
})
it('clears upload state after completion', async () => {
filesStore.upload.mockResolvedValue(1)
const files = [{ name: 'document.pdf', size: 1000 }]
await wrapper.vm.upload(files)
expect(wrapper.vm.loading).toBe(false)
expect(wrapper.vm.isUploading).toBe(false)
expect(wrapper.vm.uploadAbortController).toBe(null)
})
})
describe('multiple file envelope upload', () => {
it('uses envelope name for multiple files instead of filename', async () => {
filesStore.upload.mockResolvedValue(1)
const files = [
{ name: 'document1.pdf', size: 1000 },
{ name: 'document2.pdf', size: 1000 },
]
await wrapper.vm.upload(files, 'My Envelope')
expect(filesStore.upload).toHaveBeenCalled()
const [formData] = filesStore.upload.mock.calls[0] as [FormData, { onUploadProgress?: (event: { loaded: number; total: number }) => void } | undefined]
expect(formData.get('name')).toBe('My Envelope')
})
it('trims envelope name before upload', async () => {
filesStore.upload.mockResolvedValue(1)
const files = [
{ name: 'document1.pdf', size: 1000 },
{ name: 'document2.pdf', size: 1000 },
]
await wrapper.vm.upload(files, ' Envelope Name ')
expect(filesStore.upload).toHaveBeenCalled()
const [formData] = filesStore.upload.mock.calls[0] as [FormData, { onUploadProgress?: (event: { loaded: number; total: number }) => void } | undefined]
expect(formData.get('name')).toBe('Envelope Name')
})
it('calculates total bytes correctly from multiple files', async () => {
filesStore.upload.mockResolvedValue(1)
const files = [
{ name: 'document1.pdf', size: 1000 },
{ name: 'document2.pdf', size: 2000 },
]
await wrapper.vm.upload(files, 'Envelope')
expect(wrapper.vm.totalBytes).toBe(3000)
})
})
describe('upload cancellation', () => {
it('aborts upload when cancel requested', () => {
const abortController = new AbortController()
vi.spyOn(abortController, 'abort')
wrapper.vm.uploadAbortController = abortController
wrapper.vm.cancelUpload()
expect(abortController.abort).toHaveBeenCalled()
})
it('handles missing abort controller gracefully', () => {
wrapper.vm.uploadAbortController = null
expect(() => wrapper.vm.cancelUpload()).not.toThrow()
})
})
describe('direct file upload dialog', () => {
it('opens file input with PDF filter', () => {
const createElementSpy = vi.spyOn(document, 'createElement')
wrapper.vm.uploadFile()
const fileInputResult = createElementSpy.mock.results.find(
result => result.value?.type === 'file',
)
const fileInput = fileInputResult?.value as HTMLInputElement | undefined
expect(fileInput).toBeDefined()
if (!fileInput) {
createElementSpy.mockRestore()
return
}
expect(fileInput.accept).toBe('application/pdf')
})
it('enables multiple file selection when envelope enabled', () => {
getCapabilitiesMock.mockReturnValue({
libresign: {
config: {
envelope: { 'is-available': true },
upload: { 'max-file-uploads': 20 },
},
},
})
const newWrapper = mount(RequestPicker, {
global: {
stubs: {
NcActions: true,
NcActionButton: true,
NcButton: true,
NcDialog: true,
NcTextField: true,
NcLoadingIcon: true,
NcNoteCard: true,
UploadProgress: true,
FilePicker: true,
LinkIcon: true,
FolderIcon: true,
UploadIcon: true,
PlusIcon: true,
CloudUploadIcon: true,
},
mocks: {
t: tSimple,
},
},
})
const createElementSpy = vi.spyOn(document, 'createElement')
newWrapper.vm.uploadFile()
const fileInputResult = createElementSpy.mock.results.find(
result => result.value?.type === 'file',
)
const fileInput = fileInputResult?.value as HTMLInputElement | undefined
expect(fileInput).toBeDefined()
if (!fileInput) {
createElementSpy.mockRestore()
return
}
expect(fileInput.multiple).toBe(true)
createElementSpy.mockRestore()
})
it('disables multiple selection when envelope not enabled', () => {
const createElementSpy = vi.spyOn(document, 'createElement')
wrapper.vm.uploadFile()
const fileInputResult = createElementSpy.mock.results.find(
result => result.value?.type === 'file',
)
const fileInput = fileInputResult?.value as HTMLInputElement | undefined
expect(fileInput).toBeDefined()
if (!fileInput) {
createElementSpy.mockRestore()
return
}
expect(fileInput.multiple).toBe(false)
createElementSpy.mockRestore()
})
it('closes action menu when opening file input', () => {
wrapper.vm.openedMenu = true
vi.spyOn(document, 'createElement')
wrapper.vm.uploadFile()
expect(wrapper.vm.openedMenu).toBe(false)
})
})
describe('URL file upload', () => {
it('uploads file from URL and selects it', async () => {
filesStore.upload.mockResolvedValue(42)
wrapper.vm.pdfUrl = 'https://example.com/file.pdf'
await wrapper.vm.uploadUrl()
expect(filesStore.upload).toHaveBeenCalledWith({
file: { url: 'https://example.com/file.pdf' },
})
expect(filesStore.selectFile).toHaveBeenCalledWith(42)
expect(sidebarStore.activeRequestSignatureTab).toHaveBeenCalled()
})
it('closes URL modal after successful upload', async () => {
filesStore.upload.mockResolvedValue(1)
wrapper.vm.modalUploadFromUrl = true
wrapper.vm.pdfUrl = 'https://example.com/file.pdf'
await wrapper.vm.uploadUrl()
expect(wrapper.vm.modalUploadFromUrl).toBe(false)
expect(wrapper.vm.uploadUrlErrors).toEqual([])
expect(wrapper.vm.pdfUrl).toBe('')
})
it('shows validation error from server response', async () => {
filesStore.upload.mockRejectedValue({
response: {
data: {
ocs: {
data: {
message: 'Invalid URL or file not found',
},
},
},
},
})
wrapper.vm.modalUploadFromUrl = true
wrapper.vm.pdfUrl = 'https://example.com/invalid.pdf'
await wrapper.vm.uploadUrl()
expect(wrapper.vm.uploadUrlErrors).toEqual(['Invalid URL or file not found'])
expect(wrapper.vm.loading).toBe(false)
})
})
describe('file picker selection', () => {
it('returns early when no paths selected', async () => {
await wrapper.vm.handleFileChoose([])
expect(filesStore.upload).not.toHaveBeenCalled()
})
it('returns early when nodes have no paths', async () => {
await wrapper.vm.handleFileChoose([{ name: 'file' }])
expect(filesStore.upload).not.toHaveBeenCalled()
})
it('uploads single file from picker', async () => {
filesStore.upload.mockResolvedValue(42)
const nodes = [{ path: '/Documents/file.pdf' }]
await wrapper.vm.handleFileChoose(nodes)
expect(filesStore.upload).toHaveBeenCalled()
expect(filesStore.selectFile).toHaveBeenCalledWith(42)
expect(sidebarStore.activeRequestSignatureTab).toHaveBeenCalled()
})
it('extracts filename from path for upload payload', async () => {
filesStore.upload.mockResolvedValue(1)
const nodes = [{ path: '/Documents/report.pdf' }]
await wrapper.vm.handleFileChoose(nodes)
expect(filesStore.upload).toHaveBeenCalledWith(
expect.objectContaining({
file: { path: '/Documents/report.pdf' },
name: 'report',
}),
)
})
it('shows error from server when upload fails', async () => {
filesStore.upload.mockRejectedValue({
response: {
data: {
ocs: {
data: {
message: 'File access denied',
},
},
},
},
})
const nodes = [{ path: '/Documents/file.pdf' }]
await wrapper.vm.handleFileChoose(nodes)
expect(showError).toHaveBeenCalledWith('File access denied')
})
it('shows generic error when server response malformed', async () => {
filesStore.upload.mockRejectedValue({
response: {
data: {
ocs: {
data: {},
},
},
},
})
const nodes = [{ path: '/Documents/file.pdf' }]
await wrapper.vm.handleFileChoose(nodes)
expect(showError).toHaveBeenCalledWith('Upload failed')
})
})
})