Skip to content

Commit 4c40a09

Browse files
authored
test suite for js/turtle-singer.js (#4386)
* Creating test for js/turtle-singer.js * Exporting modules for test
1 parent 8420299 commit 4c40a09

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

js/__tests__/turtle-singer.test.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
global.DEFAULTVOLUME = 100;
2+
global.TARGETBPM = 120;
3+
global.TONEBPM = 60;
4+
5+
const Singer = require('../turtle-singer');
6+
7+
const mockGlobals = {
8+
getNote: jest.fn().mockReturnValue(['C', 4]),
9+
isCustomTemperament: jest.fn(),
10+
getStepSizeUp: jest.fn().mockReturnValue(1),
11+
numberToPitch: jest.fn().mockReturnValue(['C', 4]),
12+
pitchToNumber: jest.fn().mockReturnValue(60)
13+
};
14+
15+
global.getNote = mockGlobals.getNote;
16+
global.isCustomTemperament = mockGlobals.isCustomTemperament;
17+
global.getStepSizeUp = mockGlobals.getStepSizeUp;
18+
global.numberToPitch = mockGlobals.numberToPitch;
19+
global.pitchToNumber = mockGlobals.pitchToNumber;
20+
21+
describe('Singer Class', () => {
22+
let turtleMock;
23+
let activityMock;
24+
let logoMock;
25+
let singer;
26+
27+
beforeEach(() => {
28+
turtleMock = {
29+
turtles: [],
30+
singer: new Singer(this),
31+
synthVolume: { DEFAULTVOICE: [100] },
32+
inNoteBlock: [0],
33+
notePitches: { 0: [] },
34+
noteOctaves: { 0: [] },
35+
noteCents: { 0: [] },
36+
noteHertz: { 0: [] }
37+
};
38+
39+
activityMock = {
40+
turtles: {
41+
ithTurtle: jest.fn().mockReturnValue(turtleMock),
42+
turtleList: [turtleMock],
43+
},
44+
logo: {
45+
synth: {
46+
setMasterVolume: jest.fn(),
47+
setVolume: jest.fn(),
48+
rampTo: jest.fn()
49+
},
50+
pitchDrumMatrix: { addRowBlock: jest.fn() },
51+
notation: { notationInsertTie: jest.fn(), notationRemoveTie: jest.fn() },
52+
firstNoteTime: null,
53+
stopTurtle: false,
54+
inPitchDrumMatrix: false,
55+
inMatrix: false,
56+
clearNoteParams: jest.fn(),
57+
},
58+
};
59+
60+
logoMock = {
61+
activity: activityMock,
62+
synth: {
63+
setMasterVolume: jest.fn(),
64+
setVolume: jest.fn(),
65+
rampTo: jest.fn(),
66+
getFrequency: jest.fn(),
67+
getCustomFrequency: jest.fn()
68+
},
69+
inPitchDrumMatrix: false,
70+
inMatrix: false,
71+
clearNoteParams: jest.fn(),
72+
};
73+
74+
singer = new Singer(turtleMock);
75+
});
76+
77+
test('should initialize with correct default values', () => {
78+
expect(singer.turtle).toBe(turtleMock);
79+
expect(singer.turtles).toBe(turtleMock.turtles);
80+
expect(singer.defaultNoteValue).toBe(4);
81+
expect(singer.register).toBe(0);
82+
expect(singer.beatFactor).toBe(1);
83+
expect(singer.currentOctave).toBe(4);
84+
});
85+
86+
test('should correctly add scalar transposition', () => {
87+
const result = Singer.addScalarTransposition(logoMock, turtleMock, 'C', 4, 2);
88+
expect(result).toEqual(['C', 4]);
89+
});
90+
91+
test('should correctly calculate scalar distance', () => {
92+
const result = Singer.scalarDistance(logoMock, turtleMock, 60, 62);
93+
expect(result).toBeGreaterThan(0);
94+
});
95+
96+
test('should correctly calculate inversion', () => {
97+
const result = Singer.calculateInvert(logoMock, turtleMock, 'C', 4);
98+
expect(result).toBe(0);
99+
});
100+
101+
test('should set master volume correctly', () => {
102+
Singer.setMasterVolume(logoMock, 50);
103+
expect(logoMock.synth.setMasterVolume).toHaveBeenCalledWith(50);
104+
});
105+
106+
test('should set synth volume correctly', () => {
107+
Singer.setSynthVolume(logoMock, turtleMock, 'noise1', 80);
108+
expect(logoMock.synth.setVolume).toHaveBeenCalledWith(turtleMock, 'noise1', 80 / 25);
109+
});
110+
});

js/turtle-singer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,3 +2389,7 @@ class Singer {
23892389
activity.stage.update();
23902390
}
23912391
}
2392+
2393+
if (typeof module !== "undefined" && module.exports) {
2394+
module.exports = Singer;
2395+
}

0 commit comments

Comments
 (0)