File tree Expand file tree Collapse file tree 2 files changed +21
-3
lines changed
src/components/NcCheckboxRadioSwitch
tests/unit/components/NcCheckboxRadioSwitch Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -258,7 +258,7 @@ export default {
258
258
class="checkbox-radio-switch"
259
259
:style="cssVars"
260
260
:type="isButtonType ? 'button' : null"
261
- v-bind="isButtonType ? $attrs : {} "
261
+ v-bind="isButtonType ? $attrs : dataAttrs "
262
262
v-on="isButtonType ? listeners : null">
263
263
<input v-if="!isButtonType"
264
264
:id="id"
@@ -272,7 +272,7 @@ export default {
272
272
:indeterminate.prop="hasIndeterminate ? indeterminate : null"
273
273
:required="required"
274
274
:name="name"
275
- v-bind="$attrs "
275
+ v-bind="nonDataAttrs "
276
276
v-on="listeners">
277
277
<NcCheckboxContent :id="id"
278
278
class="checkbox-radio-switch__content"
@@ -450,6 +450,18 @@ export default {
450
450
emits: ['update:checked'],
451
451
452
452
computed: {
453
+ dataAttrs() {
454
+ // filter all data attributes
455
+ return Object.fromEntries(Object.entries(this.$attrs)
456
+ .filter(([key]) => key.startsWith('data-')))
457
+ },
458
+
459
+ nonDataAttrs() {
460
+ // filter all non-data attributes
461
+ return Object.fromEntries(Object.entries(this.$attrs)
462
+ .filter(([key]) => !key.startsWith('data-')))
463
+ },
464
+
453
465
isButtonType() {
454
466
return this.type === TYPE_BUTTON
455
467
},
Original file line number Diff line number Diff line change @@ -17,19 +17,25 @@ describe('NcCheckboxRadioSwitch', () => {
17
17
expect ( wrapper . text ( ) ) . toContain ( 'Test' )
18
18
} )
19
19
20
- it ( 'forwards aria-invalid and aria-errormessage to input' , ( ) => {
20
+ it ( 'forwards all but data- attributes to the input' , ( ) => {
21
21
const wrapper = shallowMount ( NcCheckboxRadioSwitch , {
22
22
slots : {
23
23
default : 'Test' ,
24
24
} ,
25
25
attrs : {
26
26
'aria-invalid' : 'true' ,
27
27
'aria-errormessage' : 'id-test' ,
28
+ 'data-test' : 'checkbox-test-data-attr' ,
29
+ title : 'Test title' ,
28
30
} ,
29
31
} )
30
32
31
33
const input = wrapper . find ( 'input' )
32
34
expect ( input . attributes ( 'aria-invalid' ) ) . toBe ( 'true' )
33
35
expect ( input . attributes ( 'aria-errormessage' ) ) . toBe ( 'id-test' )
36
+ expect ( input . attributes ( 'title' ) ) . toBe ( 'Test title' )
37
+ expect ( input . attributes ( 'data-test' ) ) . not . toBe ( 'checkbox-test-data-attr' )
38
+ expect ( wrapper . attributes ( 'data-test' ) ) . toBe ( 'checkbox-test-data-attr' )
39
+ expect ( wrapper . attributes ( 'title' ) ) . not . toBe ( 'Test title' )
34
40
} )
35
41
} )
You can’t perform that action at this time.
0 commit comments