Skip to content

Commit 3739758

Browse files
committed
refactor(tests): Factor out getConnectedBlockInfo; improve naming
Create a new test helper to get info about what's connected to a given connection, used in both move tests, plus rename getSelectedNeighbourInfo to getFocusedNeighbourInfo.
1 parent 8f31409 commit 3739758

File tree

1 file changed

+65
-52
lines changed

1 file changed

+65
-52
lines changed

test/webdriverio/test/move_test.ts

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ suite('Move tests', function () {
3939

4040
// Get information about parent connection of selected block,
4141
// and block connected to selected block's next connection.
42-
const info = await getSelectedNeighbourInfo(this.browser);
42+
const info = await getFocusedNeighbourInfo(this.browser);
4343

4444
chai.assert(info.parentId, 'selected block has no parent block');
4545
chai.assert(
@@ -54,7 +54,7 @@ suite('Move tests', function () {
5454
// Check that the moving block has nothing connected it its
5555
// next/previous connections, and same thing connected to value
5656
// input.
57-
const newInfo = await getSelectedNeighbourInfo(this.browser);
57+
const newInfo = await getFocusedNeighbourInfo(this.browser);
5858
chai.assert(
5959
newInfo.parentId === null,
6060
'moving block should have no parent block',
@@ -69,25 +69,16 @@ suite('Move tests', function () {
6969
'moving block should have same attached value block',
7070
);
7171

72-
// Get ID of next block now connected to the (former) parent
73-
// connection of the currently-moving block (skipping insertion
74-
// markers), and make sure it's same as the ID of the block that
75-
// was formerly attached to the moving block's next connection.
76-
const newNextId = await this.browser.execute(
77-
(parentId: string, index: number) => {
78-
const parent = Blockly.getMainWorkspace().getBlockById(parentId);
79-
if (!parent) throw new Error('parent block gone');
80-
let block = parent.getConnections_(true)[index].targetBlock();
81-
while (block?.isInsertionMarker()) {
82-
block = block.getNextBlock();
83-
}
84-
return block?.id;
85-
},
72+
// Check that the block connected to the former parent
73+
// connection of the currently-moving block (if any) is the one
74+
// that was attached to the moving block's next connection.
75+
const parentInfo = await getConnectedBlockInfo(
76+
this.browser,
8677
info.parentId,
8778
info.parentIndex,
8879
);
8980
chai.assert.strictEqual(
90-
newNextId,
81+
parentInfo.id,
9182
info.nextId,
9283
'former parent connection should be connected to former next block',
9384
);
@@ -108,7 +99,7 @@ suite('Move tests', function () {
10899

109100
// Get information about parent connection of selected block,
110101
// and block connected to selected block's value input.
111-
const info = await getSelectedNeighbourInfo(this.browser);
102+
const info = await getFocusedNeighbourInfo(this.browser);
112103

113104
chai.assert(info.parentId, 'selected block has no parent block');
114105
chai.assert(
@@ -123,7 +114,7 @@ suite('Move tests', function () {
123114
// Check that the moving block has nothing connected it its
124115
// next/previous connections, and same thing connected to value
125116
// input.
126-
const newInfo = await getSelectedNeighbourInfo(this.browser);
117+
const newInfo = await getFocusedNeighbourInfo(this.browser);
127118
chai.assert(
128119
newInfo.parentId === null,
129120
'moving block should have no parent block',
@@ -138,36 +129,18 @@ suite('Move tests', function () {
138129
'moving block should have same attached value block',
139130
);
140131

141-
// Check the (former) parent connection of the currently-moving
142-
// block is (skipping insertion markers) either unconnected or
143-
// connected to a shadow block, and that is is not the block
144-
// (originally and still) connected to the moving block's zeroth
145-
// value input.
146-
const newValueInfo = await this.browser.execute(
147-
(parentId: string, index: number) => {
148-
const parent = Blockly.getMainWorkspace().getBlockById(parentId);
149-
if (!parent) throw new Error('parent block gone');
150-
let block = parent.getConnections_(true)[index].targetBlock() ?? null;
151-
while (block?.isInsertionMarker()) {
152-
block = block.inputList[0].connection?.targetBlock() ?? null;
153-
}
154-
return {
155-
id: block?.id ?? null,
156-
shadow: block?.isShadow() ?? null,
157-
};
158-
},
132+
// Check that the former parent connection of the
133+
// currently-moving block is either unconnected or connected to
134+
// a shadow block.
135+
const parentInfo = await getConnectedBlockInfo(
136+
this.browser,
159137
info.parentId,
160138
info.parentIndex,
161139
);
162140
chai.assert(
163-
newValueInfo.id === null || newValueInfo.shadow,
141+
parentInfo.id === null || parentInfo.shadow,
164142
'former parent connection should be unconnected (or shadow)',
165143
);
166-
chai.assert.notStrictEqual(
167-
newValueInfo.id,
168-
info.valueId,
169-
'former parent connection should NOT be connected to value block',
170-
);
171144

172145
// Abort move.
173146
await this.browser.keys(Key.Escape);
@@ -179,16 +152,13 @@ suite('Move tests', function () {
179152
* Get information about the currently-selected block's parent and
180153
* child blocks.
181154
*
182-
* N.B. explicitly converts any undefined values to null because
183-
* browser.execute does this implicitly and so otherwise this function
184-
* would return values that were not compliant with its own inferred
185-
* type signature!
186-
*
187-
* @returns A promise setting to an object containing the parent block
188-
* ID, index of parent connection, next block ID, and ID of the block
189-
* connected to the zeroth value value input.
155+
* @returns A promise setting to {parentId, parentIndex, nextId,
156+
* valueId}, being respectively the parent block ID, index of parent
157+
* connection, next block ID, and ID of the block connected to the
158+
* zeroth value value input, or null if the given item does not
159+
* exist.
190160
*/
191-
function getSelectedNeighbourInfo(browser: WebdriverIO.Browser) {
161+
function getFocusedNeighbourInfo(browser: WebdriverIO.Browser) {
192162
return browser.execute(() => {
193163
const focused = Blockly.getFocusManager().getFocusedNode();
194164
if (!focused) throw new Error('nothing focused');
@@ -197,6 +167,10 @@ function getSelectedNeighbourInfo(browser: WebdriverIO.Browser) {
197167
}
198168
const block = focused; // Inferred as BlockSvg.
199169
const parent = block?.getParent();
170+
// N.B. explicitly converts any undefined values to null because
171+
// browser.execute does this implicitly and so otherwise this
172+
// function would return values that were not compliant with its
173+
// own inferred type signature!
200174
return {
201175
parentId: parent?.id ?? null,
202176
parentIndex:
@@ -208,3 +182,42 @@ function getSelectedNeighbourInfo(browser: WebdriverIO.Browser) {
208182
};
209183
});
210184
}
185+
186+
/**
187+
* Given a block ID and index of a connection on that block, find the
188+
* first block connected to that block (skipping any insertion markers),
189+
* and return that block's ID and a boolean indicating whether that
190+
* block is a shadow or not.
191+
*
192+
* @param id The ID of the block having the connection we wish to examine.
193+
* @param index The index of the connection we wish to examine, within
194+
* the array returned by calling .getConnections_(true) on that block.
195+
* @returns A promise settling to {id, shadow}, where id is the ID of
196+
* the connected block or null if no block is connected, and
197+
* shadow is true iff the connected block is a shadow.
198+
*/
199+
function getConnectedBlockInfo(
200+
browser: WebdriverIO.Browser,
201+
id: string,
202+
index: number,
203+
) {
204+
return browser.execute(
205+
(id: string, index: number) => {
206+
const parent = Blockly.getMainWorkspace().getBlockById(id);
207+
if (!parent) throw new Error('parent block gone');
208+
let block = parent.getConnections_(true)[index].targetBlock() ?? null;
209+
while (block?.isInsertionMarker()) {
210+
// If insertion marker, try to follow next or zeroth input connection.
211+
const connection =
212+
block.nextConnection ?? block.inputList[0].connection;
213+
block = connection?.targetBlock() ?? null;
214+
}
215+
return {
216+
id: block?.id ?? null,
217+
shadow: block?.isShadow() ?? false,
218+
};
219+
},
220+
id,
221+
index,
222+
);
223+
}

0 commit comments

Comments
 (0)