Load extensions before sharing blocks.

This commit fixes the unit tests so the assertions are made after the promise resolves
This commit is contained in:
Paul Kaplan 2018-11-05 13:00:35 -05:00
parent bc1da9fa44
commit c8ad1955d4
2 changed files with 90 additions and 74 deletions

View file

@ -1107,6 +1107,7 @@ class VirtualMachine extends EventEmitter {
* @param {!string} targetId Id of target to add blocks to.
* @param {?string} optFromTargetId Optional target id indicating that blocks are being
* shared from that target. This is needed for resolving any potential variable conflicts.
* @return {!Promise} Promise that resolves when the extensions and blocks have been added.
*/
shareBlocksToTarget (blocks, targetId, optFromTargetId) {
const copiedBlocks = JSON.parse(JSON.stringify(blocks));
@ -1119,10 +1120,24 @@ class VirtualMachine extends EventEmitter {
fromTarget.resolveVariableSharingConflictsWithTarget(copiedBlocks, target);
}
for (let i = 0; i < copiedBlocks.length; i++) {
target.blocks.createBlock(copiedBlocks[i]);
}
// Create a unique set of extensionIds that are not yet loaded
const extensionIDs = new Set(copiedBlocks
.map(b => sb3.getExtensionIdForOpcode(b.opcode))
.filter(id => !!id) // Remove ids that do not exist
.filter(id => !this.extensionManager.isExtensionLoaded(id)) // and remove loaded extensions
);
// Create an array promises for extensions to load
const extensionPromises = Array.from(extensionIDs,
id => this.extensionManager.loadExtensionURL(id)
);
return Promise.all(extensionPromises).then(() => {
copiedBlocks.forEach(block => {
target.blocks.createBlock(block);
});
target.blocks.updateTargetSpecificBlocks(target.isStage);
});
}
/**

View file

@ -744,7 +744,7 @@ test('shareBlocksToTarget shares global variables without any name changes', t =
t.type(stage.blocks.getBlock('a block'), 'undefined');
// Share the block to the stage
vm.shareBlocksToTarget([target.blocks.getBlock('a block')], stage.id, target.id);
vm.shareBlocksToTarget([target.blocks.getBlock('a block')], stage.id, target.id).then(() => {
// Verify that the block now exists on the target as well as the stage
t.type(target.blocks.getBlock('a block'), 'object');
@ -767,6 +767,7 @@ test('shareBlocksToTarget shares global variables without any name changes', t =
t.end();
});
});
test('shareBlocksToTarget shares a local variable to the stage, creating a global variable with a new name', t => {
const vm = new VirtualMachine();
@ -803,8 +804,7 @@ test('shareBlocksToTarget shares a local variable to the stage, creating a globa
t.type(stage.blocks.getBlock('a block'), 'undefined');
// Share the block to the stage
vm.shareBlocksToTarget([target.blocks.getBlock('a block')], stage.id, target.id);
vm.shareBlocksToTarget([target.blocks.getBlock('a block')], stage.id, target.id).then(() => {
// Verify that the block still exists on the target and remains unchanged
t.type(target.blocks.getBlock('a block'), 'object');
t.type(target.blocks.getBlock('a block').fields, 'object');
@ -834,6 +834,7 @@ test('shareBlocksToTarget shares a local variable to the stage, creating a globa
t.end();
});
});
test('shareBlocksToTarget chooses a fresh name for a new global variable checking for conflicts on all sprites', t => {
const vm = new VirtualMachine();
@ -877,8 +878,7 @@ test('shareBlocksToTarget chooses a fresh name for a new global variable checkin
otherTarget.createVariable('a different var', 'Stage: a mock variable', Variable.SCALAR_TYPE);
// Share the block to the stage
vm.shareBlocksToTarget([target.blocks.getBlock('a block')], stage.id, target.id);
vm.shareBlocksToTarget([target.blocks.getBlock('a block')], stage.id, target.id).then(() => {
// Verify that the block still exists on the target and remains unchanged
t.type(target.blocks.getBlock('a block'), 'object');
t.type(target.blocks.getBlock('a block').fields, 'object');
@ -908,6 +908,7 @@ test('shareBlocksToTarget chooses a fresh name for a new global variable checkin
t.end();
});
});
test('Setting turbo mode emits events', t => {
let turboMode = null;