import { describe, expect, it } from 'vitest' import { computed, ref } from 'vue' import { compareSortValues, useSortedItems, type SortDescriptor, } from '../../composables/useSortedItems' describe('useSortedItems', () => { const items = [ { name: 'Lactate', score: 2, supplier: 'Cayman' }, { name: 'Glucose', score: undefined, supplier: 'Sigma' }, { name: 'Pyruvate', score: 5, supplier: 'Thermo' }, ] it('sorts by a reactive descriptor and keeps empty values last', () => { const sort = ref>({ key: 'score', direction: 'asc' }) const sorted = useSortedItems({ items, sort, getValue: (item, key) => item[key], }) expect(sorted.sortedItems.value.map(item => item.name)).toEqual([ 'Lactate', 'Pyruvate', 'Glucose', ]) sort.value = { key: 'score', direction: 'desc' } expect(sorted.sortedItems.value.map(item => item.name)).toEqual([ 'Pyruvate', 'Lactate', 'Glucose', ]) }) it('returns all items in original order when disabled', () => { const enabled = ref(false) const sorted = useSortedItems({ items: computed(() => items), sort: { key: 'name', direction: 'asc' }, enabled, getValue: item => item.name, }) expect(sorted.sortedItems.value.map(item => item.name)).toEqual([ 'Lactate', 'Glucose', 'Pyruvate', ]) enabled.value = true expect(sorted.sortedItems.value.map(item => item.name)).toEqual([ 'Glucose', 'Lactate', 'Pyruvate', ]) }) it('supports case-insensitive string comparison', () => { const sorted = useSortedItems({ items: [ { name: 'beta' }, { name: 'Alpha' }, { name: 'gamma' }, ], sort: { key: 'name', direction: 'asc' }, caseSensitive: false, getValue: item => item.name, }) expect(sorted.sortedItems.value.map(item => item.name)).toEqual([ 'Alpha', 'beta', 'gamma', ]) }) it('compares dates, numbers, strings, and empty values consistently', () => { expect(compareSortValues(1, 2, 'asc')).toBeLessThan(0) expect(compareSortValues(1, 2, 'desc')).toBeGreaterThan(0) expect(compareSortValues(new Date('2026-01-01'), new Date('2026-02-01'))).toBeLessThan(0) expect(compareSortValues(undefined, 2, 'asc')).toBeGreaterThan(0) expect(compareSortValues(undefined, 2, 'desc')).toBeGreaterThan(0) }) })