Files
blockly/tests/mocha/field_image_test.js
alschmiedt 5b1586ee1b test: update mocha tests to use goog_module (#5440)
* Use goog.module in mocha tests

* Fix compiler warnings

* Make test helpers a module

* Name test modules Blockly.test.*

This is to be more consistent with how non-test modules are named.

Also remove top-level goog.require of TestHelpers (now
Blockly.test.helpers) since requiring a side-effect-less module does
nothing.

* Convert block_test.js and comment_test.js to goog.module syntax

* Address PR comments

* Goog modulify tests

* Goog modulify toolbox helpers

* Fixes imports and moves common tests from workspace_test.js to a helper file.

* Update test deps after rebase

Co-authored-by: Christopher Allen <cpcallen+git@google.com>
2021-09-16 13:00:38 -07:00

200 lines
6.7 KiB
JavaScript

/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.module('Blockly.test.fieldImage');
const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers');
suite('Image Fields', function() {
setup(function() {
sharedTestSetup.call(this);
});
teardown(function() {
sharedTestTeardown.call(this);
});
/**
* Configuration for field tests with invalid values.
* @type {!Array<!FieldCreationTestCase>}
*/
var invalidValueTestCases = [
{title: 'Undefined Src', value: undefined, args: [undefined, 1, 1]},
{title: 'Undefined Size', value: 'src', args: ['src', undefined, undefined]},
{title: 'Zero Size', value: 'src', args: ['src', 0, 0]},
{title: 'Non-Parsable String for Size', value: 'src', args: ['src', 'bad', 'bad']},
];
/**
* Configuration for field tests with valid values.
* @type {!Array<!FieldCreationTestCase>}
*/
var validValueCreationTestCases = [
{title: 'With Alt', value: 'src', expectedValue: 'src',
args: ['src', 1, 1, 'alt'], expectedText: 'alt'},
{title: 'Without Alt', value: 'src', expectedValue: 'src',
args: ['src', 1, 1], expectedText: ''},
];
/**
* Adds json property to test cases based on args property.
* @param {!Array<!FieldCreationTestCase>} testCase The test case to modify.
*/
var addJson = function(testCase) {
testCase.json = {'src': testCase.args[0], 'width': testCase.args[1],
'height': testCase.args[2]};
if (testCase.args[3]) {
testCase.json['alt'] = testCase.args[3];
}
};
invalidValueTestCases.forEach(addJson);
validValueCreationTestCases.forEach(addJson);
/**
* Asserts that the field properties are correct based on the test case.
* @param {!Blockly.FieldImage} 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.FieldImage, validValueCreationTestCases, invalidValueTestCases,
validTestCaseAssertField);
testHelpers.runFromJsonSuiteTests(
Blockly.FieldImage, validValueCreationTestCases, invalidValueTestCases,
validTestCaseAssertField);
/**
* Configuration for field tests with valid values.
* @type {!Array<!FieldValueTestCase>}
*/
var validValueSetValueTestCases = [
{title: 'Good src', value: 'newSrc', expectedValue: 'newSrc',
expectedText: 'alt'},
];
suite('setValue', function() {
setup(function() {
this.field = new Blockly.FieldImage('src', 1, 1, 'alt');
});
testHelpers.runSetValueTests(
validValueSetValueTestCases, invalidValueTestCases, 'src', 'alt');
});
suite('Customizations', function() {
suite('On Click Handler', function() {
setup(function() {
this.onClick = function() {
console.log('on click');
};
});
test('JS Constructor', function() {
var field = new Blockly.FieldImage('src', 10, 10, null, this.onClick);
chai.assert.equal(field.clickHandler_, this.onClick);
});
test('setOnClickHandler', function() {
var field = new Blockly.FieldImage('src', 10, 10);
field.setOnClickHandler(this.onClick);
chai.assert.equal(field.clickHandler_, this.onClick);
});
test('Remove Click Handler', function() {
var field = new Blockly.FieldImage('src', 10, 10, null, this.onClick);
field.setOnClickHandler(null);
chai.assert.isNull(field.clickHandler_);
});
});
suite('Alt', function() {
test('JS Constructor', function() {
var field = new Blockly.FieldImage('src', 10, 10, 'alt');
chai.assert.equal(field.altText_, 'alt');
});
test('JSON Definition', function() {
var field = Blockly.FieldImage.fromJson({
src: 'src',
width: 10,
height: 10,
alt: 'alt'
});
chai.assert.equal(field.altText_, 'alt');
});
suite('SetAlt', function() {
setup(function() {
this.imageField = new Blockly.FieldImage('src', 10, 10, 'alt');
});
test('Null', function() {
this.imageField.setAlt(null);
testHelpers.assertFieldValue(this.imageField, 'src', '');
});
test('Empty String', function() {
this.imageField.setAlt('');
testHelpers.assertFieldValue(this.imageField, 'src', '');
});
test('Good Alt', function() {
this.imageField.setAlt('newAlt');
testHelpers.assertFieldValue(this.imageField, 'src', 'newAlt');
});
});
test('JS Configuration - Simple', function() {
var field = new Blockly.FieldImage('src', 10, 10, null, null, null, {
alt: 'alt'
});
chai.assert.equal(field.altText_, 'alt');
});
test('JS Configuration - Ignore', function() {
var field = new Blockly.FieldImage('src', 10, 10, 'alt', null, null, {
alt: 'configAlt'
});
chai.assert.equal(field.altText_, 'configAlt');
});
test('JS Configuration - Ignore - \'\'', function() {
var field = new Blockly.FieldImage('src', 10, 10, '', null, null, {
alt: 'configAlt'
});
chai.assert.equal(field.altText_, 'configAlt');
});
test('JS Configuration - Ignore - Config \'\'', function() {
var field = new Blockly.FieldImage('src', 10, 10, 'alt', null, null, {
alt: ''
});
chai.assert.equal(field.altText_, '');
});
});
suite('Flip RTL', function() {
test('JS Constructor', function() {
var field = new Blockly.FieldImage('src', 10, 10, null, null, true);
chai.assert.isTrue(field.getFlipRtl());
});
test('JSON Definition', function() {
var field = Blockly.FieldImage.fromJson({
src: 'src',
width: 10,
height: 10,
flipRtl: true
});
chai.assert.isTrue(field.getFlipRtl());
});
test('JS Configuration - Simple', function() {
var field = new Blockly.FieldImage('src', 10, 10, null, null, null, {
flipRtl: true
});
chai.assert.isTrue(field.getFlipRtl());
});
test('JS Configuration - Ignore - True', function() {
var field = new Blockly.FieldImage('src', 10, 10, null, null, true, {
flipRtl: false
});
chai.assert.isFalse(field.getFlipRtl());
});
test('JS Configuration - Ignore - False', function() {
var field = new Blockly.FieldImage('src', 10, 10, null, null, false, {
flipRtl: true
});
chai.assert.isTrue(field.getFlipRtl());
});
});
});
});