fix: unit tests

This commit is contained in:
Yourz
2026-01-06 00:03:39 +08:00
parent cc3509430b
commit 2810184d12

View File

@@ -5,11 +5,8 @@ import type { RenderedTreeExplorerNode } from '@/types/treeExplorerTypes'
import type { WindowRange } from '../virtualListUtils'
import {
applyWindow,
calculateScrollPercentage,
calculateSpacerHeights,
createInitialWindowRange,
shiftWindowBackward,
shiftWindowForward
createInitialWindowRange
} from '../virtualListUtils'
describe('virtualListUtils', () => {
@@ -30,58 +27,6 @@ describe('virtualListUtils', () => {
})
})
describe('shiftWindowForward', () => {
it('shifts window forward by buffer size', () => {
const currentRange: WindowRange = { start: 0, end: 60 }
const result = shiftWindowForward(currentRange, 100, 20, 60)
expect(result).toEqual({ start: 20, end: 80 })
})
it('returns null when already at end', () => {
const currentRange: WindowRange = { start: 40, end: 100 }
const result = shiftWindowForward(currentRange, 100, 20, 60)
expect(result).toBeNull()
})
it('caps end at totalChildren', () => {
const currentRange: WindowRange = { start: 30, end: 90 }
const result = shiftWindowForward(currentRange, 100, 20, 60)
expect(result).toEqual({ start: 40, end: 100 })
})
it('adjusts start to maintain window size when near end', () => {
const currentRange: WindowRange = { start: 20, end: 80 }
const result = shiftWindowForward(currentRange, 95, 20, 60)
expect(result).toEqual({ start: 35, end: 95 })
})
})
describe('shiftWindowBackward', () => {
it('shifts window backward by buffer size', () => {
const currentRange: WindowRange = { start: 40, end: 100 }
const result = shiftWindowBackward(currentRange, 100, 20, 60)
expect(result).toEqual({ start: 20, end: 80 })
})
it('returns null when already at start', () => {
const currentRange: WindowRange = { start: 0, end: 60 }
const result = shiftWindowBackward(currentRange, 100, 20, 60)
expect(result).toBeNull()
})
it('caps start at 0', () => {
const currentRange: WindowRange = { start: 10, end: 70 }
const result = shiftWindowBackward(currentRange, 100, 20, 60)
expect(result).toEqual({ start: 0, end: 60 })
})
it('caps end at totalChildren when window would exceed', () => {
const currentRange: WindowRange = { start: 20, end: 50 }
const result = shiftWindowBackward(currentRange, 50, 20, 60)
expect(result).toEqual({ start: 0, end: 50 })
})
})
describe('calculateSpacerHeights', () => {
it('calculates correct spacer heights', () => {
const range: WindowRange = { start: 20, end: 80 }
@@ -119,35 +64,6 @@ describe('virtualListUtils', () => {
})
})
})
describe('calculateScrollPercentage', () => {
it('calculates percentage correctly', () => {
const result = calculateScrollPercentage(500, 2000, 400, 0, 0)
expect(result).toBeCloseTo(0.45)
})
it('adjusts for top spacer height', () => {
const result = calculateScrollPercentage(600, 2000, 400, 200, 200)
// realContentHeight = 2000 - 200 - 200 = 1600
// adjustedScrollTop = max(0, 600 - 200) = 400
// percentage = (400 + 400) / 1600 = 0.5
expect(result).toBeCloseTo(0.5)
})
it('returns 1 when realContentHeight is 0', () => {
const result = calculateScrollPercentage(0, 100, 50, 50, 50)
expect(result).toBe(1)
})
it('handles scrollTop less than topSpacerHeight', () => {
const result = calculateScrollPercentage(100, 2000, 400, 200, 200)
// adjustedScrollTop = max(0, 100 - 200) = 0
// realContentHeight = 1600
// percentage = (0 + 400) / 1600 = 0.25
expect(result).toBeCloseTo(0.25)
})
})
describe('applyWindow', () => {
const createMockNode = (
key: string,