59 lines
2.2 KiB
TypeScript
59 lines
2.2 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { applySort } from './useDictTableState'
|
|
|
|
describe('applySort', () => {
|
|
type Row = { id: string; name: string; age: number | null; ts: string }
|
|
const rows: Row[] = [
|
|
{ id: 'b', name: 'Beta', age: 30, ts: '2026-01-02T00:00:00Z' },
|
|
{ id: 'a', name: 'Alpha', age: null, ts: '2026-01-01T00:00:00Z' },
|
|
{ id: 'c', name: 'gamma', age: 25, ts: '2026-01-03T00:00:00Z' },
|
|
]
|
|
const get = (r: Row, col: string): unknown =>
|
|
col === 'id' ? r.id : col === 'name' ? r.name : col === 'age' ? r.age : r.ts
|
|
|
|
it('returns original when sort=null', () => {
|
|
expect(applySort(rows, null, get)).toEqual(rows)
|
|
})
|
|
|
|
it('sorts strings asc with numeric collation (case-insensitive)', () => {
|
|
const out = applySort(rows, { col: 'name', dir: 'asc' }, get).map((r) => r.id)
|
|
expect(out).toEqual(['a', 'b', 'c'])
|
|
})
|
|
|
|
it('sorts strings desc', () => {
|
|
const out = applySort(rows, { col: 'name', dir: 'desc' }, get).map((r) => r.id)
|
|
expect(out).toEqual(['c', 'b', 'a'])
|
|
})
|
|
|
|
it('puts null at the end for asc', () => {
|
|
const out = applySort(rows, { col: 'age', dir: 'asc' }, get).map((r) => r.id)
|
|
expect(out).toEqual(['c', 'b', 'a'])
|
|
})
|
|
|
|
it('puts null at the end for desc too', () => {
|
|
const out = applySort(rows, { col: 'age', dir: 'desc' }, get).map((r) => r.id)
|
|
expect(out[2]).toBe('a')
|
|
})
|
|
|
|
it('sorts numeric strings naturally (BK-10 > BK-2)', () => {
|
|
type R = { bk: string }
|
|
const items: R[] = [{ bk: 'BK-10' }, { bk: 'BK-2' }, { bk: 'BK-100' }]
|
|
const out = applySort(items, { col: 'bk', dir: 'asc' }, (r) => r.bk).map((r) => r.bk)
|
|
expect(out).toEqual(['BK-2', 'BK-10', 'BK-100'])
|
|
})
|
|
|
|
it('sorts ISO date strings asc (lex == chronological)', () => {
|
|
const out = applySort(rows, { col: 'ts', dir: 'asc' }, get).map((r) => r.id)
|
|
expect(out).toEqual(['a', 'b', 'c'])
|
|
})
|
|
|
|
it('does not mutate input', () => {
|
|
const before = [...rows]
|
|
applySort(rows, { col: 'name', dir: 'desc' }, get)
|
|
expect(rows).toEqual(before)
|
|
})
|
|
})
|
|
|
|
// useDictTableState localStorage interactions покрываются E2E (browser)
|
|
// — здесь только applySort logic, чистая функция без DOM dependencies.
|