feat(renderer): useScrollSync hook (60fps throttling, ratio calc)

This commit is contained in:
2026-06-05 13:13:37 +05:30
parent 8624257c1f
commit 2d6f909b0e
2 changed files with 54 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { renderHook, act } from '@testing-library/react';
import { useScrollSync } from '@/hooks/use-scroll-sync';
describe('useScrollSync', () => {
beforeEach(() => {
vi.useFakeTimers();
// Mock performance.now() to return 0 and not auto-advance
let time = 0;
vi.spyOn(performance, 'now').mockImplementation(() => time);
});
it('throttles editor scroll events to 60fps', () => {
const onScroll = vi.fn();
const { result } = renderHook(() => useScrollSync({ onEditorScroll: onScroll }));
const mockEvt = { currentTarget: { scrollTop: 100, scrollHeight: 1000, clientHeight: 200 } } as any;
// All calls within the same act() - performance.now() stays at 0
// First call passes, subsequent calls within FRAME_MS are throttled
act(() => {
result.current.handleEditorScroll(mockEvt);
result.current.handleEditorScroll(mockEvt);
result.current.handleEditorScroll(mockEvt);
});
expect(onScroll).toHaveBeenCalledTimes(1);
});
});