Files
MyIP/tests/scroll-to.test.js
jason5ng32andClaude Opus 4.8 e8e2b556cf Refactor(frontend): move scroll-to from composables to utils
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>
2026-05-30 11:03:32 +08:00

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;
});