Files
NODEDC_DESIGN_GUIDELINE/scripts/tooltip-titles.test.mjs

29 lines
2.7 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {TooltipTitles} from '../packages/ui-react/dist/tooltipTitles.js';
class Node {
constructor(attributes={},parentElement=null){this.attributes=new Map(Object.entries(attributes));this.parentElement=parentElement;this.textContent='';}
getAttribute(name){return this.attributes.get(name)??null;}
hasAttribute(name){return this.attributes.has(name);}
setAttribute(name,value){this.attributes.set(name,value);}
removeAttribute(name){this.attributes.delete(name);}
}
test('native titles are suppressed through ancestor chain and restored without a duplicate bubble',()=>{
const parent=new Node({title:'Parent'}),button=new Node({title:'Inspector','aria-label':'Inspector','aria-describedby':'help'},parent),svg=new Node({},button),titles=new TooltipTitles();
assert.deepEqual(titles.find(svg),{element:button,text:'Inspector'});titles.suppress(button);titles.describe(button,'tip');
assert.equal(button.getAttribute('title'),null);assert.equal(parent.getAttribute('title'),null);assert.equal(button.getAttribute('aria-describedby'),'help tip');
titles.restore();assert.equal(button.getAttribute('title'),'Inspector');assert.equal(parent.getAttribute('title'),'Parent');assert.equal(button.getAttribute('aria-label'),'Inspector');assert.equal(button.getAttribute('aria-describedby'),'help');
});
test('new titles and external ARIA changes survive cleanup; empty title blocks inherited hints',()=>{
const parent=new Node({title:'Parent'}),button=new Node({title:''},parent),titles=new TooltipTitles();assert.equal(titles.find(button),null);
button.setAttribute('title','Before');titles.suppress(button);titles.describe(button,'tip');assert.equal(button.getAttribute('aria-label'),'Before');
button.setAttribute('title','After');titles.suppress(button);button.setAttribute('aria-label','External');button.setAttribute('aria-describedby','tip extra');
titles.restore();assert.equal(button.getAttribute('title'),'After');assert.equal(button.getAttribute('aria-label'),'External');assert.equal(button.getAttribute('aria-describedby'),'extra');
});
test('SVG controls use the same tooltip channel without a native title element',()=>{
const control=new Node({'data-nodedc-tooltip':'Повернуть элемент'}),shape=new Node({},control),titles=new TooltipTitles();
assert.deepEqual(titles.find(shape),{element:control,text:'Повернуть элемент'});
titles.suppress(control);titles.describe(control,'tip');assert.equal(control.getAttribute('aria-label'),'Повернуть элемент');
titles.restore();assert.equal(control.getAttribute('data-nodedc-tooltip'),'Повернуть элемент');assert.equal(control.getAttribute('aria-label'),null);
});