Something went wrong. Try again.
An experimental Wallabag client
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';import { computeScrollPercent, scrollTargetFromPercent, debounce } from './scroll-position';
describe('computeScrollPercent', () => { it('returns the linear fraction between 0 and 1', () => { expect(computeScrollPercent(0, 2000, 1000)).toBe(0); expect(computeScrollPercent(500, 2000, 1000)).toBe(0.5); expect(computeScrollPercent(1000, 2000, 1000)).toBe(1); });
it('clamps overscroll past the bottom to 1', () => { expect(computeScrollPercent(5000, 2000, 1000)).toBe(1); });
it('clamps negative scroll to 0', () => { expect(computeScrollPercent(-100, 2000, 1000)).toBe(0); });
it('returns 0 when the document fits in the viewport', () => { expect(computeScrollPercent(0, 500, 1000)).toBe(0); expect(computeScrollPercent(0, 1000, 1000)).toBe(0); });
it('handles NaN inputs without throwing', () => { expect(computeScrollPercent(NaN, 1000, 500)).toBe(0); });});
describe('scrollTargetFromPercent', () => { it('maps fraction back to pixel offset', () => { expect(scrollTargetFromPercent(0, 2000, 1000)).toBe(0); expect(scrollTargetFromPercent(0.5, 2000, 1000)).toBe(500); expect(scrollTargetFromPercent(1, 2000, 1000)).toBe(1000); });
it('clamps out-of-range fractions', () => { expect(scrollTargetFromPercent(-0.1, 2000, 1000)).toBe(0); expect(scrollTargetFromPercent(1.5, 2000, 1000)).toBe(1000); });
it('returns 0 when there is nothing to scroll', () => { expect(scrollTargetFromPercent(0.5, 500, 1000)).toBe(0); });
it('round-trips through computeScrollPercent within rounding error', () => { const target = scrollTargetFromPercent(0.37, 4321, 800); const back = computeScrollPercent(target, 4321, 800); expect(Math.abs(back - 0.37)).toBeLessThan(0.001); });});
describe('debounce', () => { beforeEach(() => { vi.useFakeTimers(); }); afterEach(() => { vi.useRealTimers(); });
it('fires once after the delay with the most recent args', () => { const fn = vi.fn<(v: number) => void>(); const d = debounce(fn, 100); d.call(1); d.call(2); d.call(3); expect(fn).not.toHaveBeenCalled(); vi.advanceTimersByTime(100); expect(fn).toHaveBeenCalledTimes(1); expect(fn).toHaveBeenCalledWith(3); });
it('resets the timer on each call', () => { const fn = vi.fn(); const d = debounce(fn, 100); d.call('a'); vi.advanceTimersByTime(60); d.call('b'); vi.advanceTimersByTime(60); expect(fn).not.toHaveBeenCalled(); vi.advanceTimersByTime(40); expect(fn).toHaveBeenCalledExactlyOnceWith('b'); });
it('flush() fires immediately with the pending args', () => { const fn = vi.fn(); const d = debounce(fn, 100); d.call('pending'); d.flush(); expect(fn).toHaveBeenCalledExactlyOnceWith('pending'); // Pending state must be cleared so the timer can't double-fire. vi.advanceTimersByTime(500); expect(fn).toHaveBeenCalledTimes(1); });
it('flush() is a no-op when nothing is pending', () => { const fn = vi.fn(); const d = debounce(fn, 100); d.flush(); expect(fn).not.toHaveBeenCalled(); });
it('cancel() drops pending calls', () => { const fn = vi.fn(); const d = debounce(fn, 100); d.call('drop me'); d.cancel(); vi.advanceTimersByTime(500); expect(fn).not.toHaveBeenCalled(); });});