Skip to content

Commit 882ed8b

Browse files
committed
Add tests
1 parent 255d105 commit 882ed8b

File tree

6 files changed

+87
-2
lines changed

6 files changed

+87
-2
lines changed

src/engine/blocks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ class Blocks {
824824
// Delete comments attached to the block.
825825
if (block.comment) {
826826
const editingTarget = this.runtime.getEditingTarget();
827-
if (editingTarget.comments.hasOwnProperty(block.comment)) {
827+
if (editingTarget && editingTarget.comments.hasOwnProperty(block.comment)) {
828828
delete editingTarget.comments[block.comment];
829829
}
830830
}

src/serialization/sb3.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ const deserializeFields = function (fields) {
798798
* work with pre-parsed deserialized blocks.
799799
*
800800
* @param {object} blocks Serialized SB3 "blocks" property of a target. Will be mutated.
801-
* @return {object} input is modified and returned
801+
* @return {object} object that has "blocks" and "oldToNew" attributes
802802
*/
803803
const deserializeBlocks = function (blocks) {
804804
// oldToNew is used to fix comments attached to variable blocks: see #1893

test/fixtures/variable-comment.sb3

1.19 KB
Binary file not shown.

test/integration/variable-comment.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const path = require('path');
2+
const test = require('tap').test;
3+
const makeTestStorage = require('../fixtures/make-test-storage');
4+
const readFileToBuffer = require('../fixtures/readProjectFile').readFileToBuffer;
5+
const VirtualMachine = require('../../src/index');
6+
7+
const projectUri = path.resolve(__dirname, '../fixtures/variable-comment.sb3');
8+
const project = readFileToBuffer(projectUri);
9+
10+
test('load an sb3 project with comments attached to variable blocks (#1893)', t => {
11+
const vm = new VirtualMachine();
12+
vm.attachStorage(makeTestStorage());
13+
14+
// Evaluate playground data and exit
15+
vm.on('playgroundData', e => {
16+
const threads = JSON.parse(e.threads);
17+
t.equal(threads.length, 0);
18+
19+
const stage = vm.runtime.targets[0];
20+
const stageComments = Object.values(stage.comments);
21+
22+
// Stage has 2 comments
23+
t.equal(stageComments.length, 2);
24+
25+
// comment1 is atached to variable block
26+
t.equal(stageComments[0].text, 'comment1');
27+
const variableBlock = stage.blocks.getBlock(stageComments[0].blockId);
28+
t.equal(variableBlock.opcode, 'data_variable');
29+
30+
// comment2 is atached to variable block
31+
t.equal(stageComments[1].text, 'comment2');
32+
const listBlock = stage.blocks.getBlock(stageComments[1].blockId);
33+
t.equal(listBlock.opcode, 'data_listcontents');
34+
35+
t.end();
36+
process.nextTick(process.exit);
37+
});
38+
39+
// Start VM, load project, and run
40+
t.doesNotThrow(() => {
41+
vm.start();
42+
vm.clear();
43+
vm.setCompatibilityMode(false);
44+
vm.setTurboMode(false);
45+
vm.loadProject(project).then(() => {
46+
vm.greenFlag();
47+
setTimeout(() => {
48+
vm.getPlaygroundData();
49+
vm.stopAll();
50+
}, 100);
51+
});
52+
});
53+
});

test/unit/engine_blocks.js

+19
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,25 @@ test('delete inputs', t => {
600600
t.end();
601601
});
602602

603+
test('delete block with comment', t => {
604+
const b = new Blocks(new Runtime());
605+
const fakeTarget = {
606+
comments: {
607+
bar: {
608+
blockId: 'foo'
609+
}
610+
}
611+
};
612+
b.runtime.getEditingTarget = () => fakeTarget;
613+
b.createBlock({
614+
id: 'foo',
615+
comment: 'bar'
616+
});
617+
b.deleteBlock('foo');
618+
t.notOk(fakeTarget.comments.hasOwnProperty('bar'));
619+
t.end();
620+
});
621+
603622
test('updateAssetName function updates name in sound field', t => {
604623
const b = new Blocks(new Runtime());
605624
b.createBlock({

test/unit/sprites_rendered-target.js

+13
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ test('blocks get new id on duplicate', t => {
4545
});
4646
});
4747

48+
test('comments are duplicated when duplicating target', t => {
49+
const r = new Runtime();
50+
const s = new Sprite(null, r);
51+
const rt = new RenderedTarget(s, r);
52+
rt.createComment('commentid', null, 'testcomment', 0, 0, 100, 100, false);
53+
t.ok(s.comments.hasOwnProperty('commentid'));
54+
return rt.duplicate().then(duplicate => {
55+
t.notOk(duplicate.comments.hasOwnProperty('commentid'));
56+
t.ok(Object.keys(duplicate.comments).length === 1);
57+
t.end();
58+
});
59+
});
60+
4861
test('direction', t => {
4962
const r = new Runtime();
5063
const s = new Sprite(null, r);

0 commit comments

Comments
 (0)