Skip to content

Commit a128967

Browse files
authored
test suite for js/turtles.js (#4378)
* creating test for js/turtles.js * Exporting modules for test * Rewriting test suite for turtles.js as after some changes in the root file js/turtles.js some of tests are failing which is now being working fine
1 parent 84c45f5 commit a128967

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

js/__tests__/turtles.test.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
const Turtles = require('../turtles');
2+
3+
global.createjs = {
4+
Container: jest.fn().mockImplementation(() => ({
5+
addChild: jest.fn(),
6+
removeAllChildren: jest.fn(),
7+
on: jest.fn(),
8+
removeAllEventListeners: jest.fn(),
9+
})),
10+
Bitmap: jest.fn().mockImplementation(() => ({})),
11+
};
12+
13+
global.importMembers = jest.fn();
14+
global.setupRhythmActions = jest.fn();
15+
global.setupMeterActions = jest.fn();
16+
global.setupPitchActions = jest.fn();
17+
global.setupIntervalsActions = jest.fn();
18+
global.setupToneActions = jest.fn();
19+
global.setupOrnamentActions = jest.fn();
20+
global.setupVolumeActions = jest.fn();
21+
global.setupDrumActions = jest.fn();
22+
global.setupDictActions = jest.fn();
23+
24+
global.Turtle = jest.fn().mockImplementation(() => ({
25+
painter: {
26+
doSetHeading: jest.fn(),
27+
doSetPensize: jest.fn(),
28+
doSetChroma: jest.fn(),
29+
doSetValue: jest.fn(),
30+
doSetColor: jest.fn(),
31+
},
32+
rename: jest.fn(),
33+
container: {
34+
scaleX: 1,
35+
scaleY: 1,
36+
scale: 1,
37+
on: jest.fn(),
38+
removeAllEventListeners: jest.fn(),
39+
},
40+
}));
41+
42+
describe('Turtles Class', () => {
43+
let activityMock;
44+
let turtles;
45+
46+
beforeEach(() => {
47+
activityMock = {
48+
stage: { addChild: jest.fn(), removeChild: jest.fn() },
49+
refreshCanvas: jest.fn(),
50+
turtleContainer: new createjs.Container(),
51+
hideAuxMenu: jest.fn(),
52+
hideGrids: jest.fn(),
53+
_doCartesianPolar: jest.fn(),
54+
};
55+
56+
turtles = new Turtles(activityMock);
57+
turtles.activity = activityMock;
58+
turtles.getTurtleCount = jest.fn().mockReturnValue(0);
59+
turtles.getTurtle = jest.fn(() => ({
60+
container: {
61+
scaleX: 1,
62+
scaleY: 1,
63+
scale: 1,
64+
},
65+
}));
66+
67+
turtles.pushTurtle = jest.fn();
68+
turtles.addTurtleStageProps = jest.fn();
69+
turtles.createArtwork = jest.fn();
70+
turtles.createHitArea = jest.fn();
71+
turtles.addTurtleGraphicProps = jest.fn();
72+
turtles.isShrunk = jest.fn().mockReturnValue(false);
73+
document.body.innerHTML = '<div id="loader"></div>';
74+
});
75+
76+
test('should initialize properly', () => {
77+
expect(turtles.activity).not.toBeUndefined();
78+
expect(global.importMembers).toHaveBeenCalledWith(turtles, "", [activityMock]);
79+
});
80+
81+
test('should call initActions on construction', () => {
82+
const spy = jest.spyOn(turtles, 'initActions');
83+
turtles.initActions();
84+
expect(spy).toHaveBeenCalled();
85+
});
86+
87+
test('should add a turtle properly', () => {
88+
turtles.addTurtle({}, { id: 1, name: 'TestTurtle' });
89+
90+
expect(turtles.getTurtleCount).toHaveBeenCalled();
91+
expect(turtles.pushTurtle).toHaveBeenCalled();
92+
expect(turtles.addTurtleStageProps).toHaveBeenCalled();
93+
expect(turtles.createArtwork).toHaveBeenCalled();
94+
expect(turtles.createHitArea).toHaveBeenCalled();
95+
expect(turtles.addTurtleGraphicProps).toHaveBeenCalled();
96+
expect(turtles.isShrunk).toHaveBeenCalled();
97+
});
98+
99+
test('should toggle running state correctly', () => {
100+
turtles.markAllAsStopped();
101+
expect(activityMock.refreshCanvas).toHaveBeenCalled();
102+
});
103+
});

js/turtles.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,3 +1322,7 @@ Turtles.TurtlesView = class {
13221322
return this;
13231323
}
13241324
};
1325+
1326+
if (typeof module !== "undefined" && module.exports) {
1327+
module.exports = Turtles;
1328+
}

0 commit comments

Comments
 (0)