import { describe, expect, it } from 'vitest' import { scrollRatio, scrollTopForRatio } from '../src/scrollsync' describe('scrollsync', () => { it('没有溢出时不同步', () => { expect(scrollRatio({ scrollTop: 0, scrollHeight: 100, clientHeight: 100 })).toBeNull() expect(scrollTopForRatio({ scrollHeight: 80, clientHeight: 80 }, 0.5)).toBeNull() }) it('按可滚动范围的比例映射,并夹在 0 到 1', () => { expect(scrollRatio({ scrollTop: 50, scrollHeight: 200, clientHeight: 100 })).toBe(0.5) expect(scrollTopForRatio({ scrollHeight: 300, clientHeight: 100 }, 0.5)).toBe(100) expect(scrollTopForRatio({ scrollHeight: 300, clientHeight: 100 }, 2)).toBe(200) expect(scrollTopForRatio({ scrollHeight: 300, clientHeight: 100 }, -1)).toBe(0) }) })