Skip to content

Commit e49a8f0

Browse files
committed
test: improve turtles.test.js mocks with canvas, stage properties, and global functions
1 parent 8483b22 commit e49a8f0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

js/__tests__/turtles.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,34 @@ global.Turtle = jest.fn().mockImplementation(() => ({
5858
}
5959
}));
6060

61+
// Mock global functions required by TurtlesView
62+
global.docById = jest.fn(id => {
63+
const element = document.getElementById(id);
64+
if (!element && id === "buttoncontainerTOP") {
65+
const div = document.createElement("div");
66+
div.id = id;
67+
document.body.appendChild(div);
68+
return div;
69+
}
70+
return element;
71+
});
72+
73+
global.window = global.window || {};
74+
global.window.jQuery = jest.fn(() => ({
75+
tooltip: jest.fn()
76+
}));
77+
global.window.jQuery.noConflict = jest.fn(() => global.window.jQuery);
78+
79+
global.window.btoa = jest.fn(str => Buffer.from(str).toString("base64"));
80+
global.base64Encode = jest.fn(str => str);
81+
82+
global.platformColor = {
83+
background: "#ffffff",
84+
ruleColor: "#000000"
85+
};
86+
87+
global._ = jest.fn(str => str);
88+
6189
/**
6290
* Helper to mix TurtlesModel and TurtlesView prototype methods into a Turtles instance,
6391
* mimicking what importMembers does at runtime.
@@ -90,6 +118,7 @@ describe("Turtles Class", () => {
90118
stage: { addChild: jest.fn(), removeChild: jest.fn() },
91119
refreshCanvas: jest.fn(),
92120
turtleContainer: new createjs.Container(),
121+
canvas: { width: 1200, height: 900, style: {} },
93122
hideAuxMenu: jest.fn(),
94123
hideGrids: jest.fn(),
95124
_doCartesianPolar: jest.fn()
@@ -98,6 +127,12 @@ describe("Turtles Class", () => {
98127
turtles = new Turtles(activityMock);
99128
turtles.activity = activityMock;
100129
turtles._borderContainer = new createjs.Container();
130+
turtles._canvas = activityMock.canvas;
131+
turtles._masterStage = activityMock.stage;
132+
turtles._stage = activityMock.turtleContainer;
133+
turtles._scale = 1.0;
134+
turtles._w = 1200;
135+
turtles._h = 900;
101136

102137
// Mix in the prototypes to get the getters and setters
103138
mixinPrototypes(turtles);

0 commit comments

Comments
 (0)