-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminSelectOptionsSection.spec.ts
More file actions
138 lines (122 loc) · 4.08 KB
/
AdminSelectOptionsSection.spec.ts
File metadata and controls
138 lines (122 loc) · 4.08 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
// SPDX-FileCopyrightText: 2026 LibreCode coop and LibreCode contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
import { describe, expect, it, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import { defineComponent } from 'vue'
import AdminSelectOptionsSection from '../../../components/admin/AdminSelectOptionsSection.vue'
vi.mock('@nextcloud/l10n', () => ({
n: (_app: string, singular: string, plural: string, count: number, parameters?: Record<string, string | number>) => {
const template = count === 1 ? singular : plural
if (parameters === undefined) {
return `tr:${template}`
}
return Object.entries(parameters).reduce((translated, [key, value]) => translated.replace(`{${key}}`, String(value)), `tr:${template}`)
},
t: (_app: string, text: string, parameters?: Record<string, string | number>) => {
if (parameters === undefined) {
return `tr:${text}`
}
return Object.entries(parameters).reduce((translated, [key, value]) => translated.replace(`{${key}}`, String(value)), `tr:${text}`)
},
}))
vi.mock('@nextcloud/vue', () => ({
NcActionButton: defineComponent({ template: '<div><slot /><slot name="icon" /></div>' }),
NcActions: defineComponent({ template: '<div><slot /><slot name="icon" /></div>' }),
NcButton: defineComponent({
name: 'NcButton',
emits: ['click'],
template: '<button type="button" v-bind="$attrs" @click="$emit(\'click\', $event)"><slot /></button>',
}),
NcIconSvgWrapper: defineComponent({ template: '<div />' }),
NcInputField: defineComponent({ template: '<div />' }),
}))
vi.mock('@nextcloud/vue/components/NcDialog', () => ({
default: defineComponent({ template: '<div><slot /><slot name="actions" /></div>' }),
}))
vi.mock('@nextcloud/vue/components/NcTextArea', () => ({
default: defineComponent({ template: '<textarea />' }),
}))
const DraggableStub = defineComponent({
name: 'Draggable',
props: {
modelValue: {
type: Array,
required: true,
},
},
template: '<div><slot v-for="(item, index) in modelValue" :key="item.id" :element="item" :index="index" /></div>',
})
describe('AdminSelectOptionsSection', () => {
it('renders translated headings and pluralized meta', () => {
const wrapper = mount(AdminSelectOptionsSection, {
props: {
modelValue: [{ id: 'option-0', value: 'Alpha' }],
isSaving: false,
},
global: {
stubs: {
Draggable: DraggableStub,
NcDialog: false,
NcTextArea: false,
NcActionButton: false,
NcActions: false,
NcIconSvgWrapper: false,
NcInputField: false,
},
},
})
expect(wrapper.text()).toContain('tr:Options')
expect(wrapper.text()).toContain('tr:Option')
expect(wrapper.text()).toContain('tr:Add single option')
})
it('emits updated model when adding a new option', async() => {
const wrapper = mount(AdminSelectOptionsSection, {
props: {
modelValue: [{ id: 'option-0', value: 'Alpha' }],
isSaving: false,
},
global: {
stubs: {
Draggable: DraggableStub,
NcDialog: false,
NcTextArea: false,
NcActionButton: false,
NcActions: false,
NcIconSvgWrapper: false,
NcInputField: false,
},
},
})
const addButton = wrapper.find('[data-testid="profile-fields-admin-add-option"]')
await addButton.trigger('click')
const emissions = wrapper.emitted('update:modelValue')
expect(emissions).toBeTruthy()
expect((emissions as any[])[0][0]).toHaveLength(2)
expect((emissions as any[])[0][0][1].value).toBe('')
})
it('does not emit add when there is already an empty option', async() => {
const wrapper = mount(AdminSelectOptionsSection, {
props: {
modelValue: [
{ id: 'option-0', value: 'Alpha' },
{ id: 'option-1', value: '' },
],
isSaving: false,
},
global: {
stubs: {
Draggable: DraggableStub,
NcDialog: false,
NcTextArea: false,
NcActionButton: false,
NcActions: false,
NcIconSvgWrapper: false,
NcInputField: false,
},
},
})
const addButton = wrapper.find('[data-testid="profile-fields-admin-add-option"]')
await addButton.trigger('click')
expect(wrapper.emitted('update:modelValue')).toBeUndefined()
})
})