Skip to content

Commit d1ff7b0

Browse files
fix: make sure initial candidate is valid (#503)
* fix: make sure initial candidate is valid Previously when inserting a C-shape block with a statement block selected the code assumed the C-shaped block had a previous connection. The preview code then failed to preview the nonsense connection. Fixes #476 * chore: implement test for insert bug scenario
1 parent b37ab5e commit d1ff7b0

File tree

3 files changed

+100
-12
lines changed

3 files changed

+100
-12
lines changed

src/keyboard_drag_strategy.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -297,18 +297,26 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
297297
if (neighbour) {
298298
this.searchNode = neighbour;
299299
switch (neighbour.type) {
300-
case ConnectionType.INPUT_VALUE:
301-
return {
302-
neighbour: neighbour,
303-
local: this.block.outputConnection,
304-
distance: 0,
305-
};
306-
case ConnectionType.NEXT_STATEMENT:
307-
return {
308-
neighbour: neighbour,
309-
local: this.block.previousConnection,
310-
distance: 0,
311-
};
300+
case ConnectionType.INPUT_VALUE: {
301+
if (this.block.outputConnection) {
302+
return {
303+
neighbour: neighbour,
304+
local: this.block.outputConnection,
305+
distance: 0,
306+
};
307+
}
308+
break;
309+
}
310+
case ConnectionType.NEXT_STATEMENT: {
311+
if (this.block.previousConnection) {
312+
return {
313+
neighbour: neighbour,
314+
local: this.block.previousConnection,
315+
distance: 0,
316+
};
317+
}
318+
break;
319+
}
312320
}
313321
}
314322
return null;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
import * as chai from 'chai';
8+
import {Browser, Key} from 'webdriverio';
9+
import {
10+
getFocusedBlockType,
11+
PAUSE_TIME,
12+
setCurrentCursorNodeById,
13+
tabNavigateToWorkspace,
14+
testFileLocations,
15+
testSetup,
16+
} from './test_setup.js';
17+
18+
suite.only('Insert test', function () {
19+
// Setting timeout to unlimited as these tests take longer time to run
20+
this.timeout(0);
21+
22+
// Clear the workspace and load start blocks
23+
setup(async function () {
24+
this.browser = await testSetup(testFileLocations.BASE);
25+
await this.browser.pause(PAUSE_TIME);
26+
});
27+
28+
test('Insert C-shaped block with statement block selected', async function () {
29+
// Navigate to draw_circle_1.
30+
await tabNavigateToWorkspace(this.browser);
31+
await setCurrentCursorNodeById(this.browser, 'draw_circle_1');
32+
33+
await moveToToolboxCategory(this.browser, 'Functions');
34+
// Move to flyout.
35+
await this.browser.keys(Key.ArrowRight);
36+
// Select Function block.
37+
await this.browser.keys(Key.Enter);
38+
// Confirm move.
39+
await this.browser.keys(Key.Enter);
40+
41+
chai.assert.equal(
42+
'procedures_defnoreturn',
43+
await getFocusedBlockType(this.browser),
44+
);
45+
});
46+
});
47+
48+
async function moveToToolboxCategory(browser: Browser, category: string) {
49+
await browser.keys('t');
50+
const categoryIndex = await browser.execute((category) => {
51+
const all = Array.from(
52+
document.querySelectorAll('.blocklyToolboxCategoryLabel'),
53+
).map((node) => node.textContent);
54+
return all.indexOf(category);
55+
}, category);
56+
if (categoryIndex < 0) {
57+
throw new Error(`No category found: ${category}`);
58+
}
59+
if (categoryIndex > 0) {
60+
await browser.keys(Key.ArrowDown.repeat(categoryIndex));
61+
}
62+
}

test/webdriverio/test/test_setup.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,24 @@ export async function getCurrentFocusNodeId(
231231
});
232232
}
233233

234+
/**
235+
* Get the block type of the current focused node. Assumes the current node
236+
* is a block.
237+
*
238+
* @param browser The active WebdriverIO Browser object.
239+
* @returns A Promise that resolves to the block type of the current cursor
240+
* node.
241+
*/
242+
export async function getFocusedBlockType(
243+
browser: WebdriverIO.Browser,
244+
): Promise<string | undefined> {
245+
return await browser.execute(() => {
246+
const block = Blockly.getFocusManager().getFocusedNode() as
247+
| Blockly.BlockSvg
248+
| undefined;
249+
return block?.type;
250+
});
251+
}
234252
/**
235253
* Get the connection type of the current focused node. Assumes the current node
236254
* is a connection.

0 commit comments

Comments
 (0)