@@ -16,6 +16,7 @@ import {
1616 tabNavigateBackward ,
1717 tabNavigateToWorkspace ,
1818 keyRight ,
19+ getCurrentFocusNodeId ,
1920} from './test_setup.js' ;
2021
2122suite ( 'Toolbox and flyout test' , function ( ) {
@@ -226,6 +227,18 @@ suite('Toolbox and flyout test', function () {
226227 const flyoutIsOpen = await checkIfFlyoutIsOpen ( this . browser ) ;
227228 chai . assert . isFalse ( flyoutIsOpen ) ;
228229 } ) ;
230+
231+ test ( 'Clicking on toolbox category focuses it and opens flyout' , async function ( ) {
232+ const elemId = await findToolboxCategoryIdByName ( this . browser , 'Loops' ) ;
233+ const elem = this . browser . $ ( `#${ elemId } ` ) ;
234+ await elem . click ( ) ;
235+
236+ // The clicked category should now be focused.
237+ const focusedId = await getCurrentFocusNodeId ( this . browser ) ;
238+ const flyoutIsOpen = await checkIfFlyoutIsOpen ( this . browser ) ;
239+ chai . assert . strictEqual ( focusedId , elemId ) ;
240+ chai . assert . isTrue ( flyoutIsOpen ) ;
241+ } ) ;
229242} ) ;
230243
231244/**
@@ -246,3 +259,32 @@ async function checkIfFlyoutIsOpen(
246259 return flyout . isVisible ( ) ;
247260 } ) ;
248261}
262+
263+ /**
264+ * Finds the element ID of the toolbox category with the specified name.
265+ *
266+ * This throws an error if the current main workspace has no toolbox or the
267+ * toolbox does not have a category with the specified name.
268+ *
269+ * @param browser The active WebdriverIO Browser object.
270+ * @param name The name of the category to find.
271+ * @returns A promise with the focusable element ID of the sought category.
272+ */
273+ async function findToolboxCategoryIdByName (
274+ browser : WebdriverIO . Browser ,
275+ name : string ,
276+ ) : Promise < string > {
277+ return await browser . execute ( ( name ) => {
278+ const workspaceSvg = Blockly . getMainWorkspace ( ) as Blockly . WorkspaceSvg ;
279+ const toolbox = workspaceSvg . getToolbox ( ) as Blockly . Toolbox ;
280+ if ( ! toolbox ) throw new Error ( 'Workspace has no toolbox.' ) ;
281+
282+ for ( const item of toolbox . getToolboxItems ( ) ) {
283+ if ( item instanceof Blockly . ToolboxCategory && item . getName ( ) === name ) {
284+ return item . getFocusableElement ( ) . id ;
285+ }
286+ }
287+
288+ throw new Error ( `No category found with name: ${ name } .` ) ;
289+ } , name ) ;
290+ }
0 commit comments