5b75792d6a
抽取UI编辑器键盘快捷键处理模块 阻止对话框内撤销重做并保留普通按钮快捷键行为
63 lines
2.0 KiB
TypeScript
63 lines
2.0 KiB
TypeScript
// @vitest-environment jsdom
|
|
|
|
import { fireEvent } from '@testing-library/react';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import { handleUiEditorKeyDown } from '../src/view/ui-editor/uiEditorKeyboardShortcuts';
|
|
|
|
describe('ui editor keyboard shortcuts', () => {
|
|
afterEach(() => {
|
|
document.body.replaceChildren();
|
|
});
|
|
|
|
it.each([
|
|
{ key: 'z', ctrlKey: true },
|
|
{ key: 'z', ctrlKey: true, shiftKey: true },
|
|
{ key: 'y', ctrlKey: true },
|
|
])('does not change history from inside a modal (%o)', (shortcut) => {
|
|
const dialog = document.createElement('div');
|
|
dialog.setAttribute('role', 'dialog');
|
|
const button = document.createElement('button');
|
|
dialog.append(button);
|
|
document.body.append(dialog);
|
|
const historyUndo = vi.fn(() => true);
|
|
const historyRedo = vi.fn(() => true);
|
|
const listener = (event: KeyboardEvent) =>
|
|
handleUiEditorKeyDown(event, {
|
|
selectedNodeId: null,
|
|
activeImageId: null,
|
|
deleteNode: vi.fn(),
|
|
historyUndo,
|
|
historyRedo,
|
|
});
|
|
window.addEventListener('keydown', listener);
|
|
|
|
fireEvent.keyDown(button, shortcut);
|
|
|
|
expect(historyUndo).not.toHaveBeenCalled();
|
|
expect(historyRedo).not.toHaveBeenCalled();
|
|
window.removeEventListener('keydown', listener);
|
|
});
|
|
|
|
it('keeps undo available from a non-modal button', () => {
|
|
const button = document.createElement('button');
|
|
document.body.append(button);
|
|
const historyUndo = vi.fn(() => true);
|
|
const historyRedo = vi.fn(() => true);
|
|
const listener = (event: KeyboardEvent) =>
|
|
handleUiEditorKeyDown(event, {
|
|
selectedNodeId: null,
|
|
activeImageId: null,
|
|
deleteNode: vi.fn(),
|
|
historyUndo,
|
|
historyRedo,
|
|
});
|
|
window.addEventListener('keydown', listener);
|
|
|
|
fireEvent.keyDown(button, { key: 'z', ctrlKey: true });
|
|
|
|
expect(historyUndo).toHaveBeenCalledTimes(1);
|
|
window.removeEventListener('keydown', listener);
|
|
});
|
|
});
|