mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 06:23:37 -05:00
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:
parent
a735459427
commit
cb31f206a0
2 changed files with 48 additions and 1 deletions
|
@ -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.');
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 = [
|
||||||
|
|
Loading…
Reference in a new issue