Improved test coverage of deleteSprite() in src/virtual-machine. Fixed bug where null check was happening after looking up property of given target.

This commit is contained in:
Karishma Chadha 2017-11-06 17:41:57 -05:00
parent a735459427
commit cb31f206a0
2 changed files with 48 additions and 1 deletions

View file

@ -474,9 +474,12 @@ class VirtualMachine extends EventEmitter {
*/ */
deleteSprite (targetId) { deleteSprite (targetId) {
const target = this.runtime.getTargetById(targetId); const target = this.runtime.getTargetById(targetId);
const targetIndexBeforeDelete = this.runtime.targets.map(t => t.id).indexOf(target.id); // const targetIndexBeforeDelete = this.runtime.targets.map(t => t.id).indexOf(target.id);
// can't call target.id if target doesn't exist.
// moving below
if (target) { if (target) {
const targetIndexBeforeDelete = this.runtime.targets.map(t => t.id).indexOf(target.id);
if (!target.isSprite()) { if (!target.isSprite()) {
throw new Error('Cannot delete non-sprite targets.'); throw new Error('Cannot delete non-sprite targets.');
} }

View file

@ -110,6 +110,50 @@ test('renameSprite does not increment when renaming to the same name', t => {
t.end(); t.end();
}); });
test('deleteSprite throws when used on a non-sprite target', t => {
const vm = new VirtualMachine();
vm.runtime.targets = [{
id: 'id',
isSprite: () => false
}];
t.throws(
(() => vm.deleteSprite('id')),
new Error ('Cannot delete non-sprite targets.')
);
t.end();
});
test('deleteSprite throws when there is no sprite for the given target', t => {
const vm = new VirtualMachine();
vm.runtime.targets = [{
id: 'id',
isSprite: () => true,
sprite: null
}];
t.throws(
(() => vm.deleteSprite('id')),
new Error ('No sprite associated with this target.')
);
t.end();
});
test('deleteSprite throws when there is no target with given id', t => {
const vm = new VirtualMachine();
vm.runtime.targets = [{
id: 'id',
isSprite: () => true,
sprite: {
name: 'this name'
}
}];
vm.runtime.getTargetById = () => null;
t.throws(
(() => vm.deleteSprite('id')),
new Error ('No target with the provided id.')
);
t.end();
});
test('emitWorkspaceUpdate', t => { test('emitWorkspaceUpdate', t => {
const vm = new VirtualMachine(); const vm = new VirtualMachine();
vm.runtime.targets = [ vm.runtime.targets = [