Skip to content

Commit 8adff5d

Browse files
authored
Test suite for turtle-painter.js (#4368)
* creating test suite for turtle-painter.js * Exporting modules for test
1 parent 57a1301 commit 8adff5d

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const Painter = require("../turtle-painter");
2+
global.WRAP = true;
3+
const mockTurtle = {
4+
turtles: {
5+
screenX2turtleX: jest.fn(),
6+
screenY2turtleY: jest.fn(),
7+
turtleX2screenX: jest.fn(),
8+
turtleY2screenY: jest.fn(),
9+
scale: 1
10+
},
11+
activity: { refreshCanvas: jest.fn() },
12+
container: { x: 0, y: 0, rotation: 0 },
13+
ctx: {
14+
beginPath: jest.fn(),
15+
clearRect: jest.fn(),
16+
stroke: jest.fn(),
17+
closePath: jest.fn(),
18+
moveTo: jest.fn(),
19+
lineTo: jest.fn(),
20+
arc: jest.fn(),
21+
canvas: { width: 800, height: 600 }
22+
},
23+
penstrokes: { image: null },
24+
orientation: 0,
25+
updateCache: jest.fn(),
26+
blinking: jest.fn().mockReturnValue(false)
27+
};
28+
29+
describe("Painter Class", () => {
30+
let painter;
31+
32+
beforeEach(() => {
33+
painter = new Painter(mockTurtle);
34+
});
35+
36+
describe("Constructor", () => {
37+
test("should initialize with default values", () => {
38+
expect(painter._color).toBe(0);
39+
expect(painter._stroke).toBe(5);
40+
expect(painter._penDown).toBe(true);
41+
});
42+
});
43+
44+
describe("Setters and Getters", () => {
45+
test("should set and get color", () => {
46+
painter.color = 10;
47+
expect(painter.color).toBe(10);
48+
});
49+
50+
test("should set and get stroke", () => {
51+
painter.stroke = 8;
52+
expect(painter.stroke).toBe(8);
53+
});
54+
});
55+
56+
describe("Actions", () => {
57+
test("should move forward", () => {
58+
painter.doForward(10);
59+
expect(mockTurtle.activity.refreshCanvas).toHaveBeenCalled();
60+
});
61+
62+
test("should turn right", () => {
63+
painter.doRight(90);
64+
expect(mockTurtle.orientation).toBe(90);
65+
});
66+
});
67+
});

js/turtle-painter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,3 +1473,7 @@ class Painter {
14731473
this.turtle.updateCache();
14741474
}
14751475
}
1476+
1477+
if (typeof module !== "undefined" && module.exports) {
1478+
module.exports = Painter;
1479+
}

0 commit comments

Comments
 (0)