Files
wechat_wc/tools/custom-tab-bar.test.mjs
T
broccoli 4646f87797 fix(ui): 拖动松手从手指位置平滑契合到目标 tab
- 拖动状态与位置状态分离
- 松手先恢复过渡但保留手指位置,下一帧再吸附到目标 tab,避免从旧位置跳变
2026-08-18 23:13:25 +08:00

44 lines
1.6 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import { clampPercent, getTabBarFrame, hasDragged, tabIndexAtPercent } from '../src/custom-tab-bar/tabBarGeometry.js'
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
test('maps a drag percent to the tab under the finger', () => {
assert.equal(tabIndexAtPercent(10, 5), 0)
assert.equal(tabIndexAtPercent(30, 5), 1)
assert.equal(tabIndexAtPercent(50, 5), 2)
assert.equal(tabIndexAtPercent(70, 5), 3)
assert.equal(tabIndexAtPercent(90, 5), 4)
})
test('clamps percent and maps out-of-range to edge tabs', () => {
assert.equal(clampPercent(-5), 0)
assert.equal(clampPercent(120), 100)
assert.equal(clampPercent(Number.NaN), 0)
assert.equal(tabIndexAtPercent(-5, 5), 0)
assert.equal(tabIndexAtPercent(120, 5), 4)
})
test('only treats a move as a drag beyond the threshold', () => {
assert.equal(hasDragged(100, 110, 12), false)
assert.equal(hasDragged(100, 112, 12), true)
assert.equal(hasDragged(100, 88, 12), true)
})
test('derives the floating bar frame from window width', () => {
assert.deepEqual(getTabBarFrame(375), { left: 18, width: 339 })
assert.deepEqual(getTabBarFrame(750), { left: 36, width: 678 })
})
test('enables the slide transition before snapping the indicator to the target', () => {
const source = readFileSync(path.join(root, 'src/custom-tab-bar/index.tsx'), 'utf8')
assert.match(
source,
/isDraggingRef\.current = false\s+setIsDragging\(false\)\s+Taro\.nextTick\(\(\) => \{[\s\S]*?setDragPercent\(null\)/
)
})