Merge pull request #2302 from ErikMejerHansen/develop

Fix custom field types
This commit is contained in:
Karishma Chadha 2019-12-27 11:44:00 -05:00 committed by GitHub
commit 8faad67439
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 2 deletions

View file

@ -981,8 +981,10 @@ class Runtime extends EventEmitter {
fieldName: fieldName,
extendedName: extendedName,
argumentTypeInfo: {
shadowType: extendedName,
fieldType: `field_${extendedName}`
shadow: {
type: extendedName,
fieldName: `field_${extendedName}`
}
},
scratchBlocksDefinition: this._buildCustomFieldTypeForScratchBlocks(
extendedName,

View file

@ -82,6 +82,47 @@ const testExtensionInfo = {
]
};
const extensionInfoWithCustomFieldTypes = {
id: 'test_custom_fieldType',
name: 'fake test extension with customFieldTypes',
color1: '#111111',
color2: '#222222',
color3: '#333333',
blocks: [
{ // Block that uses custom field types
opcode: 'motorTurnFor',
blockType: BlockType.COMMAND,
text: '[PORT] run [DIRECTION] for [VALUE] [UNIT]',
arguments: {
PORT: {
defaultValue: 'A',
type: 'single-port-selector'
},
DIRECTION: {
defaultValue: 'clockwise',
type: 'custom-direction'
}
}
}
],
customFieldTypes: {
'single-port-selector': {
output: 'string',
outputShape: 2,
implementation: {
fromJson: () => null
}
},
'custom-direction': {
output: 'string',
outputShape: 3,
implementation: {
fromJson: () => null
}
}
}
};
const testCategoryInfo = function (t, block) {
t.equal(block.json.category, 'fake test extension');
t.equal(block.json.colour, '#111111');
@ -258,3 +299,29 @@ test('registerExtensionPrimitives', t => {
runtime._registerExtensionPrimitives(testExtensionInfo);
});
test('custom field types should be added to block and EXTENSION_FIELD_ADDED callback triggered', t => {
const runtime = new Runtime();
runtime.on(Runtime.EXTENSION_ADDED, categoryInfo => {
const blockInfo = categoryInfo.blocks[0];
// We expect that for each argument there's a corresponding <field>-tag in the block XML
Object.values(blockInfo.info.arguments).forEach(argument => {
const regex = new RegExp(`<field name="field_${categoryInfo.id}_${argument.type}">`);
t.true(regex.test(blockInfo.xml));
});
});
let fieldAddedCallbacks = 0;
runtime.on(Runtime.EXTENSION_FIELD_ADDED, () => {
fieldAddedCallbacks++;
});
runtime._registerExtensionPrimitives(extensionInfoWithCustomFieldTypes);
// Extension includes two custom field types
t.equal(fieldAddedCallbacks, 2);
t.end();
});