-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathOperation.spec.ts
More file actions
408 lines (336 loc) · 9.74 KB
/
Copy pathOperation.spec.ts
File metadata and controls
408 lines (336 loc) · 9.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
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
/*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { cleanup, render } from '@testing-library/vue'
import { afterEach, describe, expect, it } from 'vitest'
import { nextTick } from 'vue'
import Operation from './Operation.vue'
afterEach(() => {
cleanup()
})
describe('Operation.vue', () => {
const mockOperation = {
name: 'Test Operation',
description: 'This is a test operation',
iconClass: 'icon-test',
icon: '',
}
it('renders operation with required props', () => {
const { getByRole } = render(Operation, {
props: {
operation: mockOperation,
},
})
expect(getByRole('heading', { level: 3 })).toBeTruthy()
expect(getByRole('heading', { level: 3 }).textContent).toBe('Test Operation')
})
it('displays operation name and description', () => {
const { getByText } = render(Operation, {
props: {
operation: mockOperation,
},
})
expect(getByText('Test Operation')).toBeTruthy()
expect(getByText('This is a test operation')).toBeTruthy()
})
it('renders icon with iconClass', () => {
const { container } = render(Operation, {
props: {
operation: mockOperation,
},
})
const icon = container.querySelector('.icon')
expect(icon).toBeTruthy()
expect(icon?.classList.contains('icon-test')).toBe(true)
})
it('renders icon with background image when no iconClass', () => {
const { container } = render(Operation, {
props: {
operation: {
name: 'Test Operation',
description: 'Description',
iconClass: '',
icon: 'data:image/svg+xml;base64,test',
},
},
})
const icon = container.querySelector<HTMLElement>('.icon')
expect(icon).toBeTruthy()
expect(icon?.style.backgroundImage).toContain('data:image/svg+xml;base64,test')
})
it('does not show button when colored is false', () => {
const { queryByRole } = render(Operation, {
props: {
operation: mockOperation,
colored: false,
},
})
expect(queryByRole('button')).toBeFalsy()
})
it('shows button with correct text when colored is true', () => {
const { getByRole } = render(Operation, {
props: {
operation: mockOperation,
colored: true,
},
})
const button = getByRole('button')
expect(button).toBeTruthy()
expect(button.textContent).toContain('Add new flow')
})
it('applies colored class when colored prop is true', () => {
const { container } = render(Operation, {
props: {
operation: mockOperation,
colored: true,
},
})
const item = container.querySelector('.actions__item')
expect(item?.classList.contains('colored')).toBe(true)
})
it('does not apply colored class when colored prop is false', () => {
const { container } = render(Operation, {
props: {
operation: mockOperation,
colored: false,
},
})
const item = container.querySelector('.actions__item')
expect(item?.classList.contains('colored')).toBe(false)
})
it('renders slot content', () => {
const { getByText } = render(Operation, {
props: {
operation: mockOperation,
},
slots: {
default: '<div>Slot content</div>',
},
})
expect(getByText('Slot content')).toBeTruthy()
})
it('applies background color when colored is true and color is provided', async () => {
const { container } = render(Operation, {
props: {
operation: {
...mockOperation,
color: '#ff0000',
},
colored: true,
},
})
await nextTick()
expect(getComponentStyles(container).backgroundColor).toBe('#ff0000')
})
it('updates styles when operation color changes', async () => {
const { updateProps, container } = render(Operation, {
props: {
operation: mockOperation,
colored: false,
},
})
await nextTick()
expect(getComponentStyles(container).backgroundColor).toBe('transparent')
await updateProps({
operation: { ...mockOperation, color: '#00ff00' },
colored: true,
})
expect(getComponentStyles(container).backgroundColor).not.toBe('transparent')
})
describe('backgroundColor watcher', () => {
it('sets text color to var(--color-main-text) when background is transparent', async () => {
const { container } = render(Operation, {
props: {
operation: mockOperation,
colored: false,
},
})
await nextTick()
expect(getComponentStyles(container).color).toBe('var(--color-main-text)')
})
it('sets text color to var(--color-primary-element-text) when background is var(--color-primary-element)', async () => {
const { container } = render(Operation, {
props: {
operation: mockOperation,
colored: true,
},
})
await nextTick()
expect(getComponentStyles(container).color).toBe('var(--color-primary-element-text)')
})
it('sets text color to white (#ffffff) for dark background colors (high contrast)', async () => {
const { container } = render(Operation, {
props: {
operation: {
...mockOperation,
color: '#000000',
},
colored: true,
},
})
await nextTick()
expect(getComponentStyles(container).color).toBe('#ffffff')
})
it('sets text color to black (#000000) for light background colors (low contrast)', async () => {
const { container } = render(Operation, {
props: {
operation: {
...mockOperation,
color: '#ffffff',
},
colored: true,
},
})
await nextTick()
expect(getComponentStyles(container).color).toBe('#000000')
})
it('calculates color based on contrast for gray backgrounds', async () => {
const { container } = render(Operation, {
props: {
operation: {
...mockOperation,
color: '#808080',
},
colored: true,
},
})
await nextTick()
// Gray has contrast ratio < 4.5 with white, so should use black
expect(getComponentStyles(container).color).toBe('#000000')
})
it('applies invert filter to icon when text color is black', async () => {
const { container } = render(Operation, {
props: {
operation: {
...mockOperation,
color: '#ffffff',
},
colored: true,
},
})
await nextTick()
expect(getComponentStyles(container).filter).toBe('invert(100%)')
})
it('does not apply invert filter to icon when text color is not black', async () => {
const { container } = render(Operation, {
props: {
operation: {
...mockOperation,
color: '#000000',
},
colored: true,
},
})
await nextTick()
expect(getComponentStyles(container).filter).toBe('none')
})
it('does not apply invert filter when using CSS variable colors', async () => {
const { container } = render(Operation, {
props: {
operation: mockOperation,
colored: true,
},
})
await nextTick()
expect(getComponentStyles(container).filter).toBe('none')
})
it('handles contrast calculation error gracefully', async () => {
const { container } = render(Operation, {
props: {
operation: {
...mockOperation,
color: 'invalid-color',
},
colored: true,
},
})
await nextTick()
// Should fallback to var(--color-main-text) on error
expect(getComponentStyles(container).color).toBe('var(--color-main-text)')
})
it('updates text color reactively when operation color changes', async () => {
const { container, updateProps } = render(Operation, {
props: {
operation: {
...mockOperation,
color: '#ffffff',
},
colored: true,
},
})
await nextTick()
expect(getComponentStyles(container).color).toBe('#000000')
await updateProps({
operation: {
...mockOperation,
color: '#000000',
},
colored: true,
})
expect(getComponentStyles(container).color).toBe('#ffffff')
})
it('transitions from colored to uncolored updates text color', async () => {
const { container, updateProps } = render(Operation, {
props: {
operation: {
...mockOperation,
color: '#000000',
},
colored: true,
},
})
await nextTick()
expect(getComponentStyles(container).color).toBe('#ffffff')
await updateProps({
operation: mockOperation,
colored: false,
})
expect(getComponentStyles(container).color).toBe('var(--color-main-text)')
})
it('uses computed style for non-hex background colors', async () => {
const { container } = render(Operation, {
props: {
operation: mockOperation,
colored: true,
},
})
await nextTick()
// When colored=true and no color property, uses var(--color-primary-element)
// which is a CSS variable, so it uses the computed style path
expect(getComponentStyles(container).color).toBe('var(--color-primary-element-text)')
})
it('watcher runs with immediate:true on mount', async () => {
const { container } = render(Operation, {
props: {
operation: {
...mockOperation,
color: '#000000',
},
colored: true,
},
})
// The watcher should have already run with immediate: true
// so color should be calculated even before nextTick
expect(getComponentStyles(container).color).toBeTruthy()
})
})
})
/**
* Get the computed styles of the component for testing purposes
*
* @param container - The container element
*/
function getComponentStyles(container: Element) {
const element = container.querySelector<HTMLElement>('.actions__item')
const styles = Object.values({ ...element!.style }) // variables are exposed as --HASH-VARNAME
console.error(styles)
const color = element?.style.getPropertyValue(styles.find((key) => (key as string).endsWith('-color'))!.toString())
const backgroundColor = element?.style.getPropertyValue(styles.find((key) => (key as string).endsWith('-backgroundColor'))!.toString())
const filter = element?.style.getPropertyValue(styles.find((key) => (key as string).endsWith('-iconFilter'))!.toString())
return {
color,
backgroundColor,
filter,
}
}