Skip to content

Commit e7a4beb

Browse files
authored
test suite for js/protoblocks.js (#4382)
* creating test suite for js/protoblocks.js * Exporting modules for test
1 parent c2732eb commit e7a4beb

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

js/__tests__/protoblocks.test.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const ProtoBlock = require("../protoblocks");
2+
global.createjs = {
3+
Container: jest.fn(() => ({
4+
addChild: jest.fn(),
5+
getBounds: jest.fn(() => ({ width: 50 })),
6+
})),
7+
Text: jest.fn((text, font, color) => ({ text, font, color })),
8+
};
9+
10+
global.SVG = jest.fn(() => ({
11+
setScale: jest.fn(),
12+
setTab: jest.fn(),
13+
setSlot: jest.fn(),
14+
setFontSize: jest.fn(),
15+
setExpand: jest.fn(),
16+
basicBlock: jest.fn(() => "<svg></svg>"),
17+
docks: [[0, 0]],
18+
getWidth: jest.fn(() => 100),
19+
getHeight: jest.fn(() => 50),
20+
}));
21+
22+
global.DEFAULTBLOCKSCALE = 1.0;
23+
global.STANDARDBLOCKHEIGHT = 20;
24+
25+
describe("ProtoBlock", () => {
26+
let block;
27+
beforeEach(() => {
28+
block = new ProtoBlock("TestBlock");
29+
});
30+
31+
test("should initialize with default properties", () => {
32+
expect(block.name).toBe("TestBlock");
33+
expect(block.palette).toBeNull();
34+
expect(block.style).toBeNull();
35+
expect(block.generator).toBeNull();
36+
expect(block.expandable).toBe(false);
37+
expect(block.args).toBe(0);
38+
expect(block.defaults).toEqual([]);
39+
});
40+
41+
test("adjustWidthToLabel should set textWidth and extraWidth", () => {
42+
block.staticLabels.push("Example Label");
43+
block.adjustWidthToLabel();
44+
expect(block.textWidth).toBe(50);
45+
expect(block.extraWidth).toBeGreaterThan(0);
46+
});
47+
48+
test("zeroArgBlock should set correct properties", () => {
49+
block.zeroArgBlock();
50+
expect(block.args).toBe(0);
51+
expect(block.dockTypes).toEqual(["out", "in"]);
52+
expect(typeof block.generator).toBe("function");
53+
});
54+
55+
test("zeroArgBlockGenerator should return SVG details", () => {
56+
block.zeroArgBlock();
57+
const result = block.generator();
58+
expect(result).toEqual(["<svg></svg>", [[0, 0]], 100, 50, 50]);
59+
});
60+
61+
test("oneArgBlock should set correct properties", () => {
62+
block.oneArgBlock();
63+
expect(block.args).toBe(1);
64+
expect(block.dockTypes).toEqual(["out", "numberin", "in"]);
65+
});
66+
67+
test("twoArgBlock should configure expandable block", () => {
68+
block.twoArgBlock();
69+
expect(block.expandable).toBe(true);
70+
expect(block.args).toBe(2);
71+
expect(block.dockTypes).toContain("numberin");
72+
});
73+
74+
test("hiddenBlockFlow should set generator correctly", () => {
75+
block.hiddenBlockFlow();
76+
expect(block.size).toBe(0);
77+
expect(block.dockTypes).toContain("in");
78+
});
79+
80+
test("booleanZeroArgBlock should configure boolean output", () => {
81+
block.booleanZeroArgBlock();
82+
expect(block.dockTypes).toContain("booleanout");
83+
});
84+
85+
test("parameterBlock should set up a value output block", () => {
86+
block.parameterBlock();
87+
expect(block.dockTypes).toContain("numberout");
88+
expect(block.parameter).toBe(true);
89+
});
90+
});

js/protoblocks.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,3 +2329,7 @@ class StackClampBlock extends BaseBlock {
23292329
});
23302330
}
23312331
}
2332+
2333+
if (typeof module !== "undefined" && module.exports) {
2334+
module.exports = ProtoBlock;
2335+
}

0 commit comments

Comments
 (0)