From eb4288219ecb38ec4e38f32588089dec07d8f312 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Tue, 13 Jan 2026 14:52:19 -0800 Subject: [PATCH] fix: Fix bug that prevented redoing adding empty block comments (#9568) * fix: Fix bug that prevented redoing adding empty block comments * test: Add tests for undoing/redoing adding comments * test: Add tests for un/redoing adding non-empty comments --- core/events/events_block_change.ts | 2 +- tests/mocha/comment_test.js | 45 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/core/events/events_block_change.ts b/core/events/events_block_change.ts index e71eabb17..d4a8ba9d2 100644 --- a/core/events/events_block_change.ts +++ b/core/events/events_block_change.ts @@ -193,7 +193,7 @@ export class BlockChange extends BlockBase { break; } case 'comment': - block.setCommentText((value as string) || null); + block.setCommentText((value as string) ?? null); break; case 'collapsed': block.setCollapsed(!!value); diff --git a/tests/mocha/comment_test.js b/tests/mocha/comment_test.js index 1f52df8fd..a7b2635b9 100644 --- a/tests/mocha/comment_test.js +++ b/tests/mocha/comment_test.js @@ -167,4 +167,49 @@ suite('Comments', function () { assertBubbleLocation(this.comment, 100, 100); }); }); + suite('Undo/Redo', function () { + test('Adding an empty comment can be undone', function () { + const block = this.workspace.newBlock('empty_block'); + block.setCommentText(''); + assert.isNotNull(block.getIcon(Blockly.icons.IconType.COMMENT)); + assert.equal(block.getCommentText(), ''); + + this.workspace.undo(false); + + assert.isUndefined(block.getIcon(Blockly.icons.IconType.COMMENT)); + assert.isNull(block.getCommentText()); + }); + + test('Adding an empty comment can be redone', function () { + const block = this.workspace.newBlock('empty_block'); + block.setCommentText(''); + this.workspace.undo(false); + this.workspace.undo(true); + + assert.isNotNull(block.getIcon(Blockly.icons.IconType.COMMENT)); + assert.equal(block.getCommentText(), ''); + }); + + test('Adding a non-empty comment can be undone', function () { + const block = this.workspace.newBlock('empty_block'); + block.setCommentText('hey there'); + assert.isNotNull(block.getIcon(Blockly.icons.IconType.COMMENT)); + assert.equal(block.getCommentText(), 'hey there'); + + this.workspace.undo(false); + + assert.isUndefined(block.getIcon(Blockly.icons.IconType.COMMENT)); + assert.isNull(block.getCommentText()); + }); + + test('Adding a non-empty comment can be redone', function () { + const block = this.workspace.newBlock('empty_block'); + block.setCommentText('hey there'); + this.workspace.undo(false); + this.workspace.undo(true); + + assert.isNotNull(block.getIcon(Blockly.icons.IconType.COMMENT)); + assert.equal(block.getCommentText(), 'hey there'); + }); + }); });