From f9c5260fb0d54c45fef19fdc2de8896bf11b7f43 Mon Sep 17 00:00:00 2001 From: jschanker Date: Sat, 12 Jun 2021 03:16:12 -0400 Subject: [PATCH] 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. --- core/block.js | 4 ++++ tests/mocha/block_test.js | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/core/block.js b/core/block.js index 8f574498b..94ccbeb9c 100644 --- a/core/block.js +++ b/core/block.js @@ -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.'); diff --git a/tests/mocha/block_test.js b/tests/mocha/block_test.js index 7995902ce..e9a2b9fbf 100644 --- a/tests/mocha/block_test.js +++ b/tests/mocha/block_test.js @@ -1104,6 +1104,31 @@ suite('Blocks', function() { }); }); }); + suite('Getting/Setting Field (Values)', function() { + setup(function() { + this.block = Blockly.Xml.domToBlock(Blockly.Xml.textToDom( + 'test' + ), 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() {