mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-10 15:02:06 -05:00
Add and update unit tests for new create cloud variable functionality.
This commit is contained in:
parent
3fc1b89b30
commit
fad4d381bb
2 changed files with 119 additions and 6 deletions
|
@ -4,6 +4,7 @@ const Variable = require('../../src/engine/variable');
|
||||||
const adapter = require('../../src/engine/adapter');
|
const adapter = require('../../src/engine/adapter');
|
||||||
const Runtime = require('../../src/engine/runtime');
|
const Runtime = require('../../src/engine/runtime');
|
||||||
const events = require('../fixtures/events.json');
|
const events = require('../fixtures/events.json');
|
||||||
|
const Cloud = require('../../src/io/cloud');
|
||||||
|
|
||||||
test('spec', t => {
|
test('spec', t => {
|
||||||
const target = new Target();
|
const target = new Target();
|
||||||
|
@ -71,6 +72,58 @@ test('createListVariable creates a list', t => {
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('createVariable calls cloud io device\'s requestCreateCloudVariable', t => {
|
||||||
|
const runtime = new Runtime();
|
||||||
|
// Mock the requestCreateCloudVariable function
|
||||||
|
let requestCreateCloudWasCalled = false;
|
||||||
|
runtime.ioDevices.cloud.requestCreateCloudVariable = () => {
|
||||||
|
requestCreateCloudWasCalled = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
const target = new Target(runtime);
|
||||||
|
target.isStage = true;
|
||||||
|
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE, true /* isCloud */);
|
||||||
|
|
||||||
|
const variables = target.variables;
|
||||||
|
t.equal(Object.keys(variables).length, 1);
|
||||||
|
const variable = variables[Object.keys(variables)[0]];
|
||||||
|
t.equal(variable.id, 'foo');
|
||||||
|
t.equal(variable.name, 'bar');
|
||||||
|
t.equal(variable.type, Variable.SCALAR_TYPE);
|
||||||
|
t.equal(variable.value, 0);
|
||||||
|
// isCloud flag doesn't get set by the target createVariable function
|
||||||
|
t.equal(variable.isCloud, false);
|
||||||
|
t.equal(requestCreateCloudWasCalled, true);
|
||||||
|
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('createVariable does not call cloud io device\'s requestCreateCloudVariable if target is not stage', t => {
|
||||||
|
const runtime = new Runtime();
|
||||||
|
// Mock the requestCreateCloudVariable function
|
||||||
|
let requestCreateCloudWasCalled = false;
|
||||||
|
runtime.ioDevices.cloud.requestCreateCloudVariable = () => {
|
||||||
|
requestCreateCloudWasCalled = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
const target = new Target(runtime);
|
||||||
|
target.isStage = false;
|
||||||
|
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE, true /* isCloud */);
|
||||||
|
|
||||||
|
const variables = target.variables;
|
||||||
|
t.equal(Object.keys(variables).length, 1);
|
||||||
|
const variable = variables[Object.keys(variables)[0]];
|
||||||
|
t.equal(variable.id, 'foo');
|
||||||
|
t.equal(variable.name, 'bar');
|
||||||
|
t.equal(variable.type, Variable.SCALAR_TYPE);
|
||||||
|
t.equal(variable.value, 0);
|
||||||
|
// isCloud flag doesn't get set by the target createVariable function
|
||||||
|
t.equal(variable.isCloud, false);
|
||||||
|
t.equal(requestCreateCloudWasCalled, false);
|
||||||
|
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
test('createVariable throws when given invalid type', t => {
|
test('createVariable throws when given invalid type', t => {
|
||||||
const target = new Target();
|
const target = new Target();
|
||||||
t.throws(
|
t.throws(
|
||||||
|
|
|
@ -2,14 +2,18 @@ const test = require('tap').test;
|
||||||
const Cloud = require('../../src/io/cloud');
|
const Cloud = require('../../src/io/cloud');
|
||||||
const Target = require('../../src/engine/target');
|
const Target = require('../../src/engine/target');
|
||||||
const Variable = require('../../src/engine/variable');
|
const Variable = require('../../src/engine/variable');
|
||||||
|
const Runtime = require('../../src/engine/runtime');
|
||||||
|
|
||||||
test('spec', t => {
|
test('spec', t => {
|
||||||
const cloud = new Cloud();
|
const runtime = new Runtime();
|
||||||
|
const cloud = new Cloud(runtime);
|
||||||
|
|
||||||
t.type(cloud, 'object');
|
t.type(cloud, 'object');
|
||||||
t.type(cloud.postData, 'function');
|
t.type(cloud.postData, 'function');
|
||||||
t.type(cloud.requestUpdateVariable, 'function');
|
t.type(cloud.requestUpdateVariable, 'function');
|
||||||
t.type(cloud.updateCloudVariable, 'function');
|
t.type(cloud.updateCloudVariable, 'function');
|
||||||
|
t.type(cloud.requestCreateCloudVariable, 'function');
|
||||||
|
t.type(cloud.createCloudVariable, 'function');
|
||||||
t.type(cloud.setProvider, 'function');
|
t.type(cloud.setProvider, 'function');
|
||||||
t.type(cloud.setStage, 'function');
|
t.type(cloud.setStage, 'function');
|
||||||
t.type(cloud.clear, 'function');
|
t.type(cloud.clear, 'function');
|
||||||
|
@ -17,7 +21,8 @@ test('spec', t => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('stage and provider are null initially', t => {
|
test('stage and provider are null initially', t => {
|
||||||
const cloud = new Cloud();
|
const runtime = new Runtime();
|
||||||
|
const cloud = new Cloud(runtime);
|
||||||
|
|
||||||
t.strictEquals(cloud.provider, null);
|
t.strictEquals(cloud.provider, null);
|
||||||
t.strictEquals(cloud.stage, null);
|
t.strictEquals(cloud.stage, null);
|
||||||
|
@ -25,7 +30,8 @@ test('stage and provider are null initially', t => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('setProvider sets the provider', t => {
|
test('setProvider sets the provider', t => {
|
||||||
const cloud = new Cloud();
|
const runtime = new Runtime();
|
||||||
|
const cloud = new Cloud(runtime);
|
||||||
|
|
||||||
const provider = {
|
const provider = {
|
||||||
foo: 'a fake provider'
|
foo: 'a fake provider'
|
||||||
|
@ -37,7 +43,7 @@ test('setProvider sets the provider', t => {
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('postData updates the variable', t => {
|
test('postData update message updates the variable', t => {
|
||||||
const stage = new Target();
|
const stage = new Target();
|
||||||
const fooVar = new Variable(
|
const fooVar = new Variable(
|
||||||
'a fake var id',
|
'a fake var id',
|
||||||
|
@ -49,7 +55,8 @@ test('postData updates the variable', t => {
|
||||||
|
|
||||||
t.strictEquals(fooVar.value, 0);
|
t.strictEquals(fooVar.value, 0);
|
||||||
|
|
||||||
const cloud = new Cloud();
|
const runtime = new Runtime();
|
||||||
|
const cloud = new Cloud(runtime);
|
||||||
cloud.setStage(stage);
|
cloud.setStage(stage);
|
||||||
cloud.postData({varUpdate: {
|
cloud.postData({varUpdate: {
|
||||||
name: 'foo',
|
name: 'foo',
|
||||||
|
@ -59,6 +66,30 @@ test('postData updates the variable', t => {
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('postData create message sets isCloud flag on the variable and updates runtime cloud limit', t => {
|
||||||
|
const stage = new Target();
|
||||||
|
const fooVar = new Variable(
|
||||||
|
'a fake var id',
|
||||||
|
'foo',
|
||||||
|
Variable.SCALAR_TYPE,
|
||||||
|
false /* isCloud */
|
||||||
|
);
|
||||||
|
stage.variables[fooVar.id] = fooVar;
|
||||||
|
|
||||||
|
t.strictEquals(fooVar.value, 0);
|
||||||
|
t.strictEquals(fooVar.isCloud, false);
|
||||||
|
|
||||||
|
const runtime = new Runtime();
|
||||||
|
const cloud = new Cloud(runtime);
|
||||||
|
cloud.setStage(stage);
|
||||||
|
cloud.postData({varCreate: {
|
||||||
|
name: 'foo'
|
||||||
|
}});
|
||||||
|
t.strictEquals(fooVar.isCloud, true);
|
||||||
|
t.strictEquals(runtime.hasCloudData(), true);
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
test('requestUpdateVariable calls provider\'s updateVariable function', t => {
|
test('requestUpdateVariable calls provider\'s updateVariable function', t => {
|
||||||
let updateVariableCalled = false;
|
let updateVariableCalled = false;
|
||||||
let mockVarName = '';
|
let mockVarName = '';
|
||||||
|
@ -74,7 +105,8 @@ test('requestUpdateVariable calls provider\'s updateVariable function', t => {
|
||||||
updateVariable: mockUpdateVariable
|
updateVariable: mockUpdateVariable
|
||||||
};
|
};
|
||||||
|
|
||||||
const cloud = new Cloud();
|
const runtime = new Runtime();
|
||||||
|
const cloud = new Cloud(runtime);
|
||||||
cloud.setProvider(provider);
|
cloud.setProvider(provider);
|
||||||
cloud.requestUpdateVariable('foo', 3);
|
cloud.requestUpdateVariable('foo', 3);
|
||||||
t.equals(updateVariableCalled, true);
|
t.equals(updateVariableCalled, true);
|
||||||
|
@ -82,3 +114,31 @@ test('requestUpdateVariable calls provider\'s updateVariable function', t => {
|
||||||
t.strictEquals(mockVarValue, 3);
|
t.strictEquals(mockVarValue, 3);
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('requestCreateCloudVariable calls provider\'s createVariable function', t => {
|
||||||
|
let createVariableCalled = false;
|
||||||
|
const mockVariable = new Variable('a var id', 'my var', Variable.SCALAR_TYPE, false);
|
||||||
|
let mockVarName;
|
||||||
|
let mockVarValue;
|
||||||
|
const mockCreateVariable = (name, value) => {
|
||||||
|
createVariableCalled = true;
|
||||||
|
mockVarName = name;
|
||||||
|
mockVarValue = value;
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
const provider = {
|
||||||
|
createVariable: mockCreateVariable
|
||||||
|
};
|
||||||
|
|
||||||
|
const runtime = new Runtime();
|
||||||
|
const cloud = new Cloud(runtime);
|
||||||
|
cloud.setProvider(provider);
|
||||||
|
cloud.requestCreateCloudVariable(mockVariable);
|
||||||
|
t.equals(createVariableCalled, true);
|
||||||
|
t.strictEquals(mockVarName, 'my var');
|
||||||
|
t.strictEquals(mockVarValue, 0);
|
||||||
|
// Calling requestCreateCloudVariable does not set isCloud flag on variable
|
||||||
|
t.strictEquals(mockVariable.isCloud, false);
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue