-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathSettings.spec.ts
More file actions
469 lines (372 loc) · 13.4 KB
/
Settings.spec.ts
File metadata and controls
469 lines (372 loc) · 13.4 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
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import type { MockedFunction } from 'vitest'
import { mount } from '@vue/test-utils'
import type { VueWrapper } from '@vue/test-utils'
import type { TranslationFunction } from '../../test-types'
type SettingsComponent = typeof import('../../../components/Settings/Settings.vue').default
type AuthModule = typeof import('@nextcloud/auth')
const t: TranslationFunction = (_app, text) => text
let Settings: SettingsComponent
let auth: AuthModule
let getCurrentUserMock: MockedFunction<typeof import('@nextcloud/auth').getCurrentUser>
vi.mock('@nextcloud/auth', () => ({
getCurrentUser: vi.fn(() => ({
isAdmin: false,
})),
}))
vi.mock('@nextcloud/l10n', () => ({
translate: vi.fn(t),
translatePlural: vi.fn((app: string, singular: string, plural: string, count: number) => (count === 1 ? singular : plural)),
t: vi.fn(t),
n: vi.fn((app: string, singular: string, plural: string, count: number) => (count === 1 ? singular : plural)),
getLanguage: vi.fn(() => 'en'),
getLocale: vi.fn(() => 'en'),
isRTL: vi.fn(() => false),
}))
vi.mock('@nextcloud/router', () => ({
generateUrl: vi.fn((url) => `/admin/${url}`),
}))
beforeAll(async () => {
auth = await import('@nextcloud/auth')
getCurrentUserMock = auth.getCurrentUser as MockedFunction<typeof auth.getCurrentUser>
;({ default: Settings } = await import('../../../components/Settings/Settings.vue'))
})
describe('Settings', () => {
let wrapper: ReturnType<typeof mount> | null
const expectItem = (item: VueWrapper<unknown> | undefined) => {
expect(item).toBeDefined()
if (!item) {
throw new Error('Expected navigation item to be defined')
}
return item
}
const findItemByName = (items: Array<VueWrapper<unknown>>, name: string) => {
return items.find((item) => {
const propName = item.props('name') as string | undefined
if (!propName) {
return false
}
return propName.includes(name)
})
}
const getWrapper = () => {
if (!wrapper) {
throw new Error('Expected wrapper to be mounted')
}
return wrapper
}
const getItems = () => getWrapper().findAllComponents({ name: 'NcAppNavigationItem' })
const expectItemAt = (items: Array<VueWrapper<unknown>>, index: number) => {
const item = items.at(index)
expect(item).toBeDefined()
if (!item) {
throw new Error('Expected navigation item to be defined')
}
return item
}
const createWrapper = (isAdmin = false) => {
const user = { isAdmin } as ReturnType<typeof auth.getCurrentUser>
getCurrentUserMock.mockReturnValue(user)
return mount(Settings, {
global: {
stubs: {
NcAppNavigationItem: {
name: 'NcAppNavigationItem',
props: ['name', 'to', 'href', 'icon'],
template: '<li><slot name="icon" /><span class="item-name">{{ name }}</span><slot /></li>',
},
AccountIcon: { template: '<div class="account-icon"></div>' },
StarIcon: { template: '<div class="star-icon"></div>' },
TuneIcon: { template: '<div class="tune-icon"></div>' },
},
mocks: {
t,
},
},
})
}
beforeEach(() => {
if (wrapper) {
wrapper.unmount()
wrapper = null
}
vi.clearAllMocks()
})
describe('RULE: Account navigation item always displays', () => {
it('shows Account item for non-admin user', () => {
wrapper = createWrapper(false)
const items = getItems()
const accountItem = expectItem(findItemByName(items, 'Account'))
expect(accountItem.props('to')).toEqual({ name: 'Account' })
expect(accountItem).toBeTruthy()
})
it('shows Account item for admin user', () => {
wrapper = createWrapper(true)
const items = getItems()
const accountItem = expectItem(findItemByName(items, 'Account'))
expect(accountItem).toBeTruthy()
})
it('Account item links to Account route', () => {
wrapper = createWrapper()
const items = getItems()
const accountItem = expectItem(findItemByName(items, 'Account'))
expect(accountItem.props('to')).toEqual({ name: 'Account' })
})
it('Account item has user icon', () => {
wrapper = createWrapper()
const accountIcon = getWrapper().find('.account-icon')
expect(accountIcon.exists()).toBe(true)
})
})
describe('RULE: Administration item shows only for admin users', () => {
it('hides Administration for non-admin users', () => {
wrapper = createWrapper(false)
const items = getItems()
const adminItem = findItemByName(items, 'Administration')
expect(adminItem).toBeUndefined()
})
it('shows Administration for admin users', () => {
wrapper = createWrapper(true)
const items = getItems()
const adminItem = expectItem(findItemByName(items, 'Administration'))
expect(adminItem).toBeTruthy()
})
it('Administration item has tune icon', () => {
wrapper = createWrapper(true)
const tuneIcon = getWrapper().find('.tune-icon')
expect(tuneIcon.exists()).toBe(true)
})
it('Administration href points to admin settings', () => {
wrapper = createWrapper(true)
const items = getItems()
const adminItem = expectItem(findItemByName(items, 'Administration'))
expect(adminItem.props('href')).toContain('settings/admin/libresign')
})
})
describe('RULE: getAdminRoute generates correct URL', () => {
it('generates admin route with generateUrl', () => {
wrapper = createWrapper(true)
const route = getWrapper().vm.getAdminRoute()
expect(route).toContain('settings/admin/libresign')
})
it('returns formatted admin settings URL', () => {
wrapper = createWrapper(true)
const route = getWrapper().vm.getAdminRoute()
expect(route).toBe('/admin/settings/admin/libresign')
})
})
describe('RULE: Rate LibreSign item always displays', () => {
it('shows Rate item for non-admin', () => {
wrapper = createWrapper(false)
const items = getItems()
const rateItem = expectItem(findItemByName(items, 'Rate'))
expect(rateItem).toBeTruthy()
})
it('shows Rate item for admin', () => {
wrapper = createWrapper(true)
const items = getItems()
const rateItem = expectItem(findItemByName(items, 'Rate'))
expect(rateItem).toBeTruthy()
})
it('Rate item has star icon', () => {
wrapper = createWrapper()
const starIcon = getWrapper().find('.star-icon')
expect(starIcon.exists()).toBe(true)
})
it('Rate item links to apps marketplace', () => {
wrapper = createWrapper()
const items = getItems()
const rateItem = expectItem(findItemByName(items, 'Rate'))
expect(rateItem.props('href')).toContain('apps.nextcloud.com')
})
it('Rate item URL includes comments section', () => {
wrapper = createWrapper()
const items = getItems()
const rateItem = expectItem(findItemByName(items, 'Rate'))
expect(rateItem.props('href')).toContain('comments')
})
it('Rate item displays with heart emoji', () => {
wrapper = createWrapper()
const items = getItems()
const rateItem = expectItem(findItemByName(items, 'Rate'))
expect(rateItem.props('name')).toContain('❤️')
})
})
describe('RULE: isAdmin data property reflects user role', () => {
it('isAdmin false for non-admin user', () => {
wrapper = createWrapper(false)
expect(getWrapper().vm.isAdmin).toBe(false)
})
it('isAdmin true for admin user', () => {
wrapper = createWrapper(true)
expect(getWrapper().vm.isAdmin).toBe(true)
})
})
describe('RULE: unauthenticated users (signing via email link) do not crash the component', () => {
const createUnauthenticatedWrapper = () => {
getCurrentUserMock.mockReturnValue(null)
return mount(Settings, {
global: {
stubs: {
NcAppNavigationItem: {
name: 'NcAppNavigationItem',
props: ['name', 'to', 'href', 'icon'],
template: '<li><slot name="icon" /><span class="item-name">{{ name }}</span><slot /></li>',
},
AccountIcon: { template: '<div class="account-icon"></div>' },
StarIcon: { template: '<div class="star-icon"></div>' },
TuneIcon: { template: '<div class="tune-icon"></div>' },
},
mocks: { t },
},
})
}
it('mounts without throwing when getCurrentUser returns null', () => {
expect(() => createUnauthenticatedWrapper()).not.toThrow()
})
it('isAdmin is false when getCurrentUser returns null', () => {
wrapper = createUnauthenticatedWrapper()
expect(getWrapper().vm.isAdmin).toBe(false)
})
it('hides the Administration link when user is unauthenticated', () => {
wrapper = createUnauthenticatedWrapper()
const items = getItems()
const adminItem = findItemByName(items, 'Administration')
expect(adminItem).toBeUndefined()
})
it('shows 2 navigation items for unauthenticated user', () => {
wrapper = createUnauthenticatedWrapper()
const items = getItems()
// Account + Rate = 2
expect(items.length).toBe(2)
})
})
describe('RULE: navigation items count depends on admin status', () => {
it('shows 2 items for non-admin', () => {
wrapper = createWrapper(false)
const items = getItems()
// Account + Rate = 2
expect(items.length).toBe(2)
})
it('shows 3 items for admin', () => {
wrapper = createWrapper(true)
const items = getItems()
// Account + Administration + Rate = 3
expect(items.length).toBe(3)
})
})
describe('RULE: each navigation item has name and icon', () => {
it('Account item has all properties', () => {
wrapper = createWrapper()
const items = getItems()
const accountItem = expectItem(findItemByName(items, 'Account'))
expect(accountItem.props('name')).toBeTruthy()
})
it('Rate item has all properties', () => {
wrapper = createWrapper()
const items = getItems()
const rateItem = expectItem(findItemByName(items, 'Rate'))
expect(rateItem.props('name')).toBeTruthy()
expect(rateItem.props('href')).toBeTruthy()
})
it('Admin item has all properties when present', () => {
wrapper = createWrapper(true)
const items = getItems()
const adminItem = expectItem(findItemByName(items, 'Administration'))
expect(adminItem.props('name')).toBeTruthy()
expect(adminItem.props('href')).toBeTruthy()
})
})
describe('RULE: icons render correctly', () => {
it('renders all required icons for admin', () => {
wrapper = createWrapper(true)
expect(getWrapper().find('.account-icon').exists()).toBe(true)
expect(getWrapper().find('.tune-icon').exists()).toBe(true)
expect(getWrapper().find('.star-icon').exists()).toBe(true)
})
it('renders Account and Rate icons for non-admin', () => {
wrapper = createWrapper(false)
expect(getWrapper().find('.account-icon').exists()).toBe(true)
expect(getWrapper().find('.star-icon').exists()).toBe(true)
})
it('does not render admin icon for non-admin', () => {
wrapper = createWrapper(false)
expect(getWrapper().find('.tune-icon').exists()).toBe(false)
})
})
describe('RULE: navigation items render in correct order', () => {
it('renders Account first for non-admin', () => {
wrapper = createWrapper(false)
const items = getItems()
const firstItem = expectItemAt(items, 0)
expect(firstItem.props('name')).toContain('Account')
})
it('renders Account first for admin', () => {
wrapper = createWrapper(true)
const items = getItems()
const firstItem = expectItemAt(items, 0)
expect(firstItem.props('name')).toContain('Account')
})
it('renders Administration second for admin (if present)', () => {
wrapper = createWrapper(true)
const items = getItems()
const secondItem = expectItemAt(items, 1)
expect(secondItem.props('name')).toContain('Administration')
})
it('renders Rate last', async () => {
wrapper = createWrapper(true)
const items = getItems()
const lastItem = expectItemAt(items, items.length - 1)
expect(lastItem.props('name')).toContain('Rate')
})
})
describe('RULE: component wraps items in unordered list', () => {
it('contains ul element', () => {
wrapper = createWrapper()
const ul = getWrapper().find('ul')
expect(ul.exists()).toBe(true)
})
it('navigation items are rendered as children of ul', () => {
wrapper = createWrapper()
const ul = getWrapper().find('ul')
const items = ul.findAllComponents({ name: 'NcAppNavigationItem' })
expect(items.length).toBeGreaterThan(0)
})
})
describe('RULE: complete workflow for different user roles', () => {
it('provides different experience for regular user', () => {
wrapper = createWrapper(false)
const items = getItems()
expect(items).toHaveLength(2)
const hasAccount = items.some(i => i.props('name')?.includes('Account'))
const hasRate = items.some(i => i.props('name')?.includes('Rate'))
const hasAdmin = items.some(i => i.props('name')?.includes('Administration'))
expect(hasAccount).toBe(true)
expect(hasRate).toBe(true)
expect(hasAdmin).toBe(false)
})
it('provides full experience for admin user', () => {
wrapper = createWrapper(true)
const items = getItems()
expect(items).toHaveLength(3)
const hasAccount = items.some(i => i.props('name')?.includes('Account'))
const hasRate = items.some(i => i.props('name')?.includes('Rate'))
const hasAdmin = items.some(i => i.props('name')?.includes('Administration'))
expect(hasAccount).toBe(true)
expect(hasRate).toBe(true)
expect(hasAdmin).toBe(true)
})
})
describe('RULE: Account item icon configuration', () => {
it('Account item has icon prop', () => {
wrapper = createWrapper()
const items = getItems()
const accountItem = expectItem(findItemByName(items, 'Account'))
expect(accountItem.props('icon')).toBe('icon-user')
})
})
})