feat(app): search, sort and date format of the admin sheet list

This commit is contained in:
bchanot committed 2026-11-16 16:45:23 +01:00
1 parent d5852f5f8e
commit f3b58abe70
5 files changed
+121 -5

No files matched your search

@@ -0,0 +1,44 @@
import { describe, expect, it } from 'vitest'
import type { OfficialSheetAdminSummary } from '@faunosteo/supabase'
import { searchSheets, sortSheets } from '@/admin/sheetList'
const sheet = (id: string, fr: string, en: string, at: string) =>
({ sheet_id: id, name: { fr, en }, last_modified_at: at }) as OfficialSheetAdminSummary
const A = sheet('a', 'Âne, squelette', 'Donkey, skeleton', '2026-11-03T10:02:00+00:00')
const C = sheet('c', 'Cheval (tête)', 'Horse (head)', '2026-11-09T11:06:00.123456+00:00')
const E = sheet('e', 'Émeu', 'Emu', '2026-11-05T15:20:00+00:00')
const Z = sheet('z', 'Zèbre', 'Zebra', '2026-11-05T15:20:00+00:00')
const ids = (list: OfficialSheetAdminSummary[]) => list.map((s) => s.sheet_id).join('')
describe('searchSheets', () => {
it('matches the French or the English name, accents and case ignored', () => {
expect(ids(searchSheets([A, C, E, Z], 'ane'))).toBe('a')
expect(ids(searchSheets([A, C, E, Z], 'HORSE'))).toBe('c')
expect(ids(searchSheets([A, C, E, Z], 'émeu'))).toBe('e')
})
it('ignores surrounding spaces; an empty search keeps every sheet; brackets are plain text', () => {
expect(ids(searchSheets([A, C], ' cheval '))).toBe('c')
expect(ids(searchSheets([A, C], ' '))).toBe('ac')
expect(ids(searchSheets([A, C], '(tête)'))).toBe('c')
})
})
describe('sortSheets', () => {
it('modified: newest first, equal dates by name', () => {
expect(ids(sortSheets([A, Z, E, C], 'modified', 'fr'))).toBe('ceza')
})
it('name: the collation of the interface language, not code points', () => {
expect(ids(sortSheets([Z, E, C, A], 'name', 'fr'))).toBe('acez')
expect(ids(sortSheets([Z, E, C, A], 'name', 'en'))).toBe('aecz')
})
it('equal names fall back to the id, whatever the input order', () => {
const twin = { ...A, sheet_id: 'b' }
expect(ids(sortSheets([twin, A], 'name', 'fr'))).toBe('ab')
expect(ids(sortSheets([A, twin], 'modified', 'fr'))).toBe('ab')
})
it('returns a new array and leaves its input alone', () => {
const input = [Z, A]
expect(sortSheets(input, 'name', 'fr')).not.toBe(input)
expect(ids(input)).toBe('za')
})
})