mirror of
https://github.com/google/blockly.git
synced 2026-01-31 04:30:11 +01:00
* refactor(interfaces): Use typeof ... === 'function' to test for methods
Testing for
'name' in object
or
obj.name !== undefined
only checks for the existence of the property (and in the latter
case that the property is not set to undefined). That's fine if
the interface specifies a property of indeterminate type, but in
the usual case that the interface member is a method we can do
one better and check to make sure the property's value is
callable.
* refactor(interfaces): Always check obj is not null/undefined
Since most type predicates take an argument of type any but then
check for the existence of certain properties, explicitly check
that the argument is not null or undefined (or check implicitly
by calling another type predicate that does so first, which
necessitates adding a few casts because tsc infers the type of
the argument too narrowly).
* fix(interfaces): Add missing check to hasBubble type predicate
This appears to have inadvertently been omitted in PR #9004.
* fix(interfaces): Fix misplaced typeof
* fix: Fix typos in JSDocs
* fix(tests): Make Mocks conform to corresponding interfaces
Introduce a new MockFocusable, and add methods to MockIcon,
MockBubbleIcon and MockComment, so that they fulfil the
IFocusableNode, IIcon, IHasBubble and ICommentIcon interfaces
respectively.
* chore(tests): Add assertions verifying mocks conform to predicates
Add (test) runtime assertions that:
- isFocusableNode(MockFocusable) returns true
- isIcon(MockIcon) returns true
- hasBubble(MockBubbleIcon) returns true
- isCommentIcon(MockCommentIcon) returns true
(The latter is currently failing because Blockly is undefined when
isCommentIcon calls the MockCommentIcon's getType method.)
* fix(tests): Don't rely on Blockly being set in Mock methods
For some reason the global Blockly binding is not visible at the
time when isCommentIcon calls MockCommentIcon's getType method,
and presumably this problem would apply to getBubbleSize too,
so directly import the required items.
* refactor(tests): Make MockCommentIcon a MockBubbleIcon
This slightly simplifies it and makes it less likely to accidentally
stop conforming to IHasBubble.
* fix(interfaces): Fix incorrect check in isSelectable
Fix an error which caused ISelectable instances to fail
isSelectable() checks, one of the results of which is that
Blockly.common.getSelected() would generally return null.
Whoops!
131 lines
2.3 KiB
JavaScript
131 lines
2.3 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2023 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
import {isFocusableNode} from '../../../build/src/core/interfaces/i_focusable_node.js';
|
|
import {hasBubble} from '../../../build/src/core/interfaces/i_has_bubble.js';
|
|
import {isIcon} from '../../../build/src/core/interfaces/i_icon.js';
|
|
import {isSerializable} from '../../../build/src/core/interfaces/i_serializable.js';
|
|
|
|
export class MockFocusable {
|
|
getFocusableElement() {}
|
|
getFocusableTree() {}
|
|
onNodeFocus() {}
|
|
onNodeBlur() {}
|
|
canBeFocused() {}
|
|
}
|
|
|
|
if (!isFocusableNode(new MockFocusable())) {
|
|
throw new TypeError('MockFocusable not an IFocuableNode');
|
|
}
|
|
|
|
export class MockIcon extends MockFocusable {
|
|
getType() {
|
|
return new Blockly.icons.IconType('mock icon');
|
|
}
|
|
|
|
initView() {}
|
|
|
|
dispose() {}
|
|
|
|
getWeight() {}
|
|
|
|
getSize() {
|
|
return new Blockly.utils.Size(0, 0);
|
|
}
|
|
|
|
applyColour() {}
|
|
|
|
hideForInsertionMarker() {}
|
|
|
|
updateEditable() {}
|
|
|
|
updateCollapsed() {}
|
|
|
|
isShownWhenCollapsed() {}
|
|
|
|
setOffsetInBlock() {}
|
|
|
|
onLocationChange() {}
|
|
|
|
onClick() {}
|
|
|
|
getFocusableElement() {
|
|
throw new Error('Unsupported operation in mock.');
|
|
}
|
|
|
|
getFocusableTree() {
|
|
throw new Error('Unsupported operation in mock.');
|
|
}
|
|
|
|
onNodeFocus() {}
|
|
|
|
onNodeBlur() {}
|
|
|
|
canBeFocused() {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (!isIcon(new MockIcon())) {
|
|
throw new TypeError('MockIcon not an IIcon');
|
|
}
|
|
|
|
export class MockSerializableIcon extends MockIcon {
|
|
constructor() {
|
|
super();
|
|
this.state = '';
|
|
}
|
|
|
|
getType() {
|
|
return new Blockly.icons.IconType('serializable icon');
|
|
}
|
|
|
|
getWeight() {
|
|
return 1;
|
|
}
|
|
|
|
saveState() {
|
|
return 'some state';
|
|
}
|
|
|
|
loadState(state) {
|
|
this.state = state;
|
|
}
|
|
}
|
|
|
|
if (!isSerializable(new MockSerializableIcon())) {
|
|
throw new TypeError('MockSerializableIcon not an ISerializable');
|
|
}
|
|
|
|
export class MockBubbleIcon extends MockIcon {
|
|
constructor() {
|
|
super();
|
|
this.visible = false;
|
|
}
|
|
|
|
getType() {
|
|
return new Blockly.icons.IconType('bubble icon');
|
|
}
|
|
|
|
updateCollapsed() {}
|
|
|
|
bubbleIsVisible() {
|
|
return this.visible;
|
|
}
|
|
|
|
setBubbleVisible(visible) {
|
|
this.visible = visible;
|
|
}
|
|
|
|
getBubble() {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
if (!hasBubble(new MockBubbleIcon())) {
|
|
throw new TypeError('MockBubbleIcon not an IHasBubble');
|
|
}
|