Files
NODEDC_DESIGN_GUIDELINE/scripts/range-control-contract.test.mjs

132 lines
7.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { RangeControl } from "../packages/ui-react/dist/index.js";
test("RangeControl keeps native drag semantics and exposes one persistent exact-value editor", () => {
const markup = renderToStaticMarkup(createElement(RangeControl, {
label: "Размер головки",
value: 20,
min: 1,
max: 32,
step: 1,
formatValue: (value) => `${value} px`,
onChange: () => {},
}));
assert.match(markup, /type="range"/);
assert.match(markup, /aria-valuetext="20 px"/);
assert.match(markup, /class="nodedc-range__editor"/);
assert.match(markup, /aria-label="Размер головки: точное значение"/);
assert.doesNotMatch(markup, /nodedc-range__value-hit/);
});
test("vertical scale exposes retained bounds without center text and follows every value", () => {
for (const [value, progress] of [[-1.2, 0], [25.15, 50], [51.5, 100]]) {
for (const limitSide of ["left", "right"]) {
const markup = renderToStaticMarkup(createElement(RangeControl, {
label: "Срез", orientation: "vertical", limitSide, value, min: -1.2, max: 51.5, step: "any",
formatValue: n => `${n.toFixed(1)} м`, formatLimit: n => n.toFixed(1), onChange: () => {},
}));
const renderedProgress = Number(markup.match(/--nodedc-range-progress:([\d.]+)%/)?.[1]);
assert.ok(Math.abs(renderedProgress - progress) < 1e-9);
assert.ok(markup.includes(`data-limit-side="${limitSide}"`));
assert.ok(markup.includes(`nodedc-range-scale__limit--max">${value.toFixed(1)}</span>`));
assert.match(markup, /nodedc-range-scale__limit--min">-1.2<\/span>/);
assert.match(markup, /aria-label="Срез" aria-orientation="vertical"/);
assert.ok(markup.includes(`aria-valuetext="${value.toFixed(1)} м"`));
assert.match(markup, /min="-1.2" max="51.5" step="any"/);
assert.doesNotMatch(markup, /nodedc-range__label|nodedc-range__fill-text|nodedc-range__editor|formatLimit=/);
}
}
});
test("thin vertical rail retains a usable pointer target and boundary focus", async () => {
const styles = await readFile(new URL("../packages/ui-core/styles.css", import.meta.url), "utf8");
assert.match(styles, /width: calc\(var\(--nodedc-control-height\) \* 0\.2\)/);
assert.match(styles, /width: max\(100%, 24px\)/);
assert.match(styles, /limit--max \{ bottom: clamp\(/);
assert.match(styles, /range-scale:has\(input:focus-visible\) \.nodedc-range-scale__limit--max \{ text-decoration: underline/);
assert.match(styles, /data-orientation="vertical"\] \{[^}]*border: 0; outline: 0; box-shadow: none;/);
});
test("exact editing is a stable native caret field without remount, forced selection or a dark editor box", async () => {
const [component, styles, windowComponent] = await Promise.all([
readFile(new URL("../packages/ui-react/src/RangeControl.tsx", import.meta.url), "utf8"),
readFile(new URL("../packages/ui-core/styles.css", import.meta.url), "utf8"),
readFile(new URL("../packages/ui-react/src/Window.tsx", import.meta.url), "utf8"),
]);
assert.match(component, /exactValueBounds\?: \{/);
assert.match(component, /exactValueBounds\?\.min \?\? Number\.NEGATIVE_INFINITY/);
assert.match(component, /exactValueBounds\?\.max \?\? Number\.POSITIVE_INFINITY/);
assert.match(component, /normalizeEditedRangeValue\(parsed, min, max, step, exactValueBounds\)/);
assert.match(component, /value=\{draft\}/);
assert.match(component, /data-contrast=\{editorContrast\}/);
assert.match(component, /fillRight >= textRight \? "fill" : "base"/);
assert.match(component, /new ResizeObserver\(updateContrast\)/);
const editorSource = component.match(/className="nodedc-range__editor"[\s\S]*?onBlur=\{\(\) => \{[\s\S]*?\n \}\}\n \/>/)?.[0] ?? "";
assert.notEqual(editorSource, "");
assert.doesNotMatch(editorSource, /onPointerDown=/);
assert.match(component, /event\.key === "Enter"/);
assert.match(component, /event\.key === "Escape"/);
assert.match(component, /event\.stopPropagation\(\)/);
assert.match(component, /cancelNextBlurRef/);
assert.match(component, /event\.currentTarget\.blur\(\)/);
assert.match(component, /onBlur=\{\(\) => \{/);
assert.doesNotMatch(component, /\.select\(\)/);
assert.doesNotMatch(component, /setSelectionRange/);
assert.doesNotMatch(component, /\{editing \? \(\s*<input/);
assert.match(styles, /\.nodedc-range input\[type="range"\][\s\S]*?z-index: 4/);
assert.match(styles, /\.nodedc-range__editor[\s\S]*?z-index: 5[\s\S]*?background: transparent[\s\S]*?color: transparent[\s\S]*?caret-color: transparent[\s\S]*?box-shadow: none/);
assert.match(styles, /\.nodedc-range__editor\[data-active\][\s\S]*?caret-color: currentColor/);
assert.match(styles, /\.nodedc-range__editor\[data-active\]\[data-contrast="fill"\][\s\S]*?rgb\(var\(--nodedc-on-accent-rgb\)\)/);
assert.match(windowComponent, /const onCloseRef = useRef\(onClose\)/);
assert.match(windowComponent, /onCloseRef\.current = onClose/);
assert.match(windowComponent, /onCloseRef\.current\(\)/);
assert.doesNotMatch(windowComponent, /\[closeOnEscape, onClose, open, shouldLockBodyScroll, shouldTrapFocus\]/);
});
test("vertical band exposes two independent accessible boundaries", () => {
for (const value of [[-52, 500], [10, 35], [20, 20]]) {
const markup = renderToStaticMarkup(createElement(RangeControl, {
label: "Срез", orientation: "vertical", value, min: -52, max: 500, step: "any",
formatValue: n => `${n} м`, onChange: () => {},
}));
assert.equal((markup.match(/type="range"/g) ?? []).length, 2);
assert.match(markup, /aria-label="Срез: нижняя граница"/);
assert.match(markup, /aria-label="Срез: верхняя граница"/);
assert.ok(markup.includes(`aria-valuemax="${value[1]}"`));
assert.ok(markup.includes(`aria-valuemin="${value[0]}"`));
assert.doesNotMatch(markup, /nodedc-range__label|nodedc-range__editor/);
}
});
test("both boundaries move, clamp independently, separate after coincidence and restore endpoints", async () => {
const {moveRangeBoundary: move, nearestRangeBoundary: nearest} = await import("../packages/ui-react/dist/rangeBand.js");
let band = [-52, 500];
assert.equal(nearest(band, -40), 0); assert.equal(nearest(band, 450), 1);
band = move(band, 0, 10, -52, 500, "any"); assert.deepEqual(band, [10, 500]);
band = move(band, 1, 30, -52, 500, "any"); assert.deepEqual(band, [10, 30]);
band = move(band, 0, 50, -52, 500, "any"); assert.deepEqual(band, [30, 30]);
assert.equal(nearest(band, 20), 0); assert.equal(nearest(band, 40), 1);
band = move(band, 0, -100, -52, 500, "any");
band = move(band, 1, 600, -52, 500, "any"); assert.deepEqual(band, [-52, 500]);
assert.deepEqual(move([0, 10], 1, 7.3, 0, 10, .5), [0, 7.5]);
assert.deepEqual(move([0, 9], 1, 10.1, 0, 10.1, 3), [0, 10.1]);
});
test("horizontal thin rail has one native boundary and a formatted value, without field chrome", () => {
for(const disabled of [false,true]){
const markup=renderToStaticMarkup(createElement(RangeControl,{variant:"rail",label:"Скорость",value:5,min:.1,max:100,step:.1,disabled,formatValue:n=>`${n} м/с`,onChange(){}}));
assert.equal((markup.match(/type="range"/g)||[]).length,1);
assert.match(markup,/aria-label="Скорость" aria-orientation="horizontal"/);
assert.match(markup,/aria-valuetext="5 м\/с"/);
assert.match(markup,/nodedc-range-rail__value/);
assert.equal(markup.includes('disabled=""'),disabled);
assert.doesNotMatch(markup,/nodedc-range__label|nodedc-range__editor|nodedc-glass/);
}
});