diff --git a/packages/blockly/core/icons/mutator_icon.ts b/packages/blockly/core/icons/mutator_icon.ts index b6c2e6a975e..f00b408b3a2 100644 --- a/packages/blockly/core/icons/mutator_icon.ts +++ b/packages/blockly/core/icons/mutator_icon.ts @@ -170,6 +170,7 @@ export class MutatorIcon extends Icon implements IHasBubble { if (this.bubbleIsVisible() === visible) return; await renderManagement.finishQueuedRenders(); + if (this.sourceBlock.isDeadOrDying()) return; if (visible) { this.miniWorkspaceBubble = new MiniWorkspaceBubble( diff --git a/packages/blockly/core/icons/warning_icon.ts b/packages/blockly/core/icons/warning_icon.ts index d0cc9e25369..07a084457fd 100644 --- a/packages/blockly/core/icons/warning_icon.ts +++ b/packages/blockly/core/icons/warning_icon.ts @@ -176,6 +176,7 @@ export class WarningIcon extends Icon implements IHasBubble { if (this.bubbleIsVisible() === visible) return; await renderManagement.finishQueuedRenders(); + if (this.sourceBlock.isDeadOrDying()) return; if (visible) { this.textBubble = new TextBubble( diff --git a/packages/blockly/tests/mocha/block_test.js b/packages/blockly/tests/mocha/block_test.js index 65460e4337c..47214f0ef86 100644 --- a/packages/blockly/tests/mocha/block_test.js +++ b/packages/blockly/tests/mocha/block_test.js @@ -1954,6 +1954,18 @@ suite('Blocks', function () { assert.equal(label, 'Bubble'); }); }); + + test('disposing the block while the bubble is opening does not open it', async function () { + this.block.setWarningText('Warning'); + const icon = this.block.getIcon(Blockly.icons.WarningIcon.TYPE); + const opening = icon.setBubbleVisible(true); + this.block.dispose(); + // Run the frame that setBubbleVisible waits for. + this.clock.runAll(); + await opening; + + assert.isFalse(icon.bubbleIsVisible()); + }); }); suite('Warning icons and collapsing', function () { diff --git a/packages/blockly/tests/mocha/focus_manager_test.js b/packages/blockly/tests/mocha/focus_manager_test.js index 032e7c35d65..e335265b95e 100644 --- a/packages/blockly/tests/mocha/focus_manager_test.js +++ b/packages/blockly/tests/mocha/focus_manager_test.js @@ -130,9 +130,6 @@ suite('FocusManager', function () { } this.allFocusableTrees = []; this.allFocusableNodes = []; - - // Reset the current active element. - document.body.focus(); }); assert.includesClass = function (classList, className) { diff --git a/packages/blockly/tests/mocha/mutator_test.js b/packages/blockly/tests/mocha/mutator_test.js index 3278af6b58b..e2697866c9d 100644 --- a/packages/blockly/tests/mocha/mutator_test.js +++ b/packages/blockly/tests/mocha/mutator_test.js @@ -136,4 +136,46 @@ suite('Mutator', function () { assert.equal(label, 'Bubble'); }); }); + + suite('Disposing the block', function () { + setup(function () { + this.workspace = Blockly.inject('blocklyDiv', DEFAULT_INJECT_OPTIONS); + this.block = createRenderedBlock(this.workspace, 'controls_if'); + this.icon = this.block.getIcon(Blockly.icons.MutatorIcon.TYPE); + }); + + teardown(function () { + sharedTestTeardown.call(this); + }); + + test('while the bubble is opening does not open it', async function () { + const opening = this.icon.setBubbleVisible(true); + this.block.dispose(); + await opening; + + assert.isFalse(this.icon.bubbleIsVisible()); + assertEventNotFired( + this.eventsFireStub, + Blockly.Events.BubbleOpen, + {}, + this.workspace.id, + this.block.id, + ); + }); + + test('while the bubble is closing does not fire a close event', async function () { + await this.icon.setBubbleVisible(true); + const closing = this.icon.setBubbleVisible(false); + this.block.dispose(); + await closing; + + assertEventNotFired( + this.eventsFireStub, + Blockly.Events.BubbleOpen, + {isOpen: false}, + this.workspace.id, + this.block.id, + ); + }); + }); }); diff --git a/packages/blockly/tests/mocha/test_helpers/setup_teardown.js b/packages/blockly/tests/mocha/test_helpers/setup_teardown.js index 1801b8278f6..181fcc8fed1 100644 --- a/packages/blockly/tests/mocha/test_helpers/setup_teardown.js +++ b/packages/blockly/tests/mocha/test_helpers/setup_teardown.js @@ -227,6 +227,14 @@ export function sharedTestTeardown(workspace = this.workspace) { ); } + // Clear focus while the fake clock is still installed. While an element + // has focus, JSDom starts its animation frame interval on any DOM change, + // using whatever setInterval is current. If that happens after the + // restore below, later tests' animation frames end up on a real timer + // their fake clock cannot run, so their events arrive late. + document.activeElement?.blur?.(); + this.clock.runAll(); + // Restore all stubbed methods. this.sharedSetupSandbox_.restore(); sinon.restore();