import { describe, expect, it } from 'vitest'; import { applyClientSort } from './useClientTableSort'; describe('applyClientSort', () => { const rows = [ { id: 'a', qty: 3 }, { id: 'b', qty: 1 }, { id: 'c', qty: null as number | null }, { id: 'd', qty: 1 }, ]; it('returns same reference when no sort key', () => { const out = applyClientSort(rows, null, 'asc', (r) => r.qty); expect(out).toBe(rows); }); it('sorts ascending and keeps stable order for ties; nulls last', () => { const out = applyClientSort(rows, 'qty', 'asc', (r) => r.qty); expect(out.map((r) => r.id)).toEqual(['b', 'd', 'a', 'c']); }); it('sorts descending with nulls last', () => { const out = applyClientSort(rows, 'qty', 'desc', (r) => r.qty); expect(out.map((r) => r.id)).toEqual(['a', 'b', 'd', 'c']); }); it('calls getValue once per row', () => { let calls = 0; applyClientSort(rows, 'qty', 'asc', (r) => { calls += 1; return r.qty; }); expect(calls).toBe(rows.length); }); });