Files
site-builder/craft/src/components/basic/Menu.toHtml.test.ts
T
shadowdaoandClaude Opus 4.8 8029126ab7 deterministic + unique ids in 6 components (node-derived scope)
Migrates Menu, ColumnLayout, Countdown, Gallery, Tabs, InputField, and
TextareaField from Math.random()/content-hash scope ids to the threaded
Craft node id (via scopeId()), keeping every emitted element id,
aria-controls/aria-labelledby/for, and inline <script> function
name/getElementById() call consistently scoped per component.

Resolves the two Important id-collision review findings:
- Tabs: tabId was djb2(anchorId||labels) -- two default Tabs instances
  produced identical aria-controls/aria-labelledby ids, so one instance's
  arrow-key script clobbered the other's tab/panel wiring.
- InputField/TextareaField: fieldId was `field-${name}` -- two fields
  sharing a (often default) name produced duplicate <label for>/<input
  id> pairs, breaking the for/id association for one of them.

Also eliminates the remaining Math.random() scope ids in Menu (hover CSS
class scope) and ColumnLayout (nth-child width CSS class scope), so no
export component is non-deterministic anymore.

All fall back to a stable content hash (never Math.random) for legacy
2-arg toHtml() call sites without a node id.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-12 14:35:12 -07:00

33 lines
1.3 KiB
TypeScript

import { describe, test, expect } from 'vitest';
import { Menu } from './Menu';
const toHtml = (Menu as any).toHtml;
describe('Menu.toHtml deterministic + unique scope ids (thread node id, no Math.random)', () => {
test('same node id -> identical output across calls (deterministic)', () => {
const { html: html1 } = toHtml({}, '', 'node-menu1');
const { html: html2 } = toHtml({}, '', 'node-menu1');
expect(html1).toBe(html2);
});
test('different node ids -> different, non-colliding scope classes (identical default links, no collision)', () => {
const { html: html1 } = toHtml({}, '', 'node-menu1');
const { html: html2 } = toHtml({}, '', 'node-menu2');
const cls1 = html1.match(/\.([a-z0-9_]+-link):hover/)![1];
const cls2 = html2.match(/\.([a-z0-9_]+-link):hover/)![1];
expect(cls1).not.toBe(cls2);
});
test('the anchor class= and the <style> hover rule use the SAME scope', () => {
const { html } = toHtml({}, '', 'node-menu1');
const hoverCls = html.match(/\.([a-z0-9_]+-link):hover/)![1];
expect(html).toContain(`class="${hoverCls}"`);
});
test('no nodeId (legacy 2-arg call): still deterministic across repeated calls, not random', () => {
const { html: html1 } = toHtml({}, '');
const { html: html2 } = toHtml({}, '');
expect(html1).toBe(html2);
});
});