-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFilePicker.spec.ts
More file actions
99 lines (84 loc) · 2.74 KB
/
Copy pathFilePicker.spec.ts
File metadata and controls
99 lines (84 loc) · 2.74 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
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { INode } from '@nextcloud/files'
import { Folder } from '@nextcloud/files'
import { shallowMount } from '@vue/test-utils'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { ref, shallowRef } from 'vue'
import FilePicker from './FilePicker.vue'
const axios = vi.hoisted(() => ({
get: vi.fn(() => new Promise(() => {})),
}))
vi.mock('@nextcloud/axios', () => ({ default: axios }))
// Shared state injected into the mocked `useDAVFiles` composable so we can
// control the current folder without hitting WebDAV.
const dav = vi.hoisted(() => ({
files: undefined as never,
folder: undefined as never,
isLoading: undefined as never,
}))
vi.mock('../../composables/dav.ts', () => ({
useDAVFiles: () => ({
files: dav.files,
folder: dav.folder,
isLoading: dav.isLoading,
loadFiles: vi.fn(),
createDirectory: vi.fn(),
}),
}))
const currentFolder = new Folder({
owner: null,
source: 'http://example.com/dav/folder',
root: '/',
})
describe('FilePicker', () => {
beforeEach(() => {
dav.files = shallowRef([]) as never
dav.folder = shallowRef(currentFolder) as never
dav.isLoading = ref(false) as never
})
afterEach(() => {
vi.clearAllMocks()
})
/**
* Mount the FilePicker with a `buttons` factory that captures the nodes
* that would be passed to the button callback.
*
* @param props Additional props for the FilePicker
*/
function mountWithButtonSpy(props: Record<string, unknown> = {}) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const buttons = vi.fn((_nodes: INode[]) => [{
label: 'Pick',
callback: () => {},
}])
shallowMount(FilePicker, {
propsData: {
name: 'Test picker',
buttons,
...props,
},
})
// The last call reflects the current selection state
return buttons.mock.calls.at(-1)![0]
}
it('picks the current folder if directory picking is allowed and nothing is selected', () => {
const nodes = mountWithButtonSpy({ allowPickDirectory: true })
expect(nodes).toEqual([currentFolder])
})
it('picks the current folder if `canPickFn` allows it', () => {
const canPickFn = vi.fn(() => true)
const nodes = mountWithButtonSpy({ allowPickDirectory: true, canPickFn })
expect(canPickFn).toHaveBeenCalledWith(currentFolder)
expect(nodes).toEqual([currentFolder])
})
it('does not pick the current folder if `canPickFn` rejects it', () => {
const canPickFn = vi.fn(() => false)
const nodes = mountWithButtonSpy({ allowPickDirectory: true, canPickFn })
expect(canPickFn).toHaveBeenCalledWith(currentFolder)
// The unpickable folder must not be passed as a selected node
expect(nodes).toEqual([])
})
})