From e67e49ad9bca6a264a083670021511b79f48e19d Mon Sep 17 00:00:00 2001 From: Monica Kozbial Date: Fri, 2 Oct 2020 14:15:20 -0700 Subject: [PATCH] Update FieldDropdown to use dev-tools helpers (#4316) --- tests/mocha/field_dropdown_test.js | 229 ++++++++++++----------------- 1 file changed, 90 insertions(+), 139 deletions(-) diff --git a/tests/mocha/field_dropdown_test.js b/tests/mocha/field_dropdown_test.js index 3bd374141..5f08dfb55 100644 --- a/tests/mocha/field_dropdown_test.js +++ b/tests/mocha/field_dropdown_test.js @@ -7,159 +7,110 @@ suite('Dropdown Fields', function() { setup(function() { sharedTestSetup.call(this); + + // Invalid value test are expected to log errors. + const nativeConsoleError = console.error; + this.nativeConsoleError = nativeConsoleError; + console.error = function(msg) { + if (!msg.includes('Each FieldDropdown option')) { + nativeConsoleError.call(this, ...arguments); + } + }; }); teardown(function() { + console.error = this.nativeConsoleError; sharedTestTeardown.call(this); }); - function assertValue(dropdownField, expectedValue, expectedText) { - var actualValue = dropdownField.getValue(); - var actualText = dropdownField.getText(); - chai.assert.equal(actualValue, expectedValue); - chai.assert.equal(actualText, expectedText); - } - suite('Constructor', function() { - test('Empty', function() { - chai.assert.throws(function() { - new Blockly.FieldDropdown(); - }); - }); - test('Undefined', function() { - chai.assert.throws(function() { - new Blockly.FieldDropdown(undefined); - }); - }); - test('Array Items not Arrays', function() { - var stub = sinon.stub(console, 'error'); - chai.assert.throws(function() { - new Blockly.FieldDropdown([1, 2, 3]); - }); - sinon.assert.calledThrice(stub); - stub.restore(); - }); - test('Array Items with Invalid IDs', function() { - var stub = sinon.stub(console, 'error'); - chai.assert.throws(function() { - new Blockly.FieldDropdown([['1', 1], ['2', 2], ['3', 3]]); - }); - sinon.assert.calledThrice(stub); - stub.restore(); - }); - test('Array Items with Invalid Content', function() { - var stub = sinon.stub(console, 'error'); - chai.assert.throws(function() { - new Blockly.FieldDropdown([[1, '1'], [2, '2'], [3, '3']]); - }); - sinon.assert.calledThrice(stub); - stub.restore(); - }); - test('Text Dropdown', function() { - var dropdownField = new Blockly.FieldDropdown( - [['a', 'A'], ['b', 'B'], ['c', 'C']]); - assertValue(dropdownField, 'A', 'a'); - }); - test('Image Dropdown', function() { - var dropdownField = new Blockly.FieldDropdown([ + /** + * Configuration for field tests with invalid values. + * @type {!Array} + */ + var invalidValueCreationTestCases = [ + {title: 'Undefined', args: [undefined]}, + {title: 'Array Items not Arrays', args: [undefined]}, + {title: 'Array Items with Invalid IDs', + args: [[['1', 1], ['2', 2], ['3', 3]]]}, + {title: 'Array Items with Invalid Content', + args: [[[1, '1'], [2, '2'], [3, '3']]]}, + ]; + /** + * Configuration for field tests with valid values. + * @type {!Array} + */ + var validValueCreationTestCases = [ + {title: 'Text Dropdown', value: 'A', expectedValue: 'A', expectedText: 'a', + args: [[['a', 'A'], ['b', 'B'], ['c', 'C']]]}, + {title: 'Image Dropdown', value: 'A', expectedValue: 'A', expectedText: 'a', + args: [[ [{ src:'scrA', alt:'a' }, 'A'], [{ src:'scrB', alt:'b' }, 'B'], - [{ src:'scrC', alt:'c' }, 'C']]); - assertValue(dropdownField, 'A', 'a'); - }); - test('Dynamic Dropdown Text', function() { - var dynamicDropdownFunc = function() { + [{ src:'scrC', alt:'c' }, 'C']]]}, + {title: 'Dynamic Text Dropdown', value: 'A', expectedValue: 'A', expectedText: 'a', + args: [() => { return [['a', 'A'], ['b', 'B'], ['c', 'C']]; - }; - var dropdownField = new Blockly.FieldDropdown(dynamicDropdownFunc); - assertValue(dropdownField, 'A', 'a'); - }); - test('Dynamic Dropdown Image', function() { - var dynamicDropdownFunc = function() { + }]}, + {title: 'Dynamic Image Dropdown', value: 'A', expectedValue: 'A', expectedText: 'a', + args: [() => { return [ [{ src:'scrA', alt:'a' }, 'A'], [{ src:'scrB', alt:'b' }, 'B'], - [{ src:'scrC', alt:'c' }, 'C'] - ]; - }; - var dropdownField = new Blockly.FieldDropdown(dynamicDropdownFunc); - assertValue(dropdownField, 'A', 'a'); - }); - }); - suite('fromJson', function() { - test('Empty', function() { - chai.assert.throws(function() { - Blockly.FieldDropdown.fromJson({}); - }); - }); - test('Undefined', function() { - chai.assert.throws(function() { - Blockly.FieldDropdown.fromJson({ options: undefined }); - }); - }); - test('Array Items not Arrays', function() { - var stub = sinon.stub(console, 'error'); - chai.assert.throws(function() { - Blockly.FieldDropdown.fromJson({ options: [1, 2, 3] }); - }); - sinon.assert.calledThrice(stub); - stub.restore(); - }); - test('Array Items with Invalid IDs', function() { - var stub = sinon.stub(console, 'error'); - chai.assert.throws(function() { - Blockly.FieldDropdown.fromJson( - { options:[['1', 1], ['2', 2], ['3', 3]] }); - }); - sinon.assert.calledThrice(stub); - stub.restore(); - }); - test('Array Items with Invalid Content', function() { - var stub = sinon.stub(console, 'error'); - chai.assert.throws(function() { - Blockly.FieldDropdown.fromJson( - { options:[[1, '1'], [2, '2'], [3, '3']] }); - }); - sinon.assert.calledThrice(stub); - stub.restore(); - }); - test('Text Dropdown', function() { - var dropdownField = Blockly.FieldDropdown.fromJson( - { options:[['a', 'A'], ['b', 'B'], ['c', 'C']] }); - assertValue(dropdownField, 'A', 'a'); - }); - test('Image Dropdown', function() { - var dropdownField = Blockly.FieldDropdown.fromJson({ options:[ - [{ src:'scrA', alt:'a' }, 'A'], - [{ src:'scrB', alt:'b' }, 'B'], - [{ src:'scrC', alt:'c' }, 'C']] }); - assertValue(dropdownField, 'A', 'a'); - }); - }); + [{ src:'scrC', alt:'c' }, 'C']]; + }]}, + ]; + var addJson = function(testCase) { + testCase.json = {'options': testCase.args[0]}; + }; + invalidValueCreationTestCases.forEach(addJson); + validValueCreationTestCases.forEach(addJson); + + /** + * Asserts that the field properties are correct based on the test case. + * @param {!Blockly.FieldNumber} field The field to check. + * @param {!FieldValueTestCase} testCase The test case. + */ + var validTestCaseAssertField = function(field, testCase) { + testHelpers.assertFieldValue(field, testCase.expectedValue, testCase.expectedText); + }; + + testHelpers.runConstructorSuiteTests( + Blockly.FieldDropdown, validValueCreationTestCases, + invalidValueCreationTestCases, validTestCaseAssertField); + + testHelpers.runFromJsonSuiteTests( + Blockly.FieldDropdown, validValueCreationTestCases, + invalidValueCreationTestCases, validTestCaseAssertField); + + /** + * Configuration for field tests with invalid values. + * @type {!Array} + */ + var invalidValueSetValueTestCases = [ + {title: 'Null', value: null}, + {title: 'Undefined', value: undefined}, + {title: 'Invalid ID', value: 'bad'}, + ]; + /** + * Configuration for field tests with valid values. + * @type {!Array} + */ + var validValueSetValueTestCases = [ + {title: 'Valid ID', value: 'B', expectedValue: 'B', expectedText: 'b'}, + ]; + suite('setValue', function() { setup(function() { - this.dropdownField = new Blockly.FieldDropdown( + this.field = new Blockly.FieldDropdown( [['a', 'A'], ['b', 'B'], ['c', 'C']]); }); - test('Null', function() { - this.dropdownField.setValue(null); - assertValue(this.dropdownField, 'A', 'a'); - }); - test('Undefined', function() { - this.dropdownField.setValue(undefined); - assertValue(this.dropdownField, 'A', 'a'); - }); - test('Invalid ID', function() { - this.dropdownField.setValue('bad'); - assertValue(this.dropdownField, 'A', 'a'); - }); - test('Valid ID', function() { - this.dropdownField.setValue('B'); - assertValue(this.dropdownField, 'B', 'b'); - }); + testHelpers.runSetValueTests( + validValueSetValueTestCases, invalidValueSetValueTestCases, 'A', 'a'); test('With source block', function() { - this.dropdownField.setSourceBlock(createTestBlock()); - this.dropdownField.setValue('B'); - assertValue(this.dropdownField, 'B', 'b'); + this.field.setSourceBlock(createTestBlock()); + this.field.setValue('B'); + testHelpers.assertFieldValue(this.field, 'B', 'b'); }); }); + suite('Validators', function() { setup(function() { this.dropdownField = new Blockly.FieldDropdown([ @@ -177,7 +128,7 @@ suite('Dropdown Fields', function() { }); test('New Value', function() { this.dropdownField.setValue('1B'); - assertValue(this.dropdownField, '1A', '1a'); + testHelpers.assertFieldValue(this.dropdownField, '1A', '1a'); }); }); suite('Force 1s Validator', function() { @@ -188,7 +139,7 @@ suite('Dropdown Fields', function() { }); test('New Value', function() { this.dropdownField.setValue('2B'); - assertValue(this.dropdownField, '1B', '1b'); + testHelpers.assertFieldValue(this.dropdownField, '1B', '1b'); }); }); suite('Returns Undefined Validator', function() { @@ -197,7 +148,7 @@ suite('Dropdown Fields', function() { }); test('New Value', function() { this.dropdownField.setValue('1B'); - assertValue(this.dropdownField, '1B', '1b'); + testHelpers.assertFieldValue(this.dropdownField, '1B', '1b'); }); }); });