Skip to content

Commit ef620e0

Browse files
committed
test(editor): exercise RAF scroll-restore callback in focus wrapping test
The previous test only verified that requestAnimationFrame was scheduled but never invoked the callback. Now we capture the RAF callback, simulate async scroll drift (scrollY changing between focus call and RAF execution), and verify scrollTo is called to restore the original position.
1 parent 88c0773 commit ef620e0

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

packages/super-editor/src/core/presentation-editor/tests/PresentationEditor.focusWrapping.test.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,20 +356,33 @@ describe('PresentationEditor - Focus Wrapping (#wrapHiddenEditorFocus)', () => {
356356
}).not.toThrow();
357357
});
358358

359-
it('schedules requestAnimationFrame as async scroll safety net', () => {
359+
it('schedules requestAnimationFrame that restores scroll on async drift', () => {
360360
editor = new PresentationEditor({
361361
element: container,
362362
documentId: 'test-doc',
363363
pageSize: { w: 612, h: 792 },
364364
});
365365

366-
const rafSpy = vi.spyOn(window, 'requestAnimationFrame');
366+
// Capture the RAF callback so we can invoke it manually
367+
let rafCallback: FrameRequestCallback | null = null;
368+
const rafSpy = vi.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => {
369+
rafCallback = cb;
370+
return 1;
371+
});
372+
const scrollToSpy = vi.spyOn(window, 'scrollTo').mockImplementation(() => {});
367373

374+
// At focus time, scrollX=0 scrollY=0 → captured as beforeX=0 beforeY=0
368375
editor.editor.view.focus();
369376

370-
// RAF should be scheduled to catch async browser scroll after focus
371377
expect(rafSpy).toHaveBeenCalledTimes(1);
372-
expect(rafSpy).toHaveBeenCalledWith(expect.any(Function));
378+
expect(rafCallback).not.toBeNull();
379+
380+
// Simulate the browser async-scrolling to the hidden editor (drift from 0 to 500)
381+
Object.defineProperty(window, 'scrollY', { value: 500, configurable: true });
382+
383+
// Run the RAF callback — it should detect drift and restore to beforeY=0
384+
rafCallback!(0);
385+
expect(scrollToSpy).toHaveBeenCalledWith(0, 0);
373386
});
374387

375388
it('cancels focus-scroll RAF when scrollToPosition is called', () => {

0 commit comments

Comments
 (0)