44 lines
1.6 KiB
JavaScript
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\)/
|
|
)
|
|
})
|