diff --git a/core/events/events_block_change.ts b/core/events/events_block_change.ts index e71eabb1747..d4a8ba9d2e6 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 1f52df8fd52..a7b2635b99f 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'); + }); + }); });