Skip to content

Commit

Permalink
fix(NcCheckboxRadioSwitch): Do not use HTML attribute name id as pr…
Browse files Browse the repository at this point in the history
…operty name

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed May 10, 2024
1 parent d828323 commit 83c8b38
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
11 changes: 1 addition & 10 deletions src/components/NcCheckboxRadioSwitch/NcCheckboxContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
-->

<template>
<span :id="!isButtonType ? `${id}-label` : undefined"
class="checkbox-content"
<span class="checkbox-content"
:class="{
['checkbox-content-' + type]: true,
'checkbox-content--button-variant': buttonVariant,
Expand Down Expand Up @@ -84,14 +83,6 @@ export default {
},

props: {
/**
* Unique id attribute of the input to label
*/
id: {
type: String,
default: null,
},

/**
* Class for the icon element
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export default {
:name="name"
v-bind="$attrs"
v-on="listeners">
<NcCheckboxContent :id="id"
<NcCheckboxContent :id="!isButtonType ? `${id}-label` : undefined"
class="checkbox-radio-switch__content"
icon-class="checkbox-radio-switch__icon"
text-class="checkbox-radio-switch__text"
Expand Down
31 changes: 30 additions & 1 deletion tests/unit/components/NcCheckboxRadioSwitch/checkbox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
*/

import { shallowMount } from '@vue/test-utils'
import { mount, shallowMount } from '@vue/test-utils'
import NcCheckboxRadioSwitch from '../../../../src/components/NcCheckboxRadioSwitch/NcCheckboxRadioSwitch.vue'

describe('NcCheckboxRadioSwitch', () => {
Expand Down Expand Up @@ -49,4 +49,33 @@ describe('NcCheckboxRadioSwitch', () => {
expect(input.attributes('aria-invalid')).toBe('true')
expect(input.attributes('aria-errormessage')).toBe('id-test')
})

it('sets aria-labelledby attribute correctly', () => {
const wrapper = mount(NcCheckboxRadioSwitch, {
propsData: {
id: 'test-id',
},
slots: {
default: 'Test',
},
})

expect(wrapper.find('input').attributes('aria-labelledby')).toBe('test-id-label')
expect(wrapper.findComponent({ name: 'NcCheckboxContent' }).attributes('id')).toBe('test-id-label')
})

it('does not set id on button content', () => {
const wrapper = mount(NcCheckboxRadioSwitch, {
propsData: {
id: 'test-id',
type: 'button',
},
slots: {
default: 'Test',
},
})

expect(wrapper.find('input').exists()).toBe(false)
expect(wrapper.findComponent({ name: 'NcCheckboxContent' }).attributes('id')).toBe(undefined)
})
})

0 comments on commit 83c8b38

Please sign in to comment.