Files
blockly/tests/mocha/field_registry_test.js
Rachel Fenichel d11cc047a4 chore: fix more lint (#5700)
* chore: fix 918 violations of comma-dangle rule

* chore: fix 2 violations of comma-spacing

* chore: fix 13 violations of padded-blocks

* chore: fix 50 violations of block-spacing

* chore: fix one violation of semi-spacing

* chore: fix 4 violations of space-before-blocks

* chore: fix 38 violations of object-curly-spacing

* chore: fix 30 violations of key-spacing

* chore: fix 3 violations of quote-props

* chore: fix 5 violations of arrow-parens

* chore: fix 8 violations of no-tabs

* chore: allow uncommented helper functions in mocha tests

* chore: fix several more lint errors

* chore: tweak eslint configuration in core and tests

* chore: rebuild for tests
2021-11-10 10:18:36 -08:00

98 lines
2.9 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.TEST_ONLY.typeMap['field']['field_custom_test']) {
delete Blockly.registry.TEST_ONLY.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() {
const 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() {
const 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);
const json = {
type: 'field_custom_test',
value: 'ok',
};
const field = Blockly.fieldRegistry.fromJson(json);
chai.assert.isNotNull(field);
chai.assert.equal(field.getValue(), 'ok');
});
test('Not Registered', function() {
const json = {
type: 'field_custom_test',
value: 'ok',
};
const spy = sinon.stub(console, 'warn');
const 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);
const json = {
type: 'FIELD_CUSTOM_TEST',
value: 'ok',
};
const field = Blockly.fieldRegistry.fromJson(json);
chai.assert.isNotNull(field);
chai.assert.equal(field.getValue(), 'ok');
});
});
});