Files
planet/frontend/tests/table-marquee-and-resizer.test.ts
T
linkong e1cc27f13d
ci / backend (push) Canceled after 0s
ci / frontend (push) Canceled after 0s
ci / delivery (push) Canceled after 0s
release: bump version to 0.84.0
2026-12-04 07:07:19 +08:00

266 lines
9.7 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import {
clampSplitPaneWidth,
readStoredSplitWidth,
writeStoredSplitWidth,
} from '../src/admin/components/layout/SplitPaneResizer'
import { computeMarqueeMetrics } from '../src/admin/components/data-table/TableCellMarquee'
import {
computeColumnWidths,
dedupeAppendRows,
doesViewportChangeAffectColumns,
estimateHeaderMinWidth,
findLastDataColumnIndex,
readStoredColumnWidths,
shouldLoadMoreWaterfall,
writeStoredColumnWidths,
} from '../src/admin/components/data-table/tableLayout'
describe('computeMarqueeMetrics', () => {
test('returns no overflow when content fits within clip width plus 1px tolerance', () => {
expect(computeMarqueeMetrics(180, 220)).toEqual({ overflows: false, distancePx: 0, durationSec: 0 })
expect(computeMarqueeMetrics(201, 200)).toEqual({ overflows: false, distancePx: 0, durationSec: 0 })
expect(computeMarqueeMetrics(200, 0)).toEqual({ overflows: false, distancePx: 0, durationSec: 0 })
})
test('computes marquee scroll distance and bounded duration when content overflows', () => {
const shortOverflow = computeMarqueeMetrics(230, 200)
expect(shortOverflow.overflows).toBe(true)
expect(shortOverflow.distancePx).toBe(42)
expect(shortOverflow.durationSec).toBe(2.6)
const longOverflow = computeMarqueeMetrics(1200, 200)
expect(longOverflow.overflows).toBe(true)
expect(longOverflow.distancePx).toBe(1012)
expect(longOverflow.durationSec).toBe(12)
})
})
describe('SplitPaneResizer helpers', () => {
test('clamps pane width within explicit bounds and opposite pane minimum', () => {
expect(clampSplitPaneWidth(120, 220, 520, 1200, 360)).toBe(220)
expect(clampSplitPaneWidth(680, 220, 520, 1200, 360)).toBe(520)
expect(clampSplitPaneWidth(500, 220, 520, 740, 360)).toBe(380)
expect(clampSplitPaneWidth(Number.NaN, 220, 520)).toBe(220)
})
test('reads and writes stored split pane widths safely', () => {
const store = new Map<string, string>()
const originalWindow = globalThis.window
;(globalThis as { window?: unknown }).window = {
localStorage: {
getItem: (key: string) => store.get(key) ?? null,
setItem: (key: string, value: string) => {
store.set(key, value)
},
},
}
try {
expect(readStoredSplitWidth('k.width', 290, 220, 520)).toBe(290)
store.set('k.width', 'invalid')
expect(readStoredSplitWidth('k.width', 290, 220, 520)).toBe(290)
writeStoredSplitWidth('k.width', 412.6)
expect(store.get('k.width')).toBe('413')
expect(readStoredSplitWidth('k.width', 290, 220, 520)).toBe(413)
store.set('k.width', '999')
expect(readStoredSplitWidth('k.width', 290, 220, 520)).toBe(520)
} finally {
;(globalThis as { window?: unknown }).window = originalWindow
}
})
})
describe('DataTable elastic column & waterfall helpers', () => {
test('distributes surplus viewport width proportionally across elastic columns without utility bloat', () => {
const result = computeColumnWidths({
columns: [
{ id: 'name', baseWidth: 240, minWidth: 140, maxWidth: 720, resizable: true },
{ id: 'source_id', baseWidth: 130, minWidth: 96, maxWidth: 720, resizable: true },
{ id: 'collected_at', baseWidth: 170, minWidth: 150, maxWidth: 720, resizable: true },
{ id: 'actions', baseWidth: 80, minWidth: 80, maxWidth: 80, resizable: false },
],
viewportWidth: 1000,
selectionWidth: 40,
})
expect(result.tableWidth).toBe(1000)
expect(result.spacerWidth).toBe(0)
expect(result.widths.actions).toBe(80)
expect(result.widths.name).toBeGreaterThan(240)
expect(result.widths.source_id).toBeGreaterThan(130)
expect(result.widths.collected_at).toBeGreaterThan(170)
expect(
result.widths.name + result.widths.source_id + result.widths.collected_at + result.widths.actions + 40,
).toBe(1000)
})
test('preserves manual dragged widths cleanly and enables horizontal overflow beyond viewport', () => {
const result = computeColumnWidths({
columns: [
{ id: 'name', baseWidth: 240, minWidth: 140, maxWidth: 720, resizable: true },
{ id: 'source', baseWidth: 168, minWidth: 110, maxWidth: 720, resizable: true },
],
viewportWidth: 600,
manualWidths: { name: 520, source: 220 },
})
expect(result.widths).toEqual({ name: 520, source: 220 })
expect(result.spacerWidth).toBe(0)
expect(result.tableWidth).toBe(740)
})
test('adds spacer width before sticky-right actions when manual columns shrink narrower than viewport', () => {
const result = computeColumnWidths({
columns: [
{ id: 'name', baseWidth: 240, minWidth: 120, maxWidth: 720, resizable: true },
{ id: 'status', baseWidth: 120, minWidth: 80, maxWidth: 720, resizable: true },
{ id: 'actions', baseWidth: 96, minWidth: 96, maxWidth: 96, resizable: false },
],
viewportWidth: 900,
selectionWidth: 40,
manualWidths: { name: 160, status: 90, actions: 96 },
})
expect(result.widths).toEqual({ name: 160, status: 90, actions: 96 })
expect(result.spacerWidth).toBe(900 - (40 + 160 + 90 + 96))
expect(result.tableWidth).toBe(900)
})
test('triggers waterfall load-more only when near bottom and idle', () => {
expect(
shouldLoadMoreWaterfall({
scrollTop: 500,
clientHeight: 400,
scrollHeight: 1000,
thresholdPx: 160,
hasMore: true,
loading: false,
loadingMore: false,
}),
).toBe(true)
expect(
shouldLoadMoreWaterfall({
scrollTop: 200,
clientHeight: 400,
scrollHeight: 1000,
thresholdPx: 160,
hasMore: true,
loading: false,
loadingMore: false,
}),
).toBe(false)
expect(
shouldLoadMoreWaterfall({
scrollTop: 600,
clientHeight: 400,
scrollHeight: 1000,
thresholdPx: 160,
hasMore: true,
loading: false,
loadingMore: true,
}),
).toBe(false)
})
test('dedupes appended waterfall rows while preserving order', () => {
const merged = dedupeAppendRows(
[{ id: '1' }, { id: '2' }],
[{ id: '2' }, { id: '3' }, { id: '4' }],
(row) => row.id,
)
expect(merged).toEqual([{ id: '1' }, { id: '2' }, { id: '3' }, { id: '4' }])
})
test('persists valid stored column widths and clears storage on reset', () => {
const store = new Map<string, string>()
const originalWindow = globalThis.window
;(globalThis as { window?: unknown }).window = {
localStorage: {
getItem: (key: string) => store.get(key) ?? null,
setItem: (key: string, value: string) => {
store.set(key, value)
},
removeItem: (key: string) => {
store.delete(key)
},
},
}
try {
expect(readStoredColumnWidths('tbl.widths', ['name', 'status'])).toEqual({})
writeStoredColumnWidths('tbl.widths', { name: 312.4, status: 120 })
expect(readStoredColumnWidths('tbl.widths', ['name', 'status'])).toEqual({ name: 312, status: 120 })
store.set('tbl.widths', JSON.stringify({ name: 280, removed: 99, invalid: -5 }))
expect(readStoredColumnWidths('tbl.widths', ['name', 'status'])).toEqual({ name: 280 })
writeStoredColumnWidths('tbl.widths', {})
expect(store.has('tbl.widths')).toBe(false)
} finally {
;(globalThis as { window?: unknown }).window = originalWindow
}
})
test('finds the last data column index excluding trailing actions columns', () => {
expect(findLastDataColumnIndex([{ id: 'name' }, { id: 'metric' }, { id: 'actions' }])).toBe(1)
expect(findLastDataColumnIndex([{ id: 'name' }, { id: 'updated_at' }, { id: 'action' }])).toBe(1)
expect(findLastDataColumnIndex([{ id: 'name' }, { id: 'collected_at' }])).toBe(1)
expect(findLastDataColumnIndex([{ id: 'actions' }])).toBe(-1)
expect(findLastDataColumnIndex([])).toBe(-1)
})
test('skips viewport resize recalculation when both widths stay at or below minimum table width', () => {
const columns = [
{ id: 'name', baseWidth: 220, minWidth: 160, maxWidth: 720, resizable: true },
{ id: 'schedule', baseWidth: 72, minWidth: 72, maxWidth: 720, resizable: true },
{ id: 'actions', baseWidth: 112, minWidth: 112, maxWidth: 720, resizable: false },
]
const selectionWidth = 40 // minFullWidth = 444
expect(
doesViewportChangeAffectColumns({
columns,
selectionWidth,
prevViewportWidth: 420,
nextViewportWidth: 360,
}),
).toBe(false)
expect(
doesViewportChangeAffectColumns({
columns,
selectionWidth,
prevViewportWidth: 420,
nextViewportWidth: 520,
}),
).toBe(true)
expect(
doesViewportChangeAffectColumns({
columns,
selectionWidth,
prevViewportWidth: 520,
nextViewportWidth: 560,
}),
).toBe(true)
expect(
doesViewportChangeAffectColumns({
columns,
selectionWidth,
prevViewportWidth: 520,
nextViewportWidth: 520,
}),
).toBe(false)
})
test('estimates column minWidth strictly wider than header text and controls', () => {
const scheduleMin = estimateHeaderMinWidth('定时调度', { sortable: false, resizable: true })
expect(scheduleMin).toBeGreaterThanOrEqual(84)
const updatedMin = estimateHeaderMinWidth('更新时间', { sortable: true, resizable: true })
expect(updatedMin).toBeGreaterThan(scheduleMin)
const englishScheduleMin = estimateHeaderMinWidth('Scheduled', { sortable: false, resizable: true })
expect(englishScheduleMin).toBeGreaterThanOrEqual(96)
const shortMin = estimateHeaderMinWidth('状态', { sortable: true, resizable: true })
expect(shortMin).toBeGreaterThanOrEqual(72)
})
})