Skip to content

Commit f6467b6

Browse files
test: move registerCommentOptions before plugin
Update tests so contextMenuExists does not care whether there's a shortcut defined. Separately assert the full context menu contents including shortcuts for blocks in the workspace and toolbox, workspace comments and the workspace itself. Fixes #648
1 parent d5151f2 commit f6467b6

File tree

5 files changed

+138
-24
lines changed

5 files changed

+138
-24
lines changed

src/shortcut_formatting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function getMenuItem(labelText: string, action: string): HTMLElement {
2222
label.textContent = labelText;
2323
const shortcut = document.createElement('span');
2424
shortcut.className = 'blocklyShortcut';
25-
shortcut.textContent = getShortActionShortcut(action);
25+
shortcut.textContent = ` ${getShortActionShortcut(action)}`;
2626
container.appendChild(label);
2727
container.appendChild(shortcut);
2828
return container;

test/webdriverio/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ function createWorkspace(): Blockly.WorkspaceSvg {
8282
KeyboardNavigation.registerKeyboardNavigationStyles();
8383
const workspace = Blockly.inject(blocklyDiv, injectOptions);
8484

85-
new KeyboardNavigation(workspace);
8685
Blockly.ContextMenuItems.registerCommentOptions();
86+
new KeyboardNavigation(workspace);
8787

8888
// Disable blocks that aren't inside the setup or draw loops.
8989
workspace.addChangeListener(Blockly.Events.disableOrphans);

test/webdriverio/test/actions_test.ts

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
testFileLocations,
1616
testSetup,
1717
keyRight,
18+
contextMenuItems,
1819
} from './test_setup.js';
1920

2021
suite('Menus test', function () {
@@ -27,20 +28,47 @@ suite('Menus test', function () {
2728
await this.browser.pause(PAUSE_TIME);
2829
});
2930

30-
test('Menu action opens menu', async function () {
31+
test('Menu on block', async function () {
3132
// Navigate to draw_circle_1.
3233
await tabNavigateToWorkspace(this.browser);
3334
await focusOnBlock(this.browser, 'draw_circle_1');
3435
await this.browser.pause(PAUSE_TIME);
3536
await this.browser.keys([Key.Ctrl, Key.Return]);
3637
await this.browser.pause(PAUSE_TIME);
37-
chai.assert.isTrue(
38-
await contextMenuExists(this.browser, 'Collapse Block'),
39-
'The menu should be openable on a block',
38+
39+
chai.assert.deepEqual(
40+
process.platform === 'darwin'
41+
? [
42+
{'text': 'Duplicate D'},
43+
{'text': 'Add Comment'},
44+
{'text': 'External Inputs'},
45+
{'text': 'Collapse Block'},
46+
{'text': 'Disable Block'},
47+
{'text': 'Delete 2 Blocks Delete'},
48+
{'text': 'Move Block M'},
49+
{'text': 'Edit Block contents Right'},
50+
{'text': 'Cut ⌘ X'},
51+
{'text': 'Copy ⌘ C'},
52+
{'disabled': true, 'text': 'Paste ⌘ V'},
53+
]
54+
: [
55+
{'text': 'Duplicate D'},
56+
{'text': 'Add Comment'},
57+
{'text': 'External Inputs'},
58+
{'text': 'Collapse Block'},
59+
{'text': 'Disable Block'},
60+
{'text': 'Delete 2 Blocks Delete'},
61+
{'text': 'Move Block M'},
62+
{'text': 'Edit Block contents Right'},
63+
{'text': 'Cut Ctrl+X'},
64+
{'text': 'Copy Ctrl+C'},
65+
{'disabled': true, 'text': 'Paste Ctrl+V'},
66+
],
67+
await contextMenuItems(this.browser),
4068
);
4169
});
4270

43-
test('Menu action returns true in the toolbox', async function () {
71+
test('Menu on block in the toolbox', async function () {
4472
// Navigate to draw_circle_1.
4573
await tabNavigateToWorkspace(this.browser);
4674
await focusOnBlock(this.browser, 'draw_circle_1');
@@ -51,13 +79,60 @@ suite('Menus test', function () {
5179
await this.browser.keys([Key.Ctrl, Key.Return]);
5280
await this.browser.pause(PAUSE_TIME);
5381

54-
chai.assert.isTrue(
55-
await contextMenuExists(this.browser, 'Help'),
56-
'The menu should be openable on a block in the toolbox',
82+
chai.assert.deepEqual(
83+
process.platform === 'darwin'
84+
? [
85+
{'text': 'Help'},
86+
{'disabled': true, 'text': 'Move Block M'},
87+
{'disabled': true, 'text': 'Cut ⌘ X'},
88+
{'text': 'Copy ⌘ C'},
89+
{'disabled': true, 'text': 'Paste ⌘ V'},
90+
]
91+
: [
92+
{'text': 'Help'},
93+
{'disabled': true, 'text': 'Move Block M'},
94+
{'disabled': true, 'text': 'Cut Ctrl+X'},
95+
{'text': 'Copy Ctrl+C'},
96+
{'disabled': true, 'text': 'Paste Ctrl+V'},
97+
],
98+
await contextMenuItems(this.browser),
99+
);
100+
});
101+
102+
test('Menu on workspace', async function () {
103+
// Navigate to draw_circle_1.
104+
await tabNavigateToWorkspace(this.browser);
105+
await this.browser.keys('w');
106+
await this.browser.keys([Key.Ctrl, Key.Return]);
107+
await this.browser.pause(PAUSE_TIME);
108+
109+
chai.assert.deepEqual(
110+
process.platform === 'darwin'
111+
? [
112+
{'disabled': true, 'text': 'Undo'},
113+
{'disabled': true, 'text': 'Redo'},
114+
{'text': 'Clean up Blocks'},
115+
{'text': 'Collapse Blocks'},
116+
{'disabled': true, 'text': 'Expand Blocks'},
117+
{'text': 'Delete 4 Blocks'},
118+
{'text': 'Add Comment'},
119+
{'disabled': true, 'text': 'Paste ⌘ V'},
120+
]
121+
: [
122+
{'disabled': true, 'text': 'Undo'},
123+
{'disabled': true, 'text': 'Redo'},
124+
{'text': 'Clean up Blocks'},
125+
{'text': 'Collapse Blocks'},
126+
{'disabled': true, 'text': 'Expand Blocks'},
127+
{'text': 'Delete 4 Blocks'},
128+
{'text': 'Add Comment'},
129+
{'disabled': true, 'text': 'Paste Ctrl+V'},
130+
],
131+
await contextMenuItems(this.browser),
57132
);
58133
});
59134

60-
test('Menu action returns false during drag', async function () {
135+
test('Menu on block during drag is not shown', async function () {
61136
// Navigate to draw_circle_1.
62137
await tabNavigateToWorkspace(this.browser);
63138
await focusOnBlock(this.browser, 'draw_circle_1');

test/webdriverio/test/test_setup.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,10 @@ export async function checkActionPrecondition(
589589
/**
590590
* Wait for the specified context menu item to exist.
591591
*
592+
* Does not check the shortcut.
593+
*
592594
* @param browser The active WebdriverIO Browser object.
593-
* @param itemText The display text of the context menu item to click.
595+
* @param itemText The display text of the context menu item without shortcut.
594596
* @param reverse Whether to check for non-existence instead.
595597
* @return A Promise that resolves when the actions are completed.
596598
*/
@@ -599,10 +601,38 @@ export async function contextMenuExists(
599601
itemText: string,
600602
reverse = false,
601603
): Promise<boolean> {
602-
const item = await browser.$(`div=${itemText}`);
604+
// XPath so as not to care if there's a shortcut which adds DOM structure.
605+
const item = await browser.$(
606+
`//div[contains(@class, "blocklyMenuItem")]//*[text()="${itemText}"]`,
607+
);
603608
return await item.waitForExist({timeout: 200, reverse: reverse});
604609
}
605610

611+
/**
612+
* Wait for the context menu and return a representation of its contents.
613+
*
614+
* The text field includes the shortcut if present.
615+
*
616+
* @param browser The active WebdriverIO Browser object.
617+
* @returns The context menu items.
618+
*/
619+
export async function contextMenuItems(browser: WebdriverIO.Browser): Promise<
620+
Array<{
621+
text: string;
622+
disabled?: true;
623+
}>
624+
> {
625+
await browser.$('.blocklyContextMenu').waitForExist();
626+
const items = await browser
627+
.$$('.blocklyContextMenu .blocklyMenuItem')
628+
.map(async (item) => {
629+
const text = await item.getComputedLabel();
630+
const disabled = (await item.getAttribute('aria-disabled')) === 'true';
631+
return disabled ? {text, disabled} : {text};
632+
});
633+
return items;
634+
}
635+
606636
/**
607637
* Find a clickable element on the block and click it.
608638
* We can't always use the block's SVG root because clicking will always happen

test/webdriverio/test/workspace_comment_test.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
keyRight,
2020
keyDown,
2121
keyUp,
22+
contextMenuItems,
2223
} from './test_setup.js';
2324
import {Key} from 'webdriverio';
2425

@@ -175,17 +176,25 @@ suite('Workspace comment navigation', function () {
175176
test('Action menu can be displayed for a workspace comment', async function () {
176177
await focusOnWorkspaceComment(this.browser, this.commentId1);
177178
await sendKeyAndWait(this.browser, [Key.Ctrl, Key.Return]);
178-
chai.assert.isTrue(
179-
await contextMenuExists(this.browser, 'Duplicate Comment'),
180-
'The menu should be openable on a workspace comment',
181-
);
182-
chai.assert.isTrue(
183-
await contextMenuExists(this.browser, 'Remove Comment'),
184-
'The menu should be openable on a workspace comment',
185-
);
186-
chai.assert.isTrue(
187-
await contextMenuExists(this.browser, 'Move CommentM'),
188-
'The menu should be openable on a workspace comment',
179+
chai.assert.deepEqual(
180+
process.platform === 'darwin'
181+
? [
182+
{'text': 'Duplicate Comment D'},
183+
{'text': 'Remove Comment'},
184+
{'text': 'Move Comment M'},
185+
{'text': 'Cut ⌘ X'},
186+
{'text': 'Copy ⌘ C'},
187+
{'disabled': true, 'text': 'Paste ⌘ V'},
188+
]
189+
: [
190+
{'text': 'Duplicate Comment D'},
191+
{'text': 'Remove Comment'},
192+
{'text': 'Move Comment M'},
193+
{'text': 'Cut Ctrl+X'},
194+
{'text': 'Copy Ctrl+C'},
195+
{'disabled': true, 'text': 'Paste Ctrl+V'},
196+
],
197+
await contextMenuItems(this.browser),
189198
);
190199
});
191200

0 commit comments

Comments
 (0)