Files
site-builder/craft/src/editor/selection-commands.test.tsx
T
shadowdaoandClaude Opus 5.5 472e5e4d81 fix(selection): Esc clears a multi-selection; context menu acts on all selected
Esc was only claimed by select.parent for a single node, so a multi-selection
could not be dropped from the keyboard. Add select.clear (Esc, >1 selected)
through the existing dispatcher, which already skips text fields and open
dialogs. The context menu's Duplicate/Delete now run the multi-selection core
commands (edit.duplicate / edit.delete) when the clicked node is part of a
multi-selection, instead of acting on the first node only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 13:41:22 -07:00

136 lines
5.2 KiB
TypeScript

import { describe, test, expect, afterEach, beforeEach } from 'vitest';
import { renderEditorHarness, EditorHarness } from '../test-utils/editorHarness';
import { SELECTION_STATE } from './selection-fixture';
import {
selectParent, escapeSelection, selectNextSibling, selectPrevSibling, selectFirstChild, selectionCommands,
} from './selection-commands';
import React from 'react';
import { __resetCommandsForTests, getCommand, registerCommands, useCommandShortcuts } from './commands';
let h: EditorHarness;
beforeEach(() => { h = renderEditorHarness({ initialState: SELECTION_STATE }); });
afterEach(() => { h.unmount(); });
const sel = () => h.query.getEvent('selected').all();
const run = (fn: () => unknown) => h.act(() => { fn(); });
describe('selectParent', () => {
test('climbs parent by parent, hopping the Section inner container, never ROOT', () => {
const path: string[] = [];
let cur = 'b1';
for (let i = 0; i < 6; i++) {
let next: string | null = null;
run(() => { next = selectParent(h.query, h.actions, cur); });
if (!next) break;
path.push(next);
cur = next;
}
expect(path).toEqual(['col-0', 'cols', 'sec']);
expect(sel()).toEqual(['sec']);
});
test('no-op on a top-level node and on ROOT', () => {
run(() => h.actions.selectNode('sec'));
run(() => { expect(selectParent(h.query, h.actions, 'sec')).toBeNull(); });
expect(sel()).toEqual(['sec']);
run(() => { expect(selectParent(h.query, h.actions, 'ROOT')).toBeNull(); });
expect(sel()).toEqual(['sec']);
});
test('escapeSelection clears at the top level', () => {
run(() => h.actions.selectNode('sec'));
run(() => escapeSelection(h.query, h.actions, 'sec'));
expect(sel()).toEqual([]);
});
});
describe('siblings', () => {
test('next wraps, prev reverses and wraps', () => {
run(() => h.actions.selectNode('b1'));
run(() => selectNextSibling(h.query, h.actions, 'b1'));
expect(sel()).toEqual(['b2']);
run(() => selectNextSibling(h.query, h.actions, 'b2'));
expect(sel()).toEqual(['b1']);
run(() => selectPrevSibling(h.query, h.actions, 'b1'));
expect(sel()).toEqual(['b2']);
});
test('linked column containers are siblings of each other', () => {
run(() => h.actions.selectNode('col-0'));
run(() => selectNextSibling(h.query, h.actions, 'col-0'));
expect(sel()).toEqual(['col-1']);
});
test('a lone child has nowhere to go', () => {
run(() => { expect(selectNextSibling(h.query, h.actions, 'cols')).toBeNull(); });
});
});
test('selectFirstChild goes to the first child, null for a leaf', () => {
run(() => { expect(selectFirstChild(h.query, h.actions, 'col-0')).toBe('b1'); });
expect(sel()).toEqual(['b1']);
run(() => { expect(selectFirstChild(h.query, h.actions, 'b1')).toBeNull(); });
});
describe('command definitions', () => {
test('ids, shortcuts and when()', () => {
const byId = Object.fromEntries(selectionCommands.map((c) => [c.id, c]));
expect(byId['select.parent'].shortcut).toBe('escape');
expect(byId['select.next-sibling'].shortcut).toBe('tab');
expect(byId['select.prev-sibling'].shortcut).toBe('shift+tab');
expect(byId['select.first-child'].shortcut).toBeUndefined();
const ctx = (ids: string[]) => ({ selectedIds: ids, query: h.query }) as any;
for (const c of selectionCommands.filter((x) => x.id !== 'select.clear')) {
expect(c.group).toBe('select');
expect(c.when!(ctx(['b1']))).toBe(true);
expect(c.when!(ctx([]))).toBe(false);
expect(c.when!(ctx(['ROOT']))).toBe(false);
expect(c.when!(ctx(['a', 'b']))).toBe(false);
}
// Sibling cycling only claims Tab when it can move: a lone node ('cols') is left to the browser.
expect(byId['select.next-sibling'].when!(ctx(['cols']))).toBe(false);
expect(byId['select.prev-sibling'].when!(ctx(['cols']))).toBe(false);
expect(byId['select.parent'].when!(ctx(['cols']))).toBe(true);
__resetCommandsForTests();
expect(getCommand('select.parent')).toBeUndefined();
});
});
describe('Escape on a multi-selection', () => {
const Shortcuts: React.FC = () => { useCommandShortcuts(); return null; };
const esc = (target: EventTarget = document.body) =>
h.act(() => { target.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true, cancelable: true })); });
test('clears the whole selection (and still climbs for a single node)', () => {
const off = registerCommands(selectionCommands);
try {
h.mountChild(<Shortcuts />);
run(() => h.actions.selectNode(['b1', 'b2']));
expect(sel()).toEqual(['b1', 'b2']);
esc();
expect(sel()).toEqual([]);
run(() => h.actions.selectNode('b1'));
esc();
expect(sel()).toEqual(['col-0']);
} finally {
off();
}
});
test('not while typing in a text field', () => {
const off = registerCommands(selectionCommands);
const input = document.createElement('input');
try {
h.mountChild(<Shortcuts />);
run(() => h.actions.selectNode(['b1', 'b2']));
document.body.appendChild(input);
input.focus();
esc(input);
expect(sel()).toEqual(['b1', 'b2']);
} finally {
off();
input.remove();
}
});
});