mirror of
https://github.com/google/blockly.git
synced 2026-01-08 17:40:09 +01:00
* 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>
104 lines
3.2 KiB
JavaScript
104 lines
3.2 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2019 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
goog.module('Blockly.test.fieldRegistry');
|
|
|
|
const {createDeprecationWarningStub, sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers');
|
|
|
|
|
|
suite('Field Registry', function() {
|
|
function CustomFieldType(value) {
|
|
CustomFieldType.superClass_.constructor.call(this, value);
|
|
}
|
|
Blockly.utils.object.inherits(CustomFieldType, Blockly.Field);
|
|
CustomFieldType.fromJson = function(options) {
|
|
return new CustomFieldType(options['value']);
|
|
};
|
|
|
|
setup(function() {
|
|
sharedTestSetup.call(this);
|
|
});
|
|
teardown(function() {
|
|
sharedTestTeardown.call(this);
|
|
if (Blockly.registry.typeMap_['field']['field_custom_test']) {
|
|
delete Blockly.registry.typeMap_['field']['field_custom_test'];
|
|
}
|
|
});
|
|
|
|
suite('Registration', function() {
|
|
test('Simple', function() {
|
|
Blockly.fieldRegistry.register('field_custom_test', CustomFieldType);
|
|
});
|
|
test('fromJson as Key', function() {
|
|
chai.assert.throws(function() {
|
|
Blockly.fieldRegistry.register(CustomFieldType.fromJson, '');
|
|
}, 'Invalid name');
|
|
});
|
|
test('No fromJson', function() {
|
|
var fromJson = CustomFieldType.fromJson;
|
|
delete CustomFieldType.fromJson;
|
|
chai.assert.throws(function() {
|
|
Blockly.fieldRegistry.register('field_custom_test', CustomFieldType);
|
|
}, 'must have a fromJson function');
|
|
CustomFieldType.fromJson = fromJson;
|
|
});
|
|
test('fromJson not a function', function() {
|
|
var fromJson = CustomFieldType.fromJson;
|
|
CustomFieldType.fromJson = true;
|
|
chai.assert.throws(function() {
|
|
Blockly.fieldRegistry.register('field_custom_test', CustomFieldType);
|
|
}, 'must have a fromJson function');
|
|
CustomFieldType.fromJson = fromJson;
|
|
});
|
|
});
|
|
suite('Retrieval', function() {
|
|
test('Simple', function() {
|
|
Blockly.fieldRegistry.register('field_custom_test', CustomFieldType);
|
|
|
|
var json = {
|
|
type: 'field_custom_test',
|
|
value: 'ok'
|
|
};
|
|
|
|
// TODO(#4197): Remove stubbing of deprecation warning after fixing.
|
|
var deprecationWarnStub = createDeprecationWarningStub();
|
|
var field = Blockly.fieldRegistry.fromJson(json);
|
|
deprecationWarnStub.restore();
|
|
|
|
chai.assert.isNotNull(field);
|
|
chai.assert.equal(field.getValue(), 'ok');
|
|
});
|
|
test('Not Registered', function() {
|
|
var json = {
|
|
type: 'field_custom_test',
|
|
value: 'ok'
|
|
};
|
|
|
|
var spy = sinon.stub(console, 'warn');
|
|
var field = Blockly.fieldRegistry.fromJson(json);
|
|
chai.assert.isNull(field);
|
|
chai.assert.isTrue(spy.called);
|
|
spy.restore();
|
|
});
|
|
test('Case Different', function() {
|
|
Blockly.fieldRegistry.register('field_custom_test', CustomFieldType);
|
|
|
|
var json = {
|
|
type: 'FIELD_CUSTOM_TEST',
|
|
value: 'ok'
|
|
};
|
|
|
|
// TODO(#4197): Remove stubbing of deprecation warning after fixing.
|
|
var deprecationWarnStub = createDeprecationWarningStub();
|
|
var field = Blockly.fieldRegistry.fromJson(json);
|
|
deprecationWarnStub.restore();
|
|
|
|
chai.assert.isNotNull(field);
|
|
chai.assert.equal(field.getValue(), 'ok');
|
|
});
|
|
});
|
|
});
|