Skip to content

Commit 91ad65c

Browse files
committed
test: Add tests for contextual menus on icons
1 parent 1176951 commit 91ad65c

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

packages/blockly/tests/mocha/icon_test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,34 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
import * as Blockly from '../../build/src/core/blockly.js';
78
import {assert} from '../../node_modules/chai/index.js';
89
import {defineEmptyBlock} from './test_helpers/block_definitions.js';
910
import {MockIcon, MockSerializableIcon} from './test_helpers/icon_mocks.js';
1011
import {
1112
sharedTestSetup,
1213
sharedTestTeardown,
1314
} from './test_helpers/setup_teardown.js';
15+
import {simulateClick} from './test_helpers/user_input.js';
16+
17+
class TestIcon extends Blockly.icons.Icon {
18+
showContextMenu(e) {
19+
const menuItems = [
20+
{text: 'Test icon menu item', enabled: true, callback: () => {}},
21+
];
22+
Blockly.ContextMenu.show(
23+
e,
24+
menuItems,
25+
false,
26+
this.getSourceBlock().workspace,
27+
this.workspaceLocation,
28+
);
29+
}
30+
31+
getType() {
32+
new Blockly.icons.IconType() < TestIcon > 'test';
33+
}
34+
}
1435

1536
suite('Icon', function () {
1637
setup(function () {
@@ -366,4 +387,45 @@ suite('Icon', function () {
366387
);
367388
});
368389
});
390+
391+
suite('Contextual menus', function () {
392+
setup(function () {
393+
this.workspace = Blockly.inject('blocklyDiv', {});
394+
Blockly.icons.registry.register(
395+
new Blockly.icons.IconType('test'),
396+
TestIcon,
397+
);
398+
399+
this.block = this.workspace.newBlock('empty_block');
400+
this.block.initSvg();
401+
});
402+
403+
test('are shown when icons are right clicked', function () {
404+
const icon = new TestIcon(this.block);
405+
this.block.addIcon(icon);
406+
simulateClick(icon.getFocusableElement(), {button: 2});
407+
408+
const menu = document.querySelector('.blocklyContextMenu');
409+
assert.isNotNull(menu);
410+
assert.isTrue(menu.innerText.includes('Test icon menu item'));
411+
});
412+
413+
test('default to the contextual menu of the parent block', function () {
414+
this.block.setCommentText('hello there');
415+
const icon = this.block.getIcon(Blockly.icons.IconType.COMMENT);
416+
simulateClick(icon.getFocusableElement(), {button: 2});
417+
418+
const expectedItems =
419+
Blockly.ContextMenuRegistry.registry.getContextMenuOptions({
420+
block: this.block,
421+
});
422+
423+
assert.isNotEmpty(expectedItems);
424+
const menu = document.querySelector('.blocklyContextMenu');
425+
for (const item of expectedItems) {
426+
if (!item.text) continue;
427+
assert.isTrue(menu.innerText.includes(item.text));
428+
}
429+
});
430+
});
369431
});

0 commit comments

Comments
 (0)