-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathFilesList.spec.ts
More file actions
284 lines (235 loc) · 8.46 KB
/
FilesList.spec.ts
File metadata and controls
284 lines (235 loc) · 8.46 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
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { flushPromises, mount } from '@vue/test-utils'
import { createPinia, setActivePinia } from 'pinia'
import FilesList from '../../../views/FilesList/FilesList.vue'
import { useFilesStore } from '../../../store/files.js'
import { useUserConfigStore } from '../../../store/userconfig.js'
vi.mock('@nextcloud/l10n', () => ({
t: vi.fn((_app: string, text: string) => text),
}))
vi.mock('@nextcloud/logger', () => ({
getLogger: vi.fn(() => ({
error: vi.fn(),
warn: vi.fn(),
info: vi.fn(),
debug: vi.fn(),
})),
getLoggerBuilder: vi.fn(() => ({
setApp: vi.fn().mockReturnThis(),
detectUser: vi.fn().mockReturnThis(),
build: vi.fn(() => ({
error: vi.fn(),
warn: vi.fn(),
info: vi.fn(),
debug: vi.fn(),
})),
})),
}))
vi.mock('@nextcloud/axios', () => ({
default: {
get: vi.fn(),
post: vi.fn(),
delete: vi.fn(),
patch: vi.fn(),
},
}))
vi.mock('@nextcloud/router', () => ({
generateOcsUrl: vi.fn((path: string) => `/ocs/v2.php${path}`),
}))
vi.mock('@nextcloud/auth', () => ({
getCurrentUser: vi.fn(() => ({
uid: 'testuser',
displayName: 'Test User',
})),
}))
vi.mock('@nextcloud/event-bus', () => ({
emit: vi.fn(),
subscribe: vi.fn(),
}))
vi.mock('@nextcloud/initial-state', () => ({
loadState: vi.fn((_app: string, _key: string, defaultValue: unknown) => defaultValue),
}))
vi.mock('@nextcloud/moment', () => ({
default: vi.fn(() => ({
format: () => 'date',
fromNow: () => '2 days ago',
})),
}))
vi.mock('@nextcloud/vue/components/NcAppContent', () => ({
default: { name: 'NcAppContent', template: '<div><slot /></div>' },
}))
vi.mock('@nextcloud/vue/components/NcBreadcrumb', () => ({
default: {
name: 'NcBreadcrumb',
template: '<div><slot name="icon" /><slot name="menu-icon" /><slot /></div>',
},
}))
vi.mock('@nextcloud/vue/components/NcActionButton', () => ({
default: {
name: 'NcActionButton',
emits: ['click'],
template: '<button class="nc-action-button-stub" @click="$emit(\'click\')"><slot /></button>',
},
}))
vi.mock('@nextcloud/vue/components/NcBreadcrumbs', () => ({
default: { name: 'NcBreadcrumbs', template: '<div><slot /><slot name="actions" /></div>' },
}))
vi.mock('@nextcloud/vue/components/NcButton', () => ({
default: { name: 'NcButton', template: '<button><slot name="icon" /></button>' },
}))
vi.mock('@nextcloud/vue/components/NcEmptyContent', () => ({
default: { name: 'NcEmptyContent', template: '<section><slot name="action" /><slot name="icon" /></section>' },
}))
vi.mock('@nextcloud/vue/components/NcIconSvgWrapper', () => ({
default: {
name: 'NcIconSvgWrapper',
props: ['path', 'svg', 'size'],
template: '<i class="nc-icon" :data-path="path" :data-svg="svg" />',
},
}))
vi.mock('@nextcloud/vue/components/NcLoadingIcon', () => ({
default: { name: 'NcLoadingIcon', template: '<span class="loading" />' },
}))
vi.mock('../../../views/FilesList/FilesListVirtual.vue', () => ({
default: {
name: 'FilesListVirtual',
props: ['nodes', 'loading'],
template: '<div class="virtual-list"><slot name="empty" /></div>',
},
}))
vi.mock('../../../views/FilesList/FileListFilters.vue', () => ({
default: {
name: 'FileListFilters',
template: '<div class="file-list-filters-stub" />',
},
}))
vi.mock('../../../components/Request/RequestPicker.vue', () => ({
default: {
name: 'RequestPicker',
template: '<div class="request-picker-stub" />',
},
}))
const routeMock = {
query: {},
}
describe('FilesList.vue rendering rules', () => {
beforeEach(() => {
setActivePinia(createPinia())
vi.clearAllMocks()
})
function mountComponent() {
return mount(FilesList, {
global: {
mocks: {
$route: routeMock,
},
},
})
}
it('exposes mdi icons from setup for template bindings', async () => {
const filesStore = useFilesStore()
vi.spyOn(filesStore, 'getAllFiles').mockResolvedValue({})
const wrapper = mountComponent()
await flushPromises()
expect(wrapper.vm.mdiFolder).toBeTruthy()
expect(wrapper.vm.mdiViewGridOutline).toBeTruthy()
expect(wrapper.vm.mdiFormatListBulletedSquare).toBeTruthy()
expect(wrapper.vm.mdiChevronDown).toBeTruthy()
expect(wrapper.vm.mdiChevronUp).toBeTruthy()
expect(wrapper.vm.mdiReload).toBeTruthy()
})
it('initialises isMenuOpen as false', async () => {
const filesStore = useFilesStore()
vi.spyOn(filesStore, 'getAllFiles').mockResolvedValue({})
const wrapper = mountComponent()
await flushPromises()
expect(wrapper.vm.isMenuOpen).toBe(false)
})
it('renders RequestPicker before the breadcrumbs in the header', async () => {
const filesStore = useFilesStore()
vi.spyOn(filesStore, 'getAllFiles').mockResolvedValue({})
const wrapper = mountComponent()
await flushPromises()
const header = wrapper.find('.files-list__header')
const firstChild = header.element.children[0]
expect(firstChild.classList.contains('request-picker-stub')).toBe(true)
})
it('renders FileListFilters in the header before the grid toggle button', async () => {
const filesStore = useFilesStore()
vi.spyOn(filesStore, 'getAllFiles').mockResolvedValue({})
const wrapper = mountComponent()
await flushPromises()
const header = wrapper.find('.files-list__header')
const filterStub = header.find('.file-list-filters-stub')
const gridButton = header.find('.files-list__header-grid-button')
expect(filterStub.exists()).toBe(true)
expect(gridButton.exists()).toBe(true)
// FileListFilters must appear before the grid button in the DOM
const children = Array.from(header.element.children)
const filterIndex = children.findIndex(el => el.classList.contains('file-list-filters-stub'))
const gridIndex = children.findIndex(el => el.classList.contains('files-list__header-grid-button'))
expect(filterIndex).toBeLessThan(gridIndex)
})
it('calls filesStore.updateAllFiles once more when reload button is clicked', async () => {
const filesStore = useFilesStore()
vi.spyOn(filesStore, 'getAllFiles').mockResolvedValue({})
const updateSpy = vi.spyOn(filesStore, 'updateAllFiles').mockResolvedValue({})
const wrapper = mountComponent()
await flushPromises()
const callsBefore = updateSpy.mock.calls.length
await wrapper.find('.nc-action-button-stub').trigger('click')
expect(updateSpy.mock.calls.length).toBe(callsBefore + 1)
})
it('shows empty-state request action when user can request sign', async () => {
const filesStore = useFilesStore()
const userConfigStore = useUserConfigStore()
filesStore.canRequestSign = true
filesStore.files = {}
filesStore.ordered = []
userConfigStore.files_list_grid_view = false
vi.spyOn(filesStore, 'getAllFiles').mockResolvedValue({})
const wrapper = mountComponent()
await flushPromises()
const pickers = wrapper.findAll('.request-picker-stub')
expect(pickers).toHaveLength(2)
})
it('hides empty-state request action when user cannot request sign', async () => {
const filesStore = useFilesStore()
const userConfigStore = useUserConfigStore()
filesStore.canRequestSign = false
filesStore.files = {}
filesStore.ordered = []
userConfigStore.files_list_grid_view = false
vi.spyOn(filesStore, 'getAllFiles').mockResolvedValue({})
const wrapper = mountComponent()
await flushPromises()
const pickers = wrapper.findAll('.request-picker-stub')
expect(pickers).toHaveLength(1)
})
it('renders grid toggle icon path when in list mode', async () => {
const filesStore = useFilesStore()
const userConfigStore = useUserConfigStore()
vi.spyOn(filesStore, 'getAllFiles').mockResolvedValue({})
userConfigStore.files_list_grid_view = false
const wrapper = mountComponent()
await flushPromises()
const gridButton = wrapper.find('.files-list__header-grid-button')
const iconWithPath = gridButton.findAll('.nc-icon').find((node) => !!node.attributes('data-path'))
expect(iconWithPath?.attributes('data-path')).toBe(wrapper.vm.mdiViewGridOutline)
})
it('renders list toggle icon path when in grid mode', async () => {
const filesStore = useFilesStore()
const userConfigStore = useUserConfigStore()
vi.spyOn(filesStore, 'getAllFiles').mockResolvedValue({})
userConfigStore.files_list_grid_view = true
const wrapper = mountComponent()
await flushPromises()
const gridButton = wrapper.find('.files-list__header-grid-button')
const iconWithPath = gridButton.findAll('.nc-icon').find((node) => !!node.attributes('data-path'))
expect(iconWithPath?.attributes('data-path')).toBe(wrapper.vm.mdiFormatListBulletedSquare)
})
})