fix: use copyable interface for cut action, add tests (#8993)

* fix: use copyable for cut action

* chore: rename keydown test

* chore: add tests for shortcut items not acting on focused connections
This commit is contained in:
Maribeth Moffatt
2025-05-06 14:37:28 -04:00
committed by GitHub
parent eb5181e3ef
commit 86c831a3fe
3 changed files with 44 additions and 5 deletions

View File

@@ -13,7 +13,6 @@ import {Gesture} from './gesture.js';
import {ICopyData, isCopyable} from './interfaces/i_copyable.js';
import {isDeletable} from './interfaces/i_deletable.js';
import {isDraggable} from './interfaces/i_draggable.js';
import {isSelectable} from './interfaces/i_selectable.js';
import {KeyboardShortcut, ShortcutRegistry} from './shortcut_registry.js';
import {Coordinate} from './utils/coordinate.js';
import {KeyCodes} from './utils/keycodes.js';
@@ -163,7 +162,7 @@ export function registerCut() {
focused.isDeletable() &&
isDraggable(focused) &&
focused.isMovable() &&
isSelectable(focused) &&
isCopyable(focused) &&
!focused.workspace.isFlyout
);
},

View File

@@ -242,7 +242,6 @@
import './jso_deserialization_test.js';
import './jso_serialization_test.js';
import './json_test.js';
import './keydown_test.js';
import './layering_test.js';
import './blocks/lists_test.js';
import './blocks/logic_ternary_test.js';
@@ -258,6 +257,7 @@
import './registry_test.js';
import './render_management_test.js';
import './serializer_test.js';
import './shortcut_items_test.js';
import './shortcut_registry_test.js';
import './touch_test.js';
import './theme_test.js';

View File

@@ -12,7 +12,7 @@ import {
} from './test_helpers/setup_teardown.js';
import {createKeyDownEvent} from './test_helpers/user_input.js';
suite('Key Down', function () {
suite('Keyboard Shortcut Items', function () {
setup(function () {
sharedTestSetup.call(this);
this.workspace = Blockly.inject('blocklyDiv', {});
@@ -35,6 +35,18 @@ suite('Key Down', function () {
return block;
}
/**
* Creates a block and sets its nextConnection as the focused node.
* @param {Blockly.Workspace} workspace The workspace to create a new block on.
*/
function setSelectedConnection(workspace) {
defineStackBlock();
const block = workspace.newBlock('stack_block');
sinon
.stub(Blockly.getFocusManager(), 'getFocusedNode')
.returns(block.nextConnection);
}
/**
* Creates a test for not running keyDown events when the workspace is in read only mode.
* @param {Object} keyEvent Mocked key down event. Use createKeyDownEvent.
@@ -73,9 +85,14 @@ suite('Key Down', function () {
this.injectionDiv.dispatchEvent(this.event);
sinon.assert.notCalled(this.hideChaffSpy);
});
test('Called when connection is focused', function () {
setSelectedConnection(this.workspace);
this.injectionDiv.dispatchEvent(this.event);
sinon.assert.calledOnce(this.hideChaffSpy);
});
});
suite('Delete Block', function () {
suite('Delete', function () {
setup(function () {
this.hideChaffSpy = sinon.spy(
Blockly.WorkspaceSvg.prototype,
@@ -89,6 +106,7 @@ suite('Key Down', function () {
['Backspace', createKeyDownEvent(Blockly.utils.KeyCodes.BACKSPACE)],
];
// Delete a block.
// Note that chaff is hidden when a block is deleted.
suite('Simple', function () {
testCases.forEach(function (testCase) {
const testCaseName = testCase[0];
@@ -108,6 +126,16 @@ suite('Key Down', function () {
runReadOnlyTest(keyEvent, testCaseName);
});
});
// Do not delete anything if a connection is focused.
test('Not called when connection is focused', function () {
// Restore the stub behavior called during setup
Blockly.getFocusManager().getFocusedNode.restore();
setSelectedConnection(this.workspace);
const event = createKeyDownEvent(Blockly.utils.KeyCodes.DELETE);
this.injectionDiv.dispatchEvent(event);
sinon.assert.notCalled(this.hideChaffSpy);
});
});
suite('Copy', function () {
@@ -194,6 +222,18 @@ suite('Key Down', function () {
});
});
});
test('Not called when connection is focused', function () {
// Restore the stub behavior called during setup
Blockly.getFocusManager().getFocusedNode.restore();
setSelectedConnection(this.workspace);
const event = createKeyDownEvent(Blockly.utils.KeyCodes.C, [
Blockly.utils.KeyCodes.CTRL,
]);
this.injectionDiv.dispatchEvent(event);
sinon.assert.notCalled(this.copySpy);
sinon.assert.notCalled(this.hideChaffSpy);
});
});
suite('Undo', function () {