diff --git a/src/virtual-machine.js b/src/virtual-machine.js index 6971e4509..fe060104e 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -474,9 +474,12 @@ class VirtualMachine extends EventEmitter { */ deleteSprite (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) { + const targetIndexBeforeDelete = this.runtime.targets.map(t => t.id).indexOf(target.id); if (!target.isSprite()) { throw new Error('Cannot delete non-sprite targets.'); } diff --git a/test/unit/virtual-machine.js b/test/unit/virtual-machine.js index 624dddcd1..2aa10ec87 100644 --- a/test/unit/virtual-machine.js +++ b/test/unit/virtual-machine.js @@ -110,6 +110,50 @@ test('renameSprite does not increment when renaming to the same name', t => { 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 => { const vm = new VirtualMachine(); vm.runtime.targets = [