49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
|
|
import { describe, expect, it } from 'vitest';
|
||
|
|
import { nextIndex, swipeStep } from './carousel';
|
||
|
|
|
||
|
|
// 广告轮播纯逻辑(Task 10 Step 1):推进/环形回绕/单条静止 + 滑动手势判定
|
||
|
|
//(阈值 40px、水平分量须严格大于垂直分量)。
|
||
|
|
|
||
|
|
describe('nextIndex', () => {
|
||
|
|
it('advances forward and wraps from the tail to the head (ring)', () => {
|
||
|
|
expect(nextIndex(3, 0)).toBe(1);
|
||
|
|
expect(nextIndex(3, 1)).toBe(2);
|
||
|
|
expect(nextIndex(3, 2)).toBe(0); // 末→首
|
||
|
|
});
|
||
|
|
|
||
|
|
it('keeps a single item pinned at 0 (never switches)', () => {
|
||
|
|
expect(nextIndex(1, 0)).toBe(0);
|
||
|
|
expect(nextIndex(1, 0, -1)).toBe(0);
|
||
|
|
expect(nextIndex(0, 0)).toBe(0); // 空表防御:同 0
|
||
|
|
});
|
||
|
|
|
||
|
|
it('steps backwards wrapping from the head to the tail', () => {
|
||
|
|
expect(nextIndex(3, 0, -1)).toBe(2);
|
||
|
|
expect(nextIndex(3, 2, -1)).toBe(1);
|
||
|
|
expect(nextIndex(5, 4, -1)).toBe(3);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('swipeStep', () => {
|
||
|
|
it('maps an unambiguous horizontal swipe to its direction', () => {
|
||
|
|
expect(swipeStep(-80, 5)).toBe(1); // 左滑 → 下一张
|
||
|
|
expect(swipeStep(80, -5)).toBe(-1); // 右滑 → 上一张
|
||
|
|
});
|
||
|
|
|
||
|
|
it('ignores movement under the 40px threshold', () => {
|
||
|
|
expect(swipeStep(-39, 0)).toBe(0);
|
||
|
|
expect(swipeStep(39, 2)).toBe(0);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('accepts exactly 40px (>= threshold)', () => {
|
||
|
|
expect(swipeStep(-40, 0)).toBe(1);
|
||
|
|
expect(swipeStep(40, 0)).toBe(-1);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('ignores diagonal or vertically dominant movement', () => {
|
||
|
|
expect(swipeStep(60, 70)).toBe(0); // 垂直分量更大(斜向)
|
||
|
|
expect(swipeStep(50, 50)).toBe(0); // 等分量:水平须严格大于垂直
|
||
|
|
expect(swipeStep(-100, 100)).toBe(0);
|
||
|
|
});
|
||
|
|
});
|