Files
nuxt__ui/test/components/CheckboxGroup.spec.ts
Francesco Mussoni 07f3fe8dac feat(CheckboxGroup/RadioGroup): support icon in items (#6726)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
2026-08-21 12:06:57 +02:00

197 lines
8.8 KiB
TypeScript

import { describe, it, expect, test } from 'vitest'
import { axe } from 'vitest-axe'
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import CheckboxGroup from '../../src/runtime/components/CheckboxGroup.vue'
import Checkbox from '../../src/runtime/components/Checkbox.vue'
import type { FormInputEvents } from '../../src/module'
import { renderForm } from '../utils/form'
import theme from '#build/ui/checkbox-group'
import themeCheckbox from '#build/ui/checkbox'
import { mountSuspended } from '@nuxt/test-utils/runtime'
import { renderEach } from '../component-render'
describe('CheckboxGroup', () => {
const sizes = Object.keys(theme.variants.size) as any
const variants = Object.keys(theme.variants.variant) as any
const indicators = Object.keys(themeCheckbox.variants.indicator) as any
const items = [
{ value: '1', label: 'Option 1' },
{ value: '2', label: 'Option 2' },
{ value: '3', label: 'Option 3' }
]
const props = { items }
renderEach(CheckboxGroup, [
['with items', { props }],
['with modelValue', { props: { ...props, modelValue: ['1'] } }],
['with defaultValue', { props: { ...props, defaultValue: ['1'] } }],
['with valueKey', { props: { ...props, valueKey: 'label', defaultValue: ['Option 1'] } }],
['with labelKey', { props: { ...props, labelKey: 'value' } }],
['with descriptionKey', { props: { ...props, descriptionKey: 'value' } }],
['with disabled', { props: { ...props, disabled: true } }],
['with description', { props: { items: items.map((opt, count) => ({ ...opt, description: `Description ${count}` })) } }],
['with icon', { props: { items: items.map(opt => ({ ...opt, icon: 'i-lucide-rocket' })), indicator: 'hidden' } }],
['with icon card', { props: { items: items.map(opt => ({ ...opt, icon: 'i-lucide-rocket' })), indicator: 'hidden', variant: 'card', defaultValue: ['1'] } }],
['with icon table', { props: { items: items.map(opt => ({ ...opt, icon: 'i-lucide-rocket' })), indicator: 'hidden', variant: 'table', defaultValue: ['1'] } }],
['with highlight indicator hidden', { props: { ...props, indicator: 'hidden', variant: 'card', highlight: true, defaultValue: ['1'] } }],
['with required', { props: { ...props, legend: 'Legend', required: true } }],
...sizes.map((size: string) => [`with size ${size}`, { props: { ...props, size, defaultValue: ['1'] } }]),
...variants.map((variant: string) => [`with primary variant ${variant}`, { props: { ...props, variant, defaultValue: ['1'] } }]),
...variants.map((variant: string) => [`with primary variant ${variant} highlight`, { props: { ...props, variant, highlight: true, defaultValue: ['1'] } }]),
...variants.map((variant: string) => [`with neutral variant ${variant}`, { props: { ...props, variant, color: 'neutral', defaultValue: ['1'] } }]),
...variants.map((variant: string) => [`with neutral variant ${variant} highlight`, { props: { ...props, variant, color: 'neutral', highlight: true, defaultValue: ['1'] } }]),
...variants.map((variant: string) => [`with horizontal variant ${variant}`, { props: { ...props, variant, orientation: 'horizontal', defaultValue: ['1'] } }]),
...indicators.map((indicator: string) => [`with indicator ${indicator}`, { props: { ...props, indicator, defaultValue: ['1'] } }]),
['with ariaLabel', { props, attrs: { 'aria-label': 'Aria label' } }],
['with as', { props: { ...props, as: 'section' } }],
['with class', { props: { ...props, class: 'absolute' } }],
['with ui', { props: { ...props, ui: { fieldset: 'gap-x-4', label: 'text-red' } } }],
// Slots
['with legend slot', { props, slots: { legend: () => 'Legend slot' } }],
['with label slot', { props, slots: { label: () => 'Label slot' } }],
['with description slot', { props, slots: { description: () => 'Description slot' } }]
])
it('passes accessibility tests', async () => {
const wrapper = await mountSuspended(CheckboxGroup, {
props: {
items: [
{ value: '1', label: 'Option 1' },
{ value: '2', label: 'Option 2' }
],
legend: 'Legend'
}
})
expect(await axe(wrapper.element)).toHaveNoViolations()
})
it('forwards an item icon to the checkbox icon prop', async () => {
const wrapper = await mountSuspended(CheckboxGroup, {
props: { items: [{ value: '1', label: 'Option 1', icon: 'i-lucide-rocket' }] }
})
const checkbox = wrapper.findComponent(Checkbox) as unknown as VueWrapper<any>
expect(checkbox.props('icon')).toBe('i-lucide-rocket')
})
it('lets an item icon win over the group icon', async () => {
const wrapper = await mountSuspended(CheckboxGroup, {
props: {
icon: 'i-lucide-heart',
items: [{ value: '1', label: 'Option 1', icon: 'i-lucide-rocket' }, { value: '2', label: 'Option 2' }]
}
})
const checkboxes = wrapper.findAllComponents(Checkbox) as unknown as VueWrapper<any>[]
expect(checkboxes[0]!.props('icon')).toBe('i-lucide-rocket')
expect(checkboxes[1]!.props('icon')).toBe('i-lucide-heart')
})
it('renders an item icon next to the label only when the indicator is hidden', async () => {
const items = [{ value: '1', label: 'Option 1', icon: 'i-lucide-rocket' }]
const wrapper = await mountSuspended(CheckboxGroup, { props: { items } })
expect(wrapper.find('[data-slot="wrapper"] [data-slot="icon"]').exists()).toBe(false)
const hidden = await mountSuspended(CheckboxGroup, { props: { items, indicator: 'hidden' } })
expect(hidden.find('[data-slot="wrapper"] [data-slot="icon"]').exists()).toBe(true)
})
it('renders an item icon without a label', async () => {
const wrapper = await mountSuspended(CheckboxGroup, {
props: { items: [{ value: 'table', icon: 'i-lucide-table' }], indicator: 'hidden' }
})
expect(wrapper.find('[data-slot="wrapper"] [data-slot="icon"]').exists()).toBe(true)
})
it('passes accessibility tests with icon items', async () => {
const wrapper = await mountSuspended(CheckboxGroup, {
props: {
items: [{ value: 'system', label: 'System', icon: 'i-lucide-monitor' }, { value: 'light', label: 'Light', icon: 'i-lucide-sun' }],
variant: 'table',
indicator: 'hidden',
orientation: 'horizontal',
defaultValue: ['system']
}
})
expect(await axe(wrapper.element)).toHaveNoViolations()
})
describe('emits', () => {
test('update:modelValue event', async () => {
const wrapper = mount(CheckboxGroup, { props: { items: ['Option 1', 'Option 2'] } })
const input = wrapper.findComponent({ name: 'CheckboxGroupRoot' })
await input.setValue('Option 1')
expect(wrapper.emitted()).toMatchObject({ 'update:modelValue': [['Option 1']] })
})
test('change event', async () => {
const wrapper = mount(CheckboxGroup, { props: { items: ['Option 1', 'Option 2'] } })
const input = wrapper.findComponent({ name: 'CheckboxGroupRoot' })
await input.setValue('Option 1')
expect(wrapper.emitted()).toMatchObject({ change: [[{ type: 'change' }]] })
})
})
describe('form integration', async () => {
async function createForm(validateOn?: FormInputEvents[]) {
const wrapper = await renderForm({
props: {
validateOn,
validateOnInputDelay: 0,
async validate(state: any) {
if (!state.value?.includes('Option 2'))
return [{ name: 'value', message: 'Error message' }]
return []
}
},
slotVars: {
items: ['Option 1', 'Option 2']
},
slotTemplate: `
<UFormField name="value" label="Checkbox group">
<UCheckboxGroup id="input" v-model="state.value" :items="items" />
</UFormField>
`
})
const input = wrapper.findComponent({ name: 'CheckboxGroupRoot' })
return {
wrapper,
input
}
}
test('validate on change works', async () => {
const { input, wrapper } = await createForm(['change'])
input.setValue(['Option 1'])
await flushPromises()
expect(wrapper.text()).toContain('Error message')
input.setValue(['Option 2'])
await flushPromises()
expect(wrapper.text()).not.toContain('Error message')
})
test('validate on input works', async () => {
const { input, wrapper } = await createForm(['input'])
input.setValue(['Option 1'])
await flushPromises()
expect(wrapper.text()).toContain('Error message')
input.setValue(['Option 2'])
await flushPromises()
expect(wrapper.text()).not.toContain('Error message')
})
test('no label for=... on FormField', async () => {
const { wrapper } = await createForm()
const formFieldLabel = wrapper.findAll('label').map(label => label.attributes()).filter(label => !label.for?.includes(':'))[0]
expect(formFieldLabel?.for).toBeUndefined()
})
})
})