import { describe, expect, it } from 'vitest' import { computed, ref } from 'vue' import { useListSelection } from '../../composables/useListSelection' describe('useListSelection', () => { it('toggles single values and all configured items', () => { const selected = ref(['A']) const selection = useListSelection({ selected, items: ['A', 'B', 'C'], }) expect(selection.isSelected('A')).toBe(true) expect(selection.toggleValue('A')).toEqual([]) expect(selection.toggleValue('B')).toEqual(['A', 'B']) expect(selection.isAllSelected.value).toBe(false) selected.value = ['A', 'B', 'C'] expect(selection.isAllSelected.value).toBe(true) expect(selection.toggleAll()).toEqual([]) }) it('reports full and partial batch selection', () => { const selection = useListSelection({ selected: ['A', 'B'], }) expect(selection.isFullySelected(['A', 'B'])).toBe(true) expect(selection.isPartiallySelected(['A', 'C'])).toBe(true) expect(selection.isPartiallySelected(['C', 'D'])).toBe(false) expect(selection.toggleValues(['A', 'B'])).toEqual([]) expect(selection.toggleValues(['B', 'C'])).toEqual(['A', 'B', 'C']) }) it('uses item value adapters for object lists', () => { const selection = useListSelection({ selected: computed(() => [2]), items: [ { id: 1, label: 'Control' }, { id: 2, label: 'Treatment' }, ], getValue: item => item.id, }) expect(selection.itemValues.value).toEqual([1, 2]) expect(selection.isAllSelected.value).toBe(false) expect(selection.selectAll()).toEqual([2, 1]) }) it('respects disabled and max-selection constraints', () => { const disabledSelection = useListSelection({ selected: ['A'], disabled: true, }) expect(disabledSelection.toggleValue('B')).toEqual(['A']) expect(disabledSelection.clear()).toEqual(['A']) const limitedSelection = useListSelection({ selected: ['A'], max: 1, }) expect(limitedSelection.canAddMore.value).toBe(false) expect(limitedSelection.toggleValue('B')).toEqual(['A']) expect(limitedSelection.toggleValue('A')).toEqual([]) }) })