Skip to content

Commit 22c9cbb

Browse files
authored
Resolves all the errors with VolumeActions.test.js (#4422)
* resolve error and adding more tests Resolves all the errors of the test cases for the VolumeActions.test.js * resolving error with mocking beforeEach * removing unwanted space
1 parent ef02fc7 commit 22c9cbb

File tree

1 file changed

+125
-34
lines changed

1 file changed

+125
-34
lines changed

js/turtleactions/__tests__/VolumeActions.test.js

Lines changed: 125 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,110 +22,113 @@ describe('setupVolumeActions', () => {
2222
};
2323
global.last = jest.fn(array => array[array.length - 1]);
2424
global._ = jest.fn(msg => msg);
25-
global.VOICENAMES = {};
26-
global.DRUMNAMES = {};
25+
global.VOICENAMES = { Piano: ['Piano', 'piano'] };
26+
global.DRUMNAMES = { Kick: ['Kick', 'kick'] };
2727
global.DEFAULTVOLUME = 50;
2828
global.DEFAULTVOICE = 'default';
2929
});
3030

3131
beforeEach(() => {
32+
global.Mouse = {
33+
getMouseFromTurtle: jest.fn().mockReturnValue({
34+
MB: { listeners: [] },
35+
}),
36+
};
37+
global.MusicBlocks = { isRun: true };
3238
activity = {
3339
turtles: {
3440
ithTurtle: jest.fn(),
35-
singer: {
36-
synthVolume: { default: [DEFAULTVOLUME] },
37-
crescendoInitialVolume: { default: [DEFAULTVOLUME] },
38-
crescendoDelta: [],
39-
inCrescendo: [],
40-
instrumentNames: [],
41-
suppressOutput: false,
42-
justCounting: [],
43-
panner: new Tone.Panner(),
44-
},
4541
},
4642
blocks: {
47-
blockList: { 1: {} },
43+
blockList: {
44+
1: { connections: [{}, {}] },
45+
},
4846
},
4947
logo: {
5048
setDispatchBlock: jest.fn(),
5149
setTurtleListener: jest.fn(),
5250
synth: {
5351
loadSynth: jest.fn(),
52+
setMasterVolume: jest.fn(),
5453
},
5554
notation: {
5655
notationBeginArticulation: jest.fn(),
5756
notationEndArticulation: jest.fn(),
5857
notationEndCrescendo: jest.fn(),
5958
},
59+
blockList: {
60+
1: { connections: [{}, {}] },
61+
2: { connections: [{}] },
62+
},
6063
},
6164
errorMsg: jest.fn(),
6265
};
63-
66+
67+
if (!activity.logo.blockList) {
68+
activity.logo.blockList = {};
69+
}
70+
71+
activity.logo.blockList['testBlock'] = { connections: [{}] };
6472
targetTurtle = {
6573
singer: {
66-
synthVolume: { default: [DEFAULTVOLUME] },
74+
synthVolume: { default: [DEFAULTVOLUME] },
6775
crescendoInitialVolume: { default: [DEFAULTVOLUME] },
68-
synthVolume: {},
6976
crescendoDelta: [],
70-
crescendoInitialVolume: {},
7177
inCrescendo: [],
7278
instrumentNames: [],
7379
suppressOutput: false,
7480
justCounting: [],
7581
panner: new Tone.Panner(),
7682
},
7783
};
78-
7984
activity.turtles.ithTurtle.mockReturnValue(targetTurtle);
85+
8086
setupVolumeActions(activity);
87+
activity.errorMsg.mockImplementation((message) => {
88+
if (message.includes('not found')) {
89+
message = message.replace("null", "invalidSynth");
90+
}
91+
return message;
92+
});
8193
});
94+
8295

8396
it('should set master volume correctly', () => {
8497
Singer.VolumeActions.setMasterVolume(80, 0, 1);
8598
expect(Singer.masterVolume).toContain(80);
86-
expect(Singer.setMasterVolume).toHaveBeenCalledWith(activity.logo, 80);
99+
expect(Singer.setMasterVolume).toHaveBeenCalledWith(activity.logo, 80, 1);
87100
});
88101

89102
it('should handle out-of-range master volume', () => {
90103
Singer.VolumeActions.setMasterVolume(120, 0, 1);
91104
expect(Singer.masterVolume).toContain(100);
92105
expect(activity.errorMsg).not.toHaveBeenCalled();
93-
94106
Singer.VolumeActions.setMasterVolume(-10, 0, 1);
95107
expect(Singer.masterVolume).toContain(0);
96108
expect(activity.errorMsg).toHaveBeenCalledWith(_('Setting volume to 0.'), 1);
97109
});
98110

99111
it('should set synth volume correctly', () => {
100112
targetTurtle.singer.synthVolume['default'] = [DEFAULTVOLUME];
101-
Singer.VolumeActions.setSynthVolume('default', 70, 0);
102-
113+
activity.logo.blockList = { testBlock: { connections: [{}] } };
114+
const someBlockId = 'testBlock';
115+
Singer.VolumeActions.setSynthVolume('default', 70, 0, someBlockId);
103116
expect(targetTurtle.singer.synthVolume['default']).toContain(70);
104117
expect(Singer.setSynthVolume).toHaveBeenCalledWith(activity.logo, 0, 'default', 70);
105118
});
106119

107120
it('should apply crescendo correctly', () => {
108-
// Ensure initial values are correctly set
109121
targetTurtle.singer.synthVolume['default'] = [DEFAULTVOLUME];
110122
targetTurtle.singer.crescendoInitialVolume['default'] = [DEFAULTVOLUME];
111-
112-
// Call the crescendo function
113123
Singer.VolumeActions.doCrescendo('crescendo', 10, 0, 1);
114-
115-
// Assert the updates to synthVolume
116124
expect(targetTurtle.singer.synthVolume['default']).toHaveLength(2);
117125
expect(targetTurtle.singer.synthVolume['default'][1]).toBe(DEFAULTVOLUME);
118-
119-
// Assert the updates to crescendoInitialVolume
120126
expect(targetTurtle.singer.crescendoInitialVolume['default']).toHaveLength(2);
121127
expect(targetTurtle.singer.crescendoInitialVolume['default'][1]).toBe(DEFAULTVOLUME);
122-
123-
// Assert other properties
124128
expect(targetTurtle.singer.crescendoDelta).toContain(10);
125129
expect(activity.logo.setTurtleListener).toHaveBeenCalled();
126130
});
127131

128-
129132
it('should set panning correctly', () => {
130133
Singer.VolumeActions.setPanning(50, 0);
131134
expect(targetTurtle.singer.panner.pan.value).toBe(0.5);
@@ -134,14 +137,102 @@ describe('setupVolumeActions', () => {
134137

135138
it('should retrieve the correct master volume', () => {
136139
Singer.masterVolume.push(60);
137-
138140
expect(Singer.VolumeActions.masterVolume).toBe(60);
139141
});
140142

141143
it('should retrieve the correct synth volume', () => {
142144
targetTurtle.singer.synthVolume['default'] = [DEFAULTVOLUME, 70];
143-
144145
const volume = Singer.VolumeActions.getSynthVolume('default', 0);
145146
expect(volume).toBe(70);
146147
});
148+
149+
it('should set relative volume correctly', () => {
150+
if (!targetTurtle.singer.synthVolume['default']) {
151+
targetTurtle.singer.synthVolume['default'] = [DEFAULTVOLUME];
152+
}
153+
Singer.VolumeActions.setRelativeVolume(20, 0, 1);
154+
expect(targetTurtle.singer.synthVolume['default']).toHaveLength(2);
155+
expect(Singer.setSynthVolume).toHaveBeenCalled();
156+
});
157+
158+
it('should handle out-of-range relative volume', () => {
159+
if (!targetTurtle.singer.synthVolume['default']) {
160+
targetTurtle.singer.synthVolume['default'] = [DEFAULTVOLUME];
161+
}
162+
Singer.VolumeActions.setRelativeVolume(200, 0, 1);
163+
expect(targetTurtle.singer.synthVolume['default']).toContain(100);
164+
Singer.VolumeActions.setRelativeVolume(-200, 0, 1);
165+
expect(targetTurtle.singer.synthVolume['default']).toContain(-100);
166+
});
167+
168+
it('should handle synth volume for different synths', () => {
169+
targetTurtle.singer.synthVolume = targetTurtle.singer.synthVolume || {};
170+
Singer.VolumeActions.setSynthVolume('piano', 60, 0, 'testBlock');
171+
expect(targetTurtle.singer.synthVolume['piano'][targetTurtle.singer.synthVolume['piano'].length - 1]).toBe(60);
172+
Singer.VolumeActions.setSynthVolume('piano', 50, 0, 'testBlock');
173+
expect(targetTurtle.singer.synthVolume['piano'][targetTurtle.singer.synthVolume['piano'].length - 1]).toBe(50);
174+
expect(Singer.setSynthVolume).toHaveBeenCalledWith(activity.logo, 0, 'piano', 50);
175+
});
176+
177+
it('should use default voice when synth is not found', () => {
178+
activity.logo.blockList['testBlock'] = { connections: [{}] };
179+
Singer.VolumeActions.setSynthVolume('unknownSynth', 40, 0, 'testBlock');
180+
expect(targetTurtle.singer.synthVolume['default']).toContain(40);
181+
});
182+
183+
it('should retrieve synth volume when synth exists', () => {
184+
targetTurtle.singer.synthVolume['piano'] = [50, 80];
185+
expect(Singer.VolumeActions.getSynthVolume('piano', 0)).toBe(80);
186+
});
187+
188+
it('should return undefined when synth does not exist', () => {
189+
expect(Singer.VolumeActions.getSynthVolume('violin', 0)).toBeUndefined();
190+
});
191+
it('should handle doCrescendo correctly', () => {
192+
const initialVolume = 60;
193+
targetTurtle.singer.synthVolume['piano'] = [initialVolume];
194+
Singer.VolumeActions.doCrescendo('crescendo', 10, 0, 'block1');
195+
expect(targetTurtle.singer.crescendoDelta).toContain(10);
196+
expect(targetTurtle.singer.synthVolume['piano']).toContain(initialVolume);
197+
expect(activity.logo.setTurtleListener).toHaveBeenCalledWith(0, "_crescendo_0", expect.any(Function));
198+
});
199+
200+
it('should handle doDecrescendo correctly', () => {
201+
const initialVolume = 70;
202+
targetTurtle.singer.synthVolume['piano'] = [initialVolume];
203+
Singer.VolumeActions.doCrescendo('decrescendo', 20, 0, 'block1');
204+
expect(targetTurtle.singer.crescendoDelta).toContain(-20);
205+
expect(targetTurtle.singer.synthVolume['piano']).toContain(initialVolume);
206+
expect(activity.logo.setTurtleListener).toHaveBeenCalledWith(0, "_crescendo_0", expect.any(Function));
207+
});
208+
209+
it('should adjust relative volume correctly', () => {
210+
targetTurtle.singer.synthVolume['piano'] = [50];
211+
Singer.VolumeActions.setRelativeVolume(20, 0, 'block1');
212+
expect(targetTurtle.singer.synthVolume['piano']).toContain(60);
213+
expect(activity.logo.notation.notationBeginArticulation).toHaveBeenCalledWith(0);
214+
expect(activity.logo.setTurtleListener).toHaveBeenCalledWith(0, "_articulation_0", expect.any(Function));
215+
});
216+
217+
it('should adjust master volume correctly', () => {
218+
Singer.VolumeActions.setMasterVolume(75, 0, 'block1');
219+
expect(Singer.masterVolume).toContain(75);
220+
expect(Singer.setMasterVolume).toHaveBeenCalledWith(activity.logo, 75, 'block1');
221+
});
222+
223+
it('should set synth volume correctly', () => {
224+
targetTurtle.singer.synthVolume['default'] = [DEFAULTVOLUME];
225+
activity.logo.blockList = { testBlock: { connections: [{}] } };
226+
const someBlockId = 'testBlock';
227+
Singer.VolumeActions.setSynthVolume('default', 70, 0, someBlockId);
228+
expect(targetTurtle.singer.synthVolume['default']).toContain(70);
229+
expect(Singer.setSynthVolume).toHaveBeenCalledWith(activity.logo, 0, 'default', 70);
230+
});
231+
232+
it('should not set volume for undefined synth', () => {
233+
const errorMsgSpy = jest.spyOn(activity, 'errorMsg');
234+
activity.logo.blockList['testBlock'] = { connections: [{}] };
235+
Singer.VolumeActions.setSynthVolume('null', 50, 0, 'testBlock');
236+
expect(errorMsgSpy).toHaveBeenCalledWith('nullnot found');
237+
});
147238
});

0 commit comments

Comments
 (0)