Skip to content

Commit

Permalink
Test File for the API (#4201)
Browse files Browse the repository at this point in the history
* Test File for the DictBlocksAPI

* Create MeterBlocksAPI.test.js

* Creating test for MeterBlocksAPI.js

* Exporting modules for test

* Creating PenBlocksAPI.test.js

test for the PenBlocksAPI.js

* Exporting modules for test

* Creating test for the IntervalsBlocksAPI.js

* Exporting modules for test

* Adding test for ToneBlocksAPI

* Creating test for PitchBlocksAPI.js

* exp
  • Loading branch information
omsuneri authored Dec 29, 2024
1 parent 4e2bb77 commit a65a865
Show file tree
Hide file tree
Showing 12 changed files with 625 additions and 0 deletions.
2 changes: 2 additions & 0 deletions js/js-export/API/DictBlocksAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ class DictBlocksAPI {
return this.runCommand("getValue", [args[1], args[0], this.turIndex]);
}
}

module.exports = DictBlocksAPI;
1 change: 1 addition & 0 deletions js/js-export/API/IntervalsBlocksAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ class IntervalsBlocksAPI {
return this.runCommand("setTemperament", [args[0], args[1], args[2]]);
}
}
module.exports = IntervalsBlocksAPI;
3 changes: 3 additions & 0 deletions js/js-export/API/MeterBlocksAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ class MeterBlocksAPI {
return Singer.MeterActions.getNotesPlayed(args[0], this.turIndex);
}
}


module.exports = MeterBlocksAPI;
1 change: 1 addition & 0 deletions js/js-export/API/PenBlocksAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,4 @@ class PenBlocksAPI {
return this.runCommand("doSetFont", [args[0]]);
}
}
module.exports = PenBlocksAPI;
1 change: 1 addition & 0 deletions js/js-export/API/PitchBlocksAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,4 @@ class PitchBlocksAPI {
return Singer.PitchActions.numToPitch(args[0], "octave", this.turIndex);
}
}
module.exports = PitchBlocksAPI;
1 change: 1 addition & 0 deletions js/js-export/API/ToneBlocksAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,4 @@ class ToneBlocksAPI {
return this.ENDFLOWCOMMAND;
}
}
module.exports = ToneBlocksAPI;
100 changes: 100 additions & 0 deletions js/js-export/API/__tests__/DictBlocksAPI.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const JSInterface = {
validateArgs: jest.fn(),
};
global.JSInterface = JSInterface;

global.globalActivity = {
turtles: {
ithTurtle: jest.fn(() => ({ name: "defaultDict" })),
},
};

const DictBlocksAPI = require("../DictBlocksAPI");

describe("DictBlocksAPI", () => {
let dictBlocksAPI;

beforeEach(() => {
dictBlocksAPI = new DictBlocksAPI();
dictBlocksAPI.turIndex = 0;
dictBlocksAPI.runCommand = jest.fn();
});

afterEach(() => {
jest.clearAllMocks();
});

test("getDict calls runCommand with correct arguments", () => {
JSInterface.validateArgs.mockReturnValue(["mockDict"]);

dictBlocksAPI.getDict("testDict");

expect(JSInterface.validateArgs).toHaveBeenCalledWith("getDict", ["testDict"]);
expect(dictBlocksAPI.runCommand).toHaveBeenCalledWith("getDict", ["mockDict", 0]);
});

test("getDict uses default turIndex when no dict is provided", () => {
JSInterface.validateArgs.mockReturnValue(["defaultDict"]);

dictBlocksAPI.getDict();

expect(JSInterface.validateArgs).toHaveBeenCalledWith("getDict", [0]);
expect(dictBlocksAPI.runCommand).toHaveBeenCalledWith("getDict", ["defaultDict", 0]);
});

test("showDict calls runCommand with correct arguments", () => {
JSInterface.validateArgs.mockReturnValue(["mockDict"]);

dictBlocksAPI.showDict("testDict");

expect(JSInterface.validateArgs).toHaveBeenCalledWith("showDict", ["testDict"]);
expect(dictBlocksAPI.runCommand).toHaveBeenCalledWith("showDict", ["mockDict", 0]);
});

test("showDict uses default turIndex when no dict is provided", () => {
JSInterface.validateArgs.mockReturnValue(["defaultDict"]);

dictBlocksAPI.showDict();

expect(JSInterface.validateArgs).toHaveBeenCalledWith("showDict", [0]);
expect(dictBlocksAPI.runCommand).toHaveBeenCalledWith("showDict", ["defaultDict", 0]);
});

test("setValue calls runCommand with correct arguments", () => {
JSInterface.validateArgs.mockReturnValue(["key", "value", "testDict"]);

dictBlocksAPI.setValue("key", "value", "testDict");

expect(JSInterface.validateArgs).toHaveBeenCalledWith("setValue", ["key", "value", "testDict"]);
expect(dictBlocksAPI.runCommand).toHaveBeenCalledWith("setValue", ["testDict", "key", "value", 0]);
});

test("setValue uses default dict when no dict is provided", () => {
JSInterface.validateArgs.mockReturnValue(["key", "value", "defaultDict"]);
global.globalActivity.turtles.ithTurtle.mockReturnValue({ name: "defaultDict" });

dictBlocksAPI.setValue("key", "value");

expect(JSInterface.validateArgs).toHaveBeenCalledWith("setValue", ["key", "value", "defaultDict"]);
expect(dictBlocksAPI.runCommand).toHaveBeenCalledWith("setValue", ["defaultDict", "key", "value", 0]);
});

test("getValue calls runCommand with correct arguments", () => {
JSInterface.validateArgs.mockReturnValue(["key", "testDict"]);

dictBlocksAPI.getValue("key", "testDict");

expect(JSInterface.validateArgs).toHaveBeenCalledWith("getValue", ["key", "testDict"]);
expect(dictBlocksAPI.runCommand).toHaveBeenCalledWith("getValue", ["testDict", "key", 0]);
});

test("getValue uses default dict when no dict is provided", () => {
JSInterface.validateArgs.mockReturnValue(["key", "defaultDict"]);
global.globalActivity.turtles.ithTurtle.mockReturnValue({ name: "defaultDict" });

dictBlocksAPI.getValue("key");

expect(JSInterface.validateArgs).toHaveBeenCalledWith("getValue", ["key", "defaultDict"]);
expect(dictBlocksAPI.runCommand).toHaveBeenCalledWith("getValue", ["defaultDict", "key", 0]);
});
});
60 changes: 60 additions & 0 deletions js/js-export/API/__tests__/IntervalsBlocksAPI.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const JSInterface = {
validateArgs: jest.fn(),
};
global.JSInterface = JSInterface;
const IntervalsBlocksAPI = require('../IntervalsBlocksAPI');

const MusicBlocks = { BLK: 'mockedBlock' };
global.MusicBlocks = MusicBlocks;

describe('IntervalsBlocksAPI', () => {
let intervalsBlocksAPI;

beforeEach(() => {
intervalsBlocksAPI = new IntervalsBlocksAPI();
intervalsBlocksAPI.turIndex = 1;
intervalsBlocksAPI.ENDFLOWCOMMAND = 'end';
intervalsBlocksAPI.runCommand = jest.fn();
});

test('setKey calls runCommand with validated arguments', () => {
JSInterface.validateArgs.mockReturnValue(['C', 'major']);
intervalsBlocksAPI.setKey('C', 'major');
expect(JSInterface.validateArgs).toHaveBeenCalledWith('setKey', ['C', 'major']);
expect(intervalsBlocksAPI.runCommand).toHaveBeenCalledWith('setKey', ['C', 'major', 1]);
});

test('defineMode calls runCommand and awaits flow', async () => {
const flow = jest.fn();
JSInterface.validateArgs.mockReturnValue(['dorian', flow]);
await intervalsBlocksAPI.defineMode('dorian', flow);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('defineMode', ['dorian', flow]);
expect(intervalsBlocksAPI.runCommand).toHaveBeenCalledWith('defineMode', ['dorian', 1, 'mockedBlock']);
expect(flow).toHaveBeenCalled();
});

test('setScalarInterval calls runCommand and awaits flow', async () => {
const flow = jest.fn();
JSInterface.validateArgs.mockReturnValue([5, flow]);
await intervalsBlocksAPI.setScalarInterval(5, flow);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('setScalarInterval', [5, flow]);
expect(intervalsBlocksAPI.runCommand).toHaveBeenCalledWith('setScalarInterval', [5, 1]);
expect(flow).toHaveBeenCalled();
});

test('setSemitoneInterval calls runCommand and awaits flow', async () => {
const flow = jest.fn();
JSInterface.validateArgs.mockReturnValue([7, flow]);
await intervalsBlocksAPI.setSemitoneInterval(7, flow);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('setSemitoneInterval', [7, flow]);
expect(intervalsBlocksAPI.runCommand).toHaveBeenCalledWith('setSemitoneInterval', [7, 1]);
expect(flow).toHaveBeenCalled();
});

test('setTemperament calls runCommand with validated arguments', () => {
JSInterface.validateArgs.mockReturnValue(['equal', 440, 4]);
intervalsBlocksAPI.setTemperament('equal', 440, 4);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('setTemperament', ['equal', 440, 4]);
expect(intervalsBlocksAPI.runCommand).toHaveBeenCalledWith('setTemperament', ['equal', 440, 4]);
});
});
88 changes: 88 additions & 0 deletions js/js-export/API/__tests__/MeterBlocksAPI.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@

const JSInterface = {
validateArgs: jest.fn(),
};
global.JSInterface = JSInterface;
const MeterBlocksAPI = require('../MeterBlocksAPI');
const Singer = { MeterActions: { getNotesPlayed: jest.fn() } };
global.Singer = Singer;

describe('MeterBlocksAPI', () => {
let meterBlocksAPI;

beforeEach(() => {
meterBlocksAPI = new MeterBlocksAPI();
meterBlocksAPI.turIndex = 1;
meterBlocksAPI.ENDFLOWCOMMAND = 'end';
meterBlocksAPI.runCommand = jest.fn();
});

test('setMeter calls runCommand with validated arguments', () => {
JSInterface.validateArgs.mockReturnValue([4, 4]);
meterBlocksAPI.setMeter(4, 4);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('setMeter', [4, 4]);
expect(meterBlocksAPI.runCommand).toHaveBeenCalledWith('setMeter', [4, 4, 1]);
});

test('setBPM calls runCommand with validated arguments', () => {
JSInterface.validateArgs.mockReturnValue([120, 4]);
meterBlocksAPI.setBPM(120, 4);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('setBPM', [120, 4]);
expect(meterBlocksAPI.runCommand).toHaveBeenCalledWith('setBPM', [120, 4, 1]);
});

test('setMasterBPM calls runCommand with validated arguments', () => {
JSInterface.validateArgs.mockReturnValue([100, 4]);
meterBlocksAPI.setMasterBPM(100, 4);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('setMasterBPM', [100, 4]);
expect(meterBlocksAPI.runCommand).toHaveBeenCalledWith('setMasterBPM', [100, 4]);
});

test('onEveryNoteDo calls runCommand with validated arguments', () => {
const action = jest.fn();
JSInterface.validateArgs.mockReturnValue([action]);
meterBlocksAPI.onEveryNoteDo(action);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('onEveryNoteDo', [action]);
expect(meterBlocksAPI.runCommand).toHaveBeenCalledWith('onEveryNoteDo', [action, null, null, 1]);
});

test('onEveryBeatDo calls runCommand with validated arguments', () => {
const action = jest.fn();
JSInterface.validateArgs.mockReturnValue([action]);
meterBlocksAPI.onEveryBeatDo(action);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('onEveryBeatDo', [action]);
expect(meterBlocksAPI.runCommand).toHaveBeenCalledWith('onEveryBeatDo', [action, null, null, 1]);
});

test('onStrongBeatDo calls runCommand with validated arguments', () => {
const action = jest.fn();
JSInterface.validateArgs.mockReturnValue([2, action]);
meterBlocksAPI.onStrongBeatDo(2, action);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('onStrongBeatDo', [2, action]);
expect(meterBlocksAPI.runCommand).toHaveBeenCalledWith('onStrongBeatDo', [2, action, null, null, 1]);
});

test('onWeakBeatDo calls runCommand with validated arguments', () => {
const action = jest.fn();
JSInterface.validateArgs.mockReturnValue([action]);
meterBlocksAPI.onWeakBeatDo(action);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('onWeakBeatDo', [action]);
expect(meterBlocksAPI.runCommand).toHaveBeenCalledWith('onWeakBeatDo', [action, null, null, 1]);
});

test('setNoClock calls runCommand and awaits flow', async () => {
const flow = jest.fn();
JSInterface.validateArgs.mockReturnValue([flow]);
await meterBlocksAPI.setNoClock(flow);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('setNoClock', [flow]);
expect(meterBlocksAPI.runCommand).toHaveBeenCalledWith('setNoClock', [1]);
expect(flow).toHaveBeenCalled();
});

test('getNotesPlayed calls Singer.MeterActions.getNotesPlayed with validated arguments', () => {
JSInterface.validateArgs.mockReturnValue([4]);
meterBlocksAPI.getNotesPlayed(4);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('getNotesPlayed', [4]);
expect(Singer.MeterActions.getNotesPlayed).toHaveBeenCalledWith(4, 1);
});
});
111 changes: 111 additions & 0 deletions js/js-export/API/__tests__/PenBlocksAPI.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const JSInterface = {
validateArgs: jest.fn(),
};
global.JSInterface = JSInterface;
const PenBlocksAPI = require('../PenBlocksAPI')
global.globalActivity = {
turtles: {
refreshCanvas: jest.fn(),
},
logo: {
turtles: {
setBackgroundColor: jest.fn(),
},
},
};

describe('PenBlocksAPI', () => {
let penBlocksAPI;

beforeEach(() => {
penBlocksAPI = new PenBlocksAPI();
penBlocksAPI.turIndex = 1;
penBlocksAPI.ENDFLOW = 'end';
penBlocksAPI.runCommand = jest.fn();
});

test('setColor calls runCommand with validated arguments', () => {
JSInterface.validateArgs.mockReturnValue([50]);
penBlocksAPI.setColor(50);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('setColor', [50]);
expect(penBlocksAPI.runCommand).toHaveBeenCalledWith('doSetColor', [50]);
});

test('setGrey calls runCommand with validated arguments', () => {
JSInterface.validateArgs.mockReturnValue([30]);
penBlocksAPI.setGrey(30);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('setGrey', [30]);
expect(penBlocksAPI.runCommand).toHaveBeenCalledWith('doSetChroma', [30]);
});

test('setShade calls runCommand with validated arguments', () => {
JSInterface.validateArgs.mockReturnValue([70]);
penBlocksAPI.setShade(70);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('setShade', [70]);
expect(penBlocksAPI.runCommand).toHaveBeenCalledWith('doSetValue', [70]);
});

test('setHue calls runCommand with validated arguments', () => {
JSInterface.validateArgs.mockReturnValue([120]);
penBlocksAPI.setHue(120);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('setHue', [120]);
expect(penBlocksAPI.runCommand).toHaveBeenCalledWith('doSetHue', [120]);
});

test('setTranslucency calculates and calls runCommand with validated arguments', () => {
JSInterface.validateArgs.mockReturnValue([80]);
penBlocksAPI.setTranslucency(80);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('setTranslucency', [80]);
expect(penBlocksAPI.runCommand).toHaveBeenCalledWith('doSetPenAlpha', [1.0 - 80 / 100]);
});

test('setPensize calls runCommand with validated arguments', () => {
JSInterface.validateArgs.mockReturnValue([10]);
penBlocksAPI.setPensize(10);
expect(JSInterface.validateArgs).toHaveBeenCalledWith('setPensize', [10]);
expect(penBlocksAPI.runCommand).toHaveBeenCalledWith('doSetPensize', [10]);
});

test('penUp calls runCommand', () => {
penBlocksAPI.penUp();
expect(penBlocksAPI.runCommand).toHaveBeenCalledWith('doPenUp');
});

test('penDown calls runCommand', () => {
penBlocksAPI.penDown();
expect(penBlocksAPI.runCommand).toHaveBeenCalledWith('doPenDown');
});

test('fillShape calls commands and refreshCanvas', async () => {
const flow = jest.fn();
await penBlocksAPI.fillShape(flow);
expect(penBlocksAPI.runCommand).toHaveBeenCalledWith('doStartFill');
expect(flow).toHaveBeenCalled();
expect(penBlocksAPI.runCommand).toHaveBeenCalledWith('doEndFill');
expect(global.globalActivity.turtles.refreshCanvas).toHaveBeenCalled();
});

test('hollowLine calls commands and refreshCanvas', async () => {
const flow = jest.fn();
await penBlocksAPI.hollowLine(flow);
expect(penBlocksAPI.runCommand).toHaveBeenCalledWith('doStartHollowLine');
expect(flow).toHaveBeenCalled();
expect(penBlocksAPI.runCommand).toHaveBeenCalledWith('doEndHollowLine');
expect(global.globalActivity.turtles.refreshCanvas).toHaveBeenCalled();
});

test('fillBackground calls setBackgroundColor', () => {
penBlocksAPI.fillBackground();
expect(penBlocksAPI.runCommand).toHaveBeenCalledWith('_anonymous', expect.any(Function));
const setBackgroundFunc = penBlocksAPI.runCommand.mock.calls[0][1];
setBackgroundFunc();
expect(global.globalActivity.logo.turtles.setBackgroundColor).toHaveBeenCalledWith(1);
});

test('setFont calls runCommand with validated arguments', () => {
JSInterface.validateArgs.mockReturnValue(['Arial']);
penBlocksAPI.setFont('Arial');
expect(JSInterface.validateArgs).toHaveBeenCalledWith('setFont', ['Arial']);
expect(penBlocksAPI.runCommand).toHaveBeenCalledWith('doSetFont', ['Arial']);
});
});
Loading

0 comments on commit a65a865

Please sign in to comment.