mirror of
https://github.com/google/blockly.git
synced 2026-01-07 09:00:11 +01:00
feat: allow overriding comment icons (#7937)
* feat: add comment icon interface * feat: have blocks construct comment icons from registry * chore: add tests for setCommentText * fix: typeguard
This commit is contained in:
@@ -19,6 +19,7 @@ import {
|
||||
createMockEvent,
|
||||
} from './test_helpers/events.js';
|
||||
import {MockIcon, MockBubbleIcon} from './test_helpers/icon_mocks.js';
|
||||
import {IconType} from '../../build/src/core/icons/icon_types.js';
|
||||
|
||||
suite('Blocks', function () {
|
||||
setup(function () {
|
||||
@@ -1367,6 +1368,93 @@ suite('Blocks', function () {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('Constructing registered comment classes', function () {
|
||||
class MockComment extends MockIcon {
|
||||
getType() {
|
||||
return Blockly.icons.IconType.COMMENT;
|
||||
}
|
||||
|
||||
setText() {}
|
||||
|
||||
getText() {
|
||||
return '';
|
||||
}
|
||||
|
||||
setBubbleSize() {}
|
||||
|
||||
getBubbleSize() {
|
||||
return Blockly.utils.Size(0, 0);
|
||||
}
|
||||
|
||||
bubbleIsVisible() {
|
||||
return true;
|
||||
}
|
||||
|
||||
setBubbleVisible() {}
|
||||
}
|
||||
|
||||
setup(function () {
|
||||
this.workspace = Blockly.inject('blocklyDiv', {});
|
||||
|
||||
this.block = this.workspace.newBlock('stack_block');
|
||||
this.block.initSvg();
|
||||
this.block.render();
|
||||
});
|
||||
|
||||
teardown(function () {
|
||||
workspaceTeardown.call(this, this.workspace);
|
||||
|
||||
Blockly.icons.registry.unregister(
|
||||
Blockly.icons.IconType.COMMENT.toString(),
|
||||
);
|
||||
Blockly.icons.registry.register(
|
||||
Blockly.icons.IconType.COMMENT,
|
||||
Blockly.icons.CommentIcon,
|
||||
);
|
||||
});
|
||||
|
||||
test('setCommentText constructs the registered comment icon', function () {
|
||||
Blockly.icons.registry.unregister(
|
||||
Blockly.icons.IconType.COMMENT.toString(),
|
||||
);
|
||||
Blockly.icons.registry.register(
|
||||
Blockly.icons.IconType.COMMENT,
|
||||
MockComment,
|
||||
);
|
||||
|
||||
this.block.setCommentText('test text');
|
||||
|
||||
chai.assert.instanceOf(
|
||||
this.block.getIcon(Blockly.icons.IconType.COMMENT),
|
||||
MockComment,
|
||||
);
|
||||
});
|
||||
|
||||
test('setCommentText throws if no icon is registered', function () {
|
||||
Blockly.icons.registry.unregister(
|
||||
Blockly.icons.IconType.COMMENT.toString(),
|
||||
);
|
||||
|
||||
chai.assert.throws(() => {
|
||||
this.block.setCommentText('test text');
|
||||
}, 'No comment icon class is registered, so a comment cannot be set');
|
||||
});
|
||||
|
||||
test('setCommentText throws if the icon is not an ICommentIcon', function () {
|
||||
Blockly.icons.registry.unregister(
|
||||
Blockly.icons.IconType.COMMENT.toString(),
|
||||
);
|
||||
Blockly.icons.registry.register(
|
||||
Blockly.icons.IconType.COMMENT,
|
||||
MockIcon,
|
||||
);
|
||||
|
||||
chai.assert.throws(() => {
|
||||
this.block.setCommentText('test text');
|
||||
}, 'The class registered as a comment icon does not conform to the ICommentIcon interface');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('Getting/Setting Field (Values)', function () {
|
||||
|
||||
Reference in New Issue
Block a user