Added tests/setFieldValue error message

* Added tests for getting/setting field (values) when names are not supplied and test for getting a field value, setting it to a new value, and getting it again.
* Added more user-friendly error message for setFieldValue telling the developer that he/she is missing the name rather than Field "undefined" not found.
This commit is contained in:
jschanker
2021-06-12 03:16:12 -04:00
committed by Christopher Allen
parent ee8e282360
commit f9c5260fb0
2 changed files with 29 additions and 0 deletions

View File

@@ -1127,6 +1127,10 @@ Blockly.Block.prototype.getFieldValue = function(name) {
* @param {!string} name The name of the field to set the value of.
*/
Blockly.Block.prototype.setFieldValue = function(newValue, name) {
if (typeof name === "undefined") {
throw Error("Call to Blockly.Block.prototype.setFieldValue without " +
"required second argument of field name.");
}
var field = this.getField(name);
if (!field) {
throw Error('Field "' + name + '" not found.');

View File

@@ -1104,6 +1104,31 @@ suite('Blocks', function() {
});
});
});
suite('Getting/Setting Field (Values)', function() {
setup(function() {
this.block = Blockly.Xml.domToBlock(Blockly.Xml.textToDom(
'<block type="text"><field name = "TEXT">test</field></block>'
), this.workspace);
});
test('Getting Field', function() {
chai.assert.instanceOf(this.block.getField("TEXT"), Blockly.Field);
});
test('Getting Field without Name', function() {
chai.assert.isNull(this.block.getField());
});
test('Getting Value of Field without Name', function() {
chai.assert.isNull(this.block.getFieldValue());
});
test('Getting/Setting Field Value', function() {
chai.assert.equal(this.block.getFieldValue("TEXT"), "test");
this.block.setFieldValue("abc", "TEXT");
chai.assert.equal(this.block.getFieldValue("TEXT"), "abc");
});
test('Setting Field without Name', function() {
chai.assert.throws(this.block.setFieldValue.bind(this.block, 'test'));
});
});
suite('Icon Management', function() {
suite('Bubbles and Collapsing', function() {
setup(function() {