Skip to content

Commit 8114110

Browse files
committed
test: Add tests
1 parent 0ef22ea commit 8114110

1 file changed

Lines changed: 214 additions & 18 deletions

File tree

packages/blockly/tests/mocha/shortcut_items_test.js

Lines changed: 214 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import {createKeyDownEvent} from './test_helpers/user_input.js';
1919
suite('Keyboard Shortcut Items', function () {
2020
setup(function () {
2121
sharedTestSetup.call(this);
22-
const toolbox = document.getElementById('toolbox-categories');
23-
this.workspace = Blockly.inject('blocklyDiv', {toolbox});
22+
const toolbox = document.getElementById('toolbox-test');
23+
this.workspace = Blockly.inject('blocklyDiv', {toolbox, renderer: 'zelos'});
2424
this.injectionDiv = this.workspace.getInjectionDiv();
2525
Blockly.ContextMenuRegistry.registry.reset();
2626
Blockly.ContextMenuItems.registerDefaultOptions();
@@ -553,22 +553,6 @@ suite('Keyboard Shortcut Items', function () {
553553
});
554554

555555
suite('Focus Toolbox (T)', function () {
556-
setup(function () {
557-
Blockly.defineBlocksWithJsonArray([
558-
{
559-
'type': 'basic_block',
560-
'message0': '%1',
561-
'args0': [
562-
{
563-
'type': 'field_input',
564-
'name': 'TEXT',
565-
'text': 'default',
566-
},
567-
],
568-
},
569-
]);
570-
});
571-
572556
test('Does not change focus when toolbox item is already focused', function () {
573557
const item = this.workspace.getToolbox().getToolboxItems()[1];
574558
Blockly.getFocusManager().focusNode(item);
@@ -799,4 +783,216 @@ suite('Keyboard Shortcut Items', function () {
799783
);
800784
});
801785
});
786+
787+
suite('Perform Action (Enter)', function () {
788+
test('Shows a toast with navigation hints on the workspace', function () {
789+
const toastSpy = sinon.spy(Blockly.Toast, 'show');
790+
791+
Blockly.getFocusManager().focusNode(this.workspace);
792+
793+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.ENTER);
794+
this.workspace.getInjectionDiv().dispatchEvent(event);
795+
796+
sinon.assert.calledWith(toastSpy, this.workspace, {
797+
id: 'workspaceNavigationHint',
798+
message: Blockly.Msg['KEYBOARD_NAV_WORKSPACE_NAVIGATION_HINT'],
799+
});
800+
801+
toastSpy.restore();
802+
});
803+
804+
test('Inserts blocks from the flyout in move mode', function () {
805+
this.workspace.getToolbox().selectItemByPosition(0);
806+
const block = this.workspace
807+
.getNavigator()
808+
.getFirstChild(this.workspace.getFlyout().getWorkspace());
809+
assert.instanceOf(block, Blockly.BlockSvg);
810+
Blockly.getFocusManager().focusNode(block);
811+
812+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.ENTER);
813+
this.workspace.getInjectionDiv().dispatchEvent(event);
814+
815+
const movingBlock = Blockly.getFocusManager().getFocusedNode();
816+
assert.notEqual(block, movingBlock);
817+
assert.instanceOf(movingBlock, Blockly.BlockSvg);
818+
assert.isTrue(movingBlock.isDragging());
819+
assert.isFalse(movingBlock.workspace.isFlyout);
820+
821+
Blockly.KeyboardMover.mover.abortMove();
822+
});
823+
824+
test('Shows a toast with navigation hints for navigable blocks', function () {
825+
const toastSpy = sinon.spy(Blockly.Toast, 'show');
826+
827+
const block = this.workspace.newBlock('controls_if');
828+
block.initSvg();
829+
block.render();
830+
Blockly.getFocusManager().focusNode(block);
831+
832+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.ENTER);
833+
this.workspace.getInjectionDiv().dispatchEvent(event);
834+
835+
sinon.assert.calledWith(toastSpy, this.workspace, {
836+
id: 'blockNavigationHint',
837+
message: Blockly.Msg['KEYBOARD_NAV_BLOCK_NAVIGATION_HINT'],
838+
});
839+
toastSpy.restore();
840+
});
841+
842+
test('Shows a toast with instructions to view help for non-navigable blocks', function () {
843+
const toastSpy = sinon.spy(Blockly.Toast, 'show');
844+
845+
const block = this.workspace.newBlock('test_align_dummy_right');
846+
block.initSvg();
847+
block.render();
848+
Blockly.getFocusManager().focusNode(block);
849+
850+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.ENTER);
851+
this.workspace.getInjectionDiv().dispatchEvent(event);
852+
853+
sinon.assert.calledWith(toastSpy, this.workspace, {
854+
id: 'helpHint',
855+
message: Blockly.Msg['HELP_PROMPT'].replace('%1', ''),
856+
});
857+
toastSpy.restore();
858+
});
859+
860+
test('Focuses field editor for blocks with full-block fields', function () {
861+
const block = this.workspace.newBlock('math_number');
862+
block.initSvg();
863+
block.render();
864+
Blockly.getFocusManager().focusNode(block);
865+
866+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.ENTER);
867+
this.workspace.getInjectionDiv().dispatchEvent(event);
868+
869+
const field = block.getField('NUM');
870+
assert.isTrue(Blockly.WidgetDiv.isVisible());
871+
assert.isTrue(field.isBeingEdited_);
872+
});
873+
874+
test('Focuses field editor for fields', function () {
875+
const block = this.workspace.newBlock('logic_compare');
876+
block.initSvg();
877+
block.render();
878+
const field = block.getField('OP');
879+
Blockly.getFocusManager().focusNode(field);
880+
881+
assert.isFalse(Blockly.DropDownDiv.isVisible());
882+
883+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.ENTER);
884+
this.workspace.getInjectionDiv().dispatchEvent(event);
885+
886+
assert.isTrue(Blockly.DropDownDiv.isVisible());
887+
});
888+
889+
test('Expands and focuses workspace comment editors', function () {
890+
const comment = this.workspace.newComment();
891+
comment.setCollapsed(true);
892+
Blockly.getFocusManager().focusNode(comment);
893+
894+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.ENTER);
895+
this.workspace.getInjectionDiv().dispatchEvent(event);
896+
897+
assert.strictEqual(
898+
Blockly.getFocusManager().getFocusedNode(),
899+
comment.getEditorFocusableNode(),
900+
);
901+
assert.isFalse(comment.view.isCollapsed());
902+
});
903+
904+
test('Focuses mutator workspace for mutator bubble', async function () {
905+
const block = this.workspace.newBlock('controls_if');
906+
block.initSvg();
907+
block.render();
908+
const icon = block.getIcon(Blockly.icons.MutatorIcon.TYPE);
909+
await icon.setBubbleVisible(true);
910+
Blockly.getFocusManager().focusNode(icon.getBubble());
911+
912+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.ENTER);
913+
this.workspace.getInjectionDiv().dispatchEvent(event);
914+
915+
assert.strictEqual(
916+
Blockly.getFocusManager().getFocusedTree(),
917+
icon.getWorkspace(),
918+
);
919+
});
920+
921+
test('Focuses comment editor for block comment bubble', async function () {
922+
const block = this.workspace.newBlock('controls_if');
923+
block.initSvg();
924+
block.render();
925+
block.setCommentText('Hello');
926+
const icon = block.getIcon(Blockly.icons.CommentIcon.TYPE);
927+
await icon.setBubbleVisible(true);
928+
Blockly.getFocusManager().focusNode(icon.getBubble());
929+
930+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.ENTER);
931+
this.workspace.getInjectionDiv().dispatchEvent(event);
932+
933+
assert.strictEqual(
934+
Blockly.getFocusManager().getFocusedNode(),
935+
icon.getBubble().getEditor(),
936+
);
937+
});
938+
939+
test('Focuses bubble for icons', async function () {
940+
const block = this.workspace.newBlock('controls_if');
941+
block.initSvg();
942+
block.render();
943+
944+
block.setCommentText('Hello world');
945+
block.setWarningText('Danger!');
946+
947+
const iconTypes = [
948+
Blockly.icons.CommentIcon.TYPE,
949+
Blockly.icons.WarningIcon.TYPE,
950+
Blockly.icons.MutatorIcon.TYPE,
951+
];
952+
953+
for (const iconType of iconTypes) {
954+
const icon = block.getIcon(iconType);
955+
Blockly.getFocusManager().focusNode(icon);
956+
957+
const bubbleShown = new Promise((resolve) => {
958+
this.workspace.addChangeListener((event) => {
959+
if (event.type === Blockly.Events.BUBBLE_OPEN) {
960+
resolve();
961+
}
962+
});
963+
});
964+
965+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.ENTER);
966+
this.workspace.getInjectionDiv().dispatchEvent(event);
967+
968+
this.clock.tick(100);
969+
970+
await bubbleShown;
971+
assert.strictEqual(
972+
Blockly.getFocusManager().getFocusedNode(),
973+
icon.getBubble(),
974+
);
975+
}
976+
});
977+
978+
test('Triggers flyout button actions', function () {
979+
const toolbox = this.workspace.getToolbox();
980+
toolbox.selectItemByPosition(3);
981+
const button = this.workspace.getFlyout().getContents()[0].getElement();
982+
assert.instanceOf(button, Blockly.FlyoutButton);
983+
Blockly.getFocusManager().focusNode(button);
984+
985+
const oldCallback = this.workspace.getButtonCallback('CREATE_VARIABLE');
986+
let called = false;
987+
this.workspace.registerButtonCallback('CREATE_VARIABLE', () => {
988+
called = true;
989+
});
990+
991+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.ENTER);
992+
this.workspace.getInjectionDiv().dispatchEvent(event);
993+
994+
assert.isTrue(called);
995+
this.workspace.registerButtonCallback('CREATE_VARIABLE', oldCallback);
996+
});
997+
});
802998
});

0 commit comments

Comments
 (0)