mirror of
https://github.com/jason5ng32/MyIP.git
synced 2026-11-04 08:55:47 +00:00
use-scroll-to exported a pure DOM helper (scrollToElement), not a composable. Relocated to utils/scroll-to.js and renamed its test accordingly. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { describe, it, after } from 'node:test';
|
|
|
|
import { scrollToElement } from '../frontend/utils/scroll-to.js';
|
|
|
|
// inject minimal DOM stub, module itself does not access globalThis, reads only when function is called
|
|
let scrollTarget = null;
|
|
globalThis.window = {
|
|
scrollY: 100,
|
|
scrollTo(opts) { scrollTarget = opts; },
|
|
};
|
|
globalThis.document = {
|
|
_elements: new Map(),
|
|
getElementById(id) { return this._elements.get(id) || null; },
|
|
};
|
|
|
|
function registerElement(id, top) {
|
|
globalThis.document._elements.set(id, {
|
|
getBoundingClientRect() { return { top }; },
|
|
});
|
|
}
|
|
|
|
describe('scrollToElement()', () => {
|
|
it('scrolls to element.top + window.scrollY - offset', () => {
|
|
registerElement('hero', 250);
|
|
scrollToElement('hero', 80);
|
|
assert.deepEqual(scrollTarget, { top: 270, behavior: 'smooth' });
|
|
});
|
|
|
|
it('accepts an element object directly (not just an id)', () => {
|
|
const el = { getBoundingClientRect() { return { top: 500 }; } };
|
|
scrollToElement(el, 0);
|
|
assert.deepEqual(scrollTarget, { top: 600, behavior: 'smooth' });
|
|
});
|
|
|
|
it('no-ops silently when id is unknown (no throw)', () => {
|
|
scrollTarget = 'unchanged';
|
|
scrollToElement('does-not-exist', 10);
|
|
assert.equal(scrollTarget, 'unchanged');
|
|
});
|
|
});
|
|
|
|
after(() => {
|
|
delete globalThis.window;
|
|
delete globalThis.document;
|
|
});
|