feat(ui): standardize collapsible application navigation

This commit is contained in:
Codex
2026-11-15 22:50:37 +03:00
parent d0648014f5
commit ae82c01049
8 changed files with 243 additions and 46 deletions
+48
View File
@@ -0,0 +1,48 @@
const fs = require('node:fs'), path = require('node:path'), os = require('node:os'), assert = require('node:assert/strict');
const root = process.env.DG_ROOT || path.resolve(__dirname, '..');
const {JSDOM} = require('jsdom'), React = require(root + '/node_modules/react'), {act} = React;
const {createRoot} = require(root + '/node_modules/react-dom/client');
const dom = new JSDOM('<div id="app"></div>', {pretendToBeVisual: true});
for (const name of ['window','document','HTMLElement','Element','Node','Event','MouseEvent','KeyboardEvent','CustomEvent']) global[name] = dom.window[name];
global.ResizeObserver = class {observe() {} disconnect() {}};
global.IS_REACT_ACT_ENVIRONMENT = true;
HTMLElement.prototype.getBoundingClientRect = () => ({top: 20, bottom: 60, left: 20, right: 80, width: 60, height: 40});
const scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'nodedc-sidebar-'));
require(root + '/node_modules/esbuild').buildSync({stdin: {contents: `export {AdminNavigationPanel} from './AdminNavigationPanel.tsx'; export {ApplicationShell} from './ApplicationShell.tsx';`, resolveDir: root + '/packages/ui-react/src'}, outfile: scratch + '/sidebar.cjs', bundle: true, platform: 'node', format: 'cjs', external: ['react','react/*','react-dom','react-dom/*'], jsx: 'automatic'});
const {ApplicationShell, AdminNavigationPanel} = require(scratch + '/sidebar.cjs');
const ui = createRoot(document.getElementById('app')), h = React.createElement;
let selected = 'one', section = 'Data', open = true, mounts = 0, closes = 0, notified = [];
function Workspace() {React.useEffect(() => {mounts++;}, []); return h('input', {defaultValue: 'keep draft', 'aria-label': 'Draft'});}
const panel = () => h(AdminNavigationPanel, {key: section, title: section, onClose: () => closes++, closeLabel: 'Close navigation', activeId: selected, onItemChange: id => selected = id, contexts: [{id: 'context', label: 'Context', onToggle: () => {}, icon: 'C'}], headerActions: h('button', null, 'Extra'), items: [{id: 'one', label: 'One', icon: '1'}, {id: 'two', label: 'Two', icon: '2', sortable: true}], onItemsReorder: () => {}});
const render = (extra = {}) => act(async () => ui.render(h(ApplicationShell, {header: null, stage: null, navigation: panel(), navigationOpen: open, contentOpen: true, contentExpanded: true, content: h(Workspace), endPanel: h('div', {'data-testid': 'right'}, 'Right'), onNavigationCollapsedChange: value => notified.push(value), ...extra})));
const button = label => [...document.querySelectorAll('button')].find(b => b.getAttribute('aria-label') === label);
const click = el => act(async () => el.dispatchEvent(new MouseEvent('click', {bubbles: true})));
const shell = () => document.querySelector('.nodedc-app-shell');
(async () => {
await render(); const draft = document.querySelector('input'); draft.value = 'unsaved text';
await click(button('Свернуть панель навигации'));
assert.equal(shell().dataset.navigationCollapsed, 'true');
assert.equal(document.querySelector('aside').dataset.collapsed, 'true');
assert.equal(button('Развернуть панель навигации').getAttribute('aria-expanded'), 'false');
assert(button('Close navigation').hidden); assert.equal(closes, 0);
assert(button('Выбрать: Context').hidden); assert(document.querySelector('.nodedc-admin-panel__extra-actions').hidden);
assert.equal(button('One').getAttribute('aria-current'), 'page');
await click(button('Two')); assert.equal(selected, 'two');
section = 'System'; await render(); assert.equal(document.querySelector('aside').dataset.collapsed, 'true', 'new root uses retained shell state');
open = false; await render(); open = true; await render(); assert.equal(document.querySelector('aside').dataset.collapsed, 'true', 'close/reopen preserves rail');
await act(async () => button('Two').focus()); assert.equal(document.querySelector('[role=tooltip]').textContent, 'Two');
await act(async () => document.dispatchEvent(new KeyboardEvent('keydown', {key: 'Escape', bubbles: true})));
assert.equal(document.querySelector('[role=tooltip]'), null); assert.equal(closes, 0, 'Escape closes tooltip only');
await click(button('Развернуть панель навигации')); assert.equal(shell().dataset.navigationCollapsed, undefined);
assert.equal(button('Close navigation').hidden, false);
assert.equal(mounts, 1); assert.equal(document.querySelector('input'), draft); assert.equal(draft.value, 'unsaved text');
assert.deepEqual(notified, [true, false]);
await render({navigationCollapsed: false}); await click(button('Свернуть панель навигации'));
assert.equal(shell().dataset.navigationCollapsed, undefined, 'controlled shell awaits owner acceptance');
await render({navigationCollapsed: true}); assert.equal(document.querySelector('aside').dataset.collapsed, 'true');
assert.equal(document.querySelector('[data-testid=right]').textContent, 'Right');
await act(async () => ui.render(h(AdminNavigationPanel, {title: 'Standalone', items: [], onClose: () => {}, onItemChange: () => {}, defaultCollapsed: true})));
assert(button('Развернуть панель навигации')); await click(button('Развернуть панель навигации')); assert(button('Свернуть панель навигации'));
await act(async () => ui.unmount());
console.log('PASS: shared/standalone/controlled collapse, root and reopen persistence, hidden actions, icon navigation, accessible names and Escape tooltip, workspace remains mounted with its draft.');
})().catch(e => {console.error(e); process.exitCode = 1;}).finally(() => {fs.rmSync(scratch, {recursive: true, force: true}); dom.window.close();});