scratch-vm/test/integration/internal-extension.js

75 lines
2.2 KiB
JavaScript
Raw Normal View History

const test = require('tap').test;
const Worker = require('tiny-worker');
const dispatch = require('../../src/dispatch/central-dispatch');
const VirtualMachine = require('../../src/virtual-machine');
// By default Central Dispatch works with the Worker class built into the browser. Tell it to use TinyWorker instead.
dispatch.workerClass = Worker;
class TestInternalExtension {
constructor () {
this.status = {};
this.status.constructorCalled = true;
}
getInfo () {
this.status.getInfoCalled = true;
return {
2018-04-20 10:29:29 -04:00
id: 'testInternalExtension',
name: 'Test Internal Extension',
blocks: [
{
opcode: 'go'
}
],
menus: {
simpleMenu: this._buildAMenu(),
2018-02-16 13:49:26 -05:00
dynamicMenu: '_buildDynamicMenu'
}
};
}
go () {
this.status.goCalled = true;
}
2018-02-16 13:49:26 -05:00
_buildAMenu () {
this.status.buildMenuCalled = true;
return ['abcd', 'efgh', 'ijkl'];
}
2018-02-16 13:49:26 -05:00
_buildDynamicMenu () {
this.status.buildDynamicMenuCalled = true;
return [1, 2, 3, 4, 6];
}
}
test('internal extension', t => {
const vm = new VirtualMachine();
const extension = new TestInternalExtension();
t.ok(extension.status.constructorCalled);
t.notOk(extension.status.getInfoCalled);
return vm.extensionManager._registerInternalExtension(extension).then(() => {
t.ok(extension.status.getInfoCalled);
2018-04-20 10:29:29 -04:00
const func = vm.runtime.getOpcodeFunction('testInternalExtension_go');
t.type(func, 'function');
t.notOk(extension.status.goCalled);
func();
t.ok(extension.status.goCalled);
// There should be 2 menus - one is an array, one is the function to call.
t.equal(vm.runtime._blockInfo[0].menus.length, 2);
// First menu has 3 items.
t.equal(
vm.runtime._blockInfo[0].menus[0].json.args0[0].options.length, 3);
// Second menu is a dynamic menu and therefore should be a function.
t.type(
vm.runtime._blockInfo[0].menus[1].json.args0[0].options, 'function');
});
});