|
| 1 | +const { Mouse, MusicBlocks } = require('../export.js'); |
| 2 | +global.importMembers = jest.fn(); |
| 3 | +global.JSInterface = { |
| 4 | + validateArgs: jest.fn((method, args) => args), |
| 5 | +}; |
| 6 | +global.Painter = { |
| 7 | + prototype: { |
| 8 | + method1: jest.fn(), |
| 9 | + method2: jest.fn(), |
| 10 | + }, |
| 11 | +}; |
| 12 | + |
| 13 | +const JSEditor = { |
| 14 | + logConsole: jest.fn(), |
| 15 | +}; |
| 16 | +global.JSEditor = JSEditor; |
| 17 | + |
| 18 | +const globalActivity = { |
| 19 | + turtles: { |
| 20 | + getTurtleCount: jest.fn(), |
| 21 | + getTurtle: jest.fn(), |
| 22 | + addTurtle: jest.fn(), |
| 23 | + getIndexOfTurtle: jest.fn(), |
| 24 | + removeTurtle: jest.fn(), |
| 25 | + screenX2turtleX: jest.fn(), |
| 26 | + screenY2turtleY: jest.fn(), |
| 27 | + }, |
| 28 | + logo: { |
| 29 | + prepSynths: jest.fn(), |
| 30 | + firstNoteTime: null, |
| 31 | + stage: { |
| 32 | + removeEventListener: jest.fn(), |
| 33 | + }, |
| 34 | + }, |
| 35 | + textMsg: jest.fn(), |
| 36 | + stage: { |
| 37 | + dispatchEvent: jest.fn(), |
| 38 | + }, |
| 39 | +}; |
| 40 | +global.globalActivity = globalActivity; |
| 41 | +global.Singer = { |
| 42 | + RhythmActions: { |
| 43 | + getNoteValue: jest.fn(), |
| 44 | + }, |
| 45 | + MeterActions: { |
| 46 | + setPickup: jest.fn(), |
| 47 | + getWholeNotesPlayed: jest.fn(), |
| 48 | + getBeatCount: jest.fn(), |
| 49 | + getMeasureCount: jest.fn(), |
| 50 | + getBPM: jest.fn(), |
| 51 | + getBeatFactor: jest.fn(), |
| 52 | + getCurrentMeter: jest.fn(), |
| 53 | + }, |
| 54 | + PitchActions: { |
| 55 | + deltaPitch: jest.fn(), |
| 56 | + consonantStepSize: jest.fn(), |
| 57 | + }, |
| 58 | + IntervalsActions: { |
| 59 | + setMovableDo: jest.fn(), |
| 60 | + getCurrentKey: jest.fn(), |
| 61 | + getCurrentMode: jest.fn(), |
| 62 | + getModeLength: jest.fn(), |
| 63 | + }, |
| 64 | + VolumeActions: { |
| 65 | + setPanning: jest.fn(), |
| 66 | + setMasterVolume: jest.fn(), |
| 67 | + masterVolume: 1.0, |
| 68 | + }, |
| 69 | +}; |
| 70 | + |
| 71 | +describe('Mouse Class', () => { |
| 72 | + let mouse; |
| 73 | + const mockFlow = jest.fn(); |
| 74 | + |
| 75 | + beforeEach(() => { |
| 76 | + globalActivity.turtles.getTurtleCount.mockReturnValue(1); |
| 77 | + globalActivity.turtles.getTurtle.mockReturnValue({ |
| 78 | + id: 1, |
| 79 | + initTurtle: jest.fn(), |
| 80 | + doWait: jest.fn(), |
| 81 | + }); |
| 82 | + mouse = new Mouse(mockFlow); |
| 83 | + }); |
| 84 | + |
| 85 | + test('should create a new Mouse instance', () => { |
| 86 | + expect(mouse).toBeInstanceOf(Mouse); |
| 87 | + expect(Mouse.MouseList).toContain(mouse); |
| 88 | + expect(Mouse.TurtleMouseMap[1]).toBe(mouse); |
| 89 | + }); |
| 90 | + |
| 91 | + test('should get Mouse from Turtle', () => { |
| 92 | + const turtle = { id: 1 }; |
| 93 | + const result = Mouse.getMouseFromTurtle(turtle); |
| 94 | + expect(result).toBe(mouse); |
| 95 | + }); |
| 96 | + |
| 97 | + test('should return null if Turtle is not in map', () => { |
| 98 | + const turtle = { id: 2 }; |
| 99 | + const result = Mouse.getMouseFromTurtle(turtle); |
| 100 | + expect(result).toBeNull(); |
| 101 | + }); |
| 102 | + |
| 103 | + test('should run the flow', () => { |
| 104 | + mouse.run(); |
| 105 | + expect(mouse.turtle.doWait).toHaveBeenCalledWith(0); |
| 106 | + expect(mockFlow).toHaveBeenCalledWith(mouse.MB); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe('MusicBlocks Class', () => { |
| 111 | + let musicBlocks; |
| 112 | + let mouse; |
| 113 | + |
| 114 | + beforeEach(() => { |
| 115 | + globalActivity.turtles.getTurtleCount.mockReturnValue(1); |
| 116 | + globalActivity.turtles.getTurtle.mockReturnValue({ |
| 117 | + id: 1, |
| 118 | + initTurtle: jest.fn(), |
| 119 | + doWait: jest.fn(), |
| 120 | + container: { x: 10, y: 20 }, |
| 121 | + }); |
| 122 | + mouse = new Mouse(jest.fn()); |
| 123 | + mouse.run = jest.fn(); |
| 124 | + musicBlocks = new MusicBlocks(mouse); |
| 125 | + }); |
| 126 | + |
| 127 | + test('should create a new MusicBlocks instance', () => { |
| 128 | + expect(musicBlocks).toBeInstanceOf(MusicBlocks); |
| 129 | + expect(musicBlocks.mouse).toBe(mouse); |
| 130 | + expect(musicBlocks.turtle).toBe(mouse.turtle); |
| 131 | + }); |
| 132 | + |
| 133 | + test('should run all mice', () => { |
| 134 | + Mouse.MouseList.push(mouse); |
| 135 | + MusicBlocks.run(); |
| 136 | + expect(globalActivity.logo.prepSynths).toHaveBeenCalled(); |
| 137 | + expect(mouse.run).toHaveBeenCalled(); |
| 138 | + }); |
| 139 | + |
| 140 | + test('should handle ENDFLOW', async () => { |
| 141 | + const result = await musicBlocks.ENDFLOW; |
| 142 | + expect(result).toBeUndefined(); |
| 143 | + }); |
| 144 | + |
| 145 | + test('should handle ENDFLOWCOMMAND', async () => { |
| 146 | + musicBlocks.turtle.waitTime = 100; |
| 147 | + musicBlocks.turtle.doWait = jest.fn(); |
| 148 | + musicBlocks.listeners.push('testSignal'); |
| 149 | + |
| 150 | + await musicBlocks.ENDFLOWCOMMAND; |
| 151 | + expect(globalActivity.stage.dispatchEvent).toHaveBeenCalledWith('testSignal'); |
| 152 | + expect(musicBlocks.turtle.doWait).toHaveBeenCalledWith(0); |
| 153 | + }); |
| 154 | + |
| 155 | + test('should print a message', () => { |
| 156 | + musicBlocks.print('test message'); |
| 157 | + expect(JSEditor.logConsole).toHaveBeenCalled(); |
| 158 | + expect(globalActivity.textMsg).toHaveBeenCalledWith('test message'); |
| 159 | + }); |
| 160 | + |
| 161 | + test('should handle undefined message', () => { |
| 162 | + musicBlocks.print(undefined); |
| 163 | + expect(globalActivity.textMsg).toHaveBeenCalledWith('undefined'); |
| 164 | + }); |
| 165 | + |
| 166 | + test('should handle null message', () => { |
| 167 | + musicBlocks.print(null); |
| 168 | + expect(globalActivity.textMsg).toHaveBeenCalledWith('null'); |
| 169 | + }); |
| 170 | + |
| 171 | + test('should get X coordinate', () => { |
| 172 | + globalActivity.turtles.screenX2turtleX.mockReturnValue(10); |
| 173 | + expect(musicBlocks.X).toBe(10); |
| 174 | + }); |
| 175 | + |
| 176 | + test('should get Y coordinate', () => { |
| 177 | + globalActivity.turtles.screenY2turtleY.mockReturnValue(20); |
| 178 | + expect(musicBlocks.Y).toBe(20); |
| 179 | + }); |
| 180 | + |
| 181 | + test('should get HEADING', () => { |
| 182 | + musicBlocks.turtle.orientation = 90; |
| 183 | + expect(musicBlocks.HEADING).toBe(90); |
| 184 | + }); |
| 185 | + |
| 186 | + test('should get PENSIZE', () => { |
| 187 | + musicBlocks.turtle.painter = { stroke: 5 }; |
| 188 | + expect(musicBlocks.PENSIZE).toBe(5); |
| 189 | + }); |
| 190 | + |
| 191 | + test('should get COLOR', () => { |
| 192 | + musicBlocks.turtle.painter = { color: 'red' }; |
| 193 | + expect(musicBlocks.COLOR).toBe('red'); |
| 194 | + }); |
| 195 | + |
| 196 | + test('should get SHADE', () => { |
| 197 | + musicBlocks.turtle.painter = { value: 50 }; |
| 198 | + expect(musicBlocks.SHADE).toBe(50); |
| 199 | + }); |
| 200 | + |
| 201 | + test('should get GREY', () => { |
| 202 | + musicBlocks.turtle.painter = { chroma: 75 }; |
| 203 | + expect(musicBlocks.GREY).toBe(75); |
| 204 | + }); |
| 205 | + |
| 206 | + test('should get NOTEVALUE', () => { |
| 207 | + Singer.RhythmActions.getNoteValue.mockReturnValue(1); |
| 208 | + expect(musicBlocks.NOTEVALUE).toBe(1); |
| 209 | + }); |
| 210 | + |
| 211 | + test('should set PICKUP', () => { |
| 212 | + musicBlocks.PICKUP = 2; |
| 213 | + expect(Singer.MeterActions.setPickup).toHaveBeenCalledWith(2, musicBlocks.turIndex); |
| 214 | + }); |
| 215 | + |
| 216 | + test('should get WHOLENOTESPLAYED', () => { |
| 217 | + Singer.MeterActions.getWholeNotesPlayed.mockReturnValue(4); |
| 218 | + expect(musicBlocks.WHOLENOTESPLAYED).toBe(4); |
| 219 | + }); |
| 220 | + |
| 221 | + test('should get BEATCOUNT', () => { |
| 222 | + Singer.MeterActions.getBeatCount.mockReturnValue(16); |
| 223 | + expect(musicBlocks.BEATCOUNT).toBe(16); |
| 224 | + }); |
| 225 | + |
| 226 | + test('should get MEASURECOUNT', () => { |
| 227 | + Singer.MeterActions.getMeasureCount.mockReturnValue(4); |
| 228 | + expect(musicBlocks.MEASURECOUNT).toBe(4); |
| 229 | + }); |
| 230 | + |
| 231 | + test('should get BPM', () => { |
| 232 | + Singer.MeterActions.getBPM.mockReturnValue(120); |
| 233 | + expect(musicBlocks.BPM).toBe(120); |
| 234 | + }); |
| 235 | + |
| 236 | + test('should get BEATFACTOR', () => { |
| 237 | + Singer.MeterActions.getBeatFactor.mockReturnValue(1.5); |
| 238 | + expect(musicBlocks.BEATFACTOR).toBe(1.5); |
| 239 | + }); |
| 240 | + |
| 241 | + test('should get CURRENTMETER', () => { |
| 242 | + Singer.MeterActions.getCurrentMeter.mockReturnValue('4/4'); |
| 243 | + expect(musicBlocks.CURRENTMETER).toBe('4/4'); |
| 244 | + }); |
| 245 | + |
| 246 | + test('should get SCALARCHANGEINPITCH', () => { |
| 247 | + Singer.PitchActions.deltaPitch.mockReturnValue(2); |
| 248 | + expect(musicBlocks.SCALARCHANGEINPITCH).toBe(2); |
| 249 | + }); |
| 250 | + |
| 251 | + test('should get CHANGEINPITCH', () => { |
| 252 | + Singer.PitchActions.deltaPitch.mockReturnValue(1); |
| 253 | + expect(musicBlocks.CHANGEINPITCH).toBe(1); |
| 254 | + }); |
| 255 | + |
| 256 | + test('should get SCALARSTEPUP', () => { |
| 257 | + Singer.PitchActions.consonantStepSize.mockReturnValue(1); |
| 258 | + expect(musicBlocks.SCALARSTEPUP).toBe(1); |
| 259 | + }); |
| 260 | + |
| 261 | + test('should get SCALARSTEPDOWN', () => { |
| 262 | + Singer.PitchActions.consonantStepSize.mockReturnValue(-1); |
| 263 | + expect(musicBlocks.SCALARSTEPDOWN).toBe(-1); |
| 264 | + }); |
| 265 | + |
| 266 | + test('should set MOVABLEDO', () => { |
| 267 | + musicBlocks.MOVABLEDO = true; |
| 268 | + expect(Singer.IntervalsActions.setMovableDo).toHaveBeenCalledWith(true, musicBlocks.turIndex); |
| 269 | + }); |
| 270 | + |
| 271 | + test('should get CURRENTKEY', () => { |
| 272 | + Singer.IntervalsActions.getCurrentKey.mockReturnValue('C'); |
| 273 | + expect(musicBlocks.CURRENTKEY).toBe('C'); |
| 274 | + }); |
| 275 | + |
| 276 | + test('should get CURRENTMODE', () => { |
| 277 | + Singer.IntervalsActions.getCurrentMode.mockReturnValue('major'); |
| 278 | + expect(musicBlocks.CURRENTMODE).toBe('major'); |
| 279 | + }); |
| 280 | + |
| 281 | + test('should get MODELENGTH', () => { |
| 282 | + Singer.IntervalsActions.getModeLength.mockReturnValue(7); |
| 283 | + expect(musicBlocks.MODELENGTH).toBe(7); |
| 284 | + }); |
| 285 | + |
| 286 | + test('should set PANNING', () => { |
| 287 | + musicBlocks.PANNING = 0.5; |
| 288 | + expect(Singer.VolumeActions.setPanning).toHaveBeenCalledWith(0.5, musicBlocks.turIndex); |
| 289 | + }); |
| 290 | + |
| 291 | + test('should set MASTERVOLUME', () => { |
| 292 | + musicBlocks.MASTERVOLUME = 0.8; |
| 293 | + expect(Singer.VolumeActions.setMasterVolume).toHaveBeenCalledWith(0.8, musicBlocks.turIndex); |
| 294 | + }); |
| 295 | + |
| 296 | + test('should get MASTERVOLUME', () => { |
| 297 | + expect(musicBlocks.MASTERVOLUME).toBe(1.0); |
| 298 | + }); |
| 299 | +}); |
0 commit comments