Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test files for GraphicBlocksAPI, VolumeBlocksAPI, OrnamentBlocksAPI in /js/js-exports/API #4200 #4209

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions js/js-export/API/GraphicsBlocksAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ class GraphicsBlocksAPI {
return this.runCommand("doScrollXY", [args[0], args[1]]);
}
}
module.exports = GraphicsBlocksAPI;
1 change: 1 addition & 0 deletions js/js-export/API/OrnamentBlocksAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ class OrnamentBlocksAPI {
return this.ENDFLOWCOMMAND;
}
}
module.exports = OrnamentBlocksAPI;
1 change: 1 addition & 0 deletions js/js-export/API/VolumeBlocksAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ class VolumeBlocksAPI {
return Singer.VolumeActions.getSynthVolume(args[0], this.turIndex);
}
}
module.exports = VolumeBlocksAPI;
148 changes: 148 additions & 0 deletions js/js-export/API/__tests__/GraphicsBlocksAPI.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
const JSInterface = {
validateArgs: jest.fn(),
};
global.JSInterface = JSInterface;

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

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

describe("GraphicsBlocksAPI", () => {
let graphicsBlocksAPI;

beforeEach(() => {
graphicsBlocksAPI = new GraphicsBlocksAPI();
graphicsBlocksAPI.turIndex = 0;
graphicsBlocksAPI.runCommand = jest.fn();
});

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

test("goForward calls runCommand with correct arguments", () => {
const steps = 5;
JSInterface.validateArgs.mockReturnValue([steps]);

graphicsBlocksAPI.goForward(steps);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("goForward", [steps]);
expect(graphicsBlocksAPI.runCommand).toHaveBeenCalledWith("doForward", [steps]);
});

test("goBackward calls runCommand with correct arguments", () => {
const steps = 5;
JSInterface.validateArgs.mockReturnValue([steps]);

graphicsBlocksAPI.goBackward(steps);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("goBackward", [steps]);
expect(graphicsBlocksAPI.runCommand).toHaveBeenCalledWith("doForward", [-steps]);
});

test("turnRight calls runCommand with correct arguments", () => {
const degrees = 90;
JSInterface.validateArgs.mockReturnValue([degrees]);

graphicsBlocksAPI.turnRight(degrees);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("turnRight", [degrees]);
expect(graphicsBlocksAPI.runCommand).toHaveBeenCalledWith("doRight", [degrees]);
});

test("turnLeft calls runCommand with correct arguments", () => {
const degrees = 90;
JSInterface.validateArgs.mockReturnValue([degrees]);

graphicsBlocksAPI.turnLeft(degrees);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("turnLeft", [degrees]);
expect(graphicsBlocksAPI.runCommand).toHaveBeenCalledWith("doRight", [-degrees]);
});

test("setXY calls runCommand with correct arguments", () => {
const x = 10;
const y = 20;
JSInterface.validateArgs.mockReturnValue([x, y]);

graphicsBlocksAPI.setXY(x, y);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("setXY", [x, y]);
expect(graphicsBlocksAPI.runCommand).toHaveBeenCalledWith("doSetXY", [x, y]);
});

test("setHeading calls runCommand with correct arguments", () => {
const degrees = 90;
JSInterface.validateArgs.mockReturnValue([degrees]);

graphicsBlocksAPI.setHeading(degrees);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("setHeading", [degrees]);
expect(graphicsBlocksAPI.runCommand).toHaveBeenCalledWith("doSetHeading", [degrees]);
});

test("drawArc calls runCommand with correct arguments", () => {
const degrees = 45;
const steps = 10;
JSInterface.validateArgs.mockReturnValue([degrees, steps]);

graphicsBlocksAPI.drawArc(degrees, steps);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("drawArc", [degrees, steps]);
expect(graphicsBlocksAPI.runCommand).toHaveBeenCalledWith("doArc", [degrees, steps]);
});

test("drawBezier calls runCommand with correct arguments", () => {
const x = 15;
const y = 25;
JSInterface.validateArgs.mockReturnValue([x, y]);

graphicsBlocksAPI.drawBezier(x, y);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("drawBezier", [x, y]);
expect(graphicsBlocksAPI.runCommand).toHaveBeenCalledWith("doBezier", [x, y]);
});

test("setBezierControlPoint1 calls runCommand with correct arguments", () => {
const x = 5;
const y = 10;
JSInterface.validateArgs.mockReturnValue([x, y]);

graphicsBlocksAPI.setBezierControlPoint1(x, y);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("setBezierControlPoint1", [x, y]);
expect(graphicsBlocksAPI.runCommand).toHaveBeenCalledWith("setControlPoint1", [x, y]);
});

test("setBezierControlPoint2 calls runCommand with correct arguments", () => {
const x = 10;
const y = 15;
JSInterface.validateArgs.mockReturnValue([x, y]);

graphicsBlocksAPI.setBezierControlPoint2(x, y);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("setBezierControlPoint2", [x, y]);
expect(graphicsBlocksAPI.runCommand).toHaveBeenCalledWith("setControlPoint2", [x, y]);
});

test("clear calls runCommand with correct arguments", () => {
graphicsBlocksAPI.clear();

expect(graphicsBlocksAPI.runCommand).toHaveBeenCalledWith("doClear", [true, true, true]);
});

test("scrollXY calls runCommand with correct arguments", () => {
const x = 10;
const y = 20;
JSInterface.validateArgs.mockReturnValue([x, y]);

graphicsBlocksAPI.scrollXY(x, y);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("scrollXY", [x, y]);
expect(graphicsBlocksAPI.runCommand).toHaveBeenCalledWith("doScrollXY", [x, y]);
});
});
62 changes: 62 additions & 0 deletions js/js-export/API/__tests__/OrnamentBlocksAPI.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const JSInterface = {
validateArgs: jest.fn(),
};
global.JSInterface = JSInterface;

const MusicBlocks = {
BLK: "MusicBlockTestValue",
};
global.MusicBlocks = MusicBlocks;

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

describe("OrnamentBlocksAPI", () => {
let ornamentBlocksAPI;

beforeEach(() => {
ornamentBlocksAPI = new OrnamentBlocksAPI();
ornamentBlocksAPI.turIndex = 0;
ornamentBlocksAPI.runCommand = jest.fn();
ornamentBlocksAPI.ENDFLOWCOMMAND = "endFlow";
});

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

test("setStaccato calls runCommand with correct arguments", async () => {
const mockFlow = jest.fn();
JSInterface.validateArgs.mockReturnValue([true, mockFlow]);

const result = await ornamentBlocksAPI.setStaccato(true, mockFlow);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("setStaccato", [true, mockFlow]);
expect(ornamentBlocksAPI.runCommand).toHaveBeenCalledWith("setStaccato", [true, 0]);
expect(mockFlow).toHaveBeenCalled();
expect(result).toBe("endFlow");
});

test("setSlur calls runCommand with correct arguments", async () => {
const mockFlow = jest.fn();
JSInterface.validateArgs.mockReturnValue([5, mockFlow]);

const result = await ornamentBlocksAPI.setSlur(5, mockFlow);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("setSlur", [5, mockFlow]);
expect(ornamentBlocksAPI.runCommand).toHaveBeenCalledWith("setSlur", [5, 0]);
expect(mockFlow).toHaveBeenCalled();
expect(result).toBe("endFlow");
});

test("doNeighbor calls runCommand with correct arguments", async () => {
const mockFlow = jest.fn();
JSInterface.validateArgs.mockReturnValue([2, 3, mockFlow]);

const result = await ornamentBlocksAPI.doNeighbor(2, 3, mockFlow);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("doNeighbor", [2, 3, mockFlow]);
expect(ornamentBlocksAPI.runCommand).toHaveBeenCalledWith("doNeighbor", [2, 3, 0, "MusicBlockTestValue"]);
expect(mockFlow).toHaveBeenCalled();
expect(result).toBe("endFlow");
});
});
85 changes: 85 additions & 0 deletions js/js-export/API/__tests__/VolumeBlocksAPI.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const JSInterface = {
validateArgs: jest.fn(),
};
global.JSInterface = JSInterface;

const Singer = {
VolumeActions: {
getSynthVolume: jest.fn(),
},
};
global.Singer = Singer;

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

describe("VolumeBlocksAPI", () => {
let volumeBlocksAPI;

beforeEach(() => {
volumeBlocksAPI = new VolumeBlocksAPI();
volumeBlocksAPI.turIndex = 0;
volumeBlocksAPI.runCommand = jest.fn().mockResolvedValue("Command executed");
volumeBlocksAPI.ENDFLOWCOMMAND = "endFlow";
});

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

test("doCrescendo calls runCommand with correct arguments", async () => {
const mockFlow = jest.fn();
JSInterface.validateArgs.mockReturnValue([50, mockFlow]);

const result = await volumeBlocksAPI.doCrescendo(50, mockFlow);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("doCrescendo", [50, mockFlow]);
expect(volumeBlocksAPI.runCommand).toHaveBeenCalledWith("doCrescendo", ["crescendo", 50, 0]);
expect(mockFlow).toHaveBeenCalled();
expect(result).toBe("endFlow");
});

test("doDecrescendo calls runCommand with correct arguments", async () => {
const mockFlow = jest.fn();
JSInterface.validateArgs.mockReturnValue([30, mockFlow]);

const result = await volumeBlocksAPI.doDecrescendo(30, mockFlow);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("doDecrescendo", [30, mockFlow]);
expect(volumeBlocksAPI.runCommand).toHaveBeenCalledWith("doCrescendo", ["decrescendo", 30, 0]);
expect(mockFlow).toHaveBeenCalled();
expect(result).toBe("endFlow");
});

test("setRelativeVolume calls runCommand with correct arguments", async () => {
const mockFlow = jest.fn();
JSInterface.validateArgs.mockReturnValue([75, mockFlow]);

const result = await volumeBlocksAPI.setRelativeVolume(75, mockFlow);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("setRelativeVolume", [75, mockFlow]);
expect(volumeBlocksAPI.runCommand).toHaveBeenCalledWith("setRelativeVolume", [75, 0]);
expect(mockFlow).toHaveBeenCalled();
expect(result).toBe("endFlow");
});

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

const result = volumeBlocksAPI.setSynthVolume("synth1", 100);

expect(JSInterface.validateArgs).toHaveBeenCalledWith("setSynthVolume", ["synth1", 100]);
expect(volumeBlocksAPI.runCommand).toHaveBeenCalledWith("setSynthVolume", ["synth1", 100, 0]);
expect(result).resolves.toBe("Command executed");
});

test("getSynthVolume calls Singer.VolumeActions.getSynthVolume with correct arguments", () => {
JSInterface.validateArgs.mockReturnValue(["synth1"]);
Singer.VolumeActions.getSynthVolume.mockReturnValue(80);

const result = volumeBlocksAPI.getSynthVolume("synth1");

expect(JSInterface.validateArgs).toHaveBeenCalledWith("getSynthVolume", ["synth1"]);
expect(Singer.VolumeActions.getSynthVolume).toHaveBeenCalledWith("synth1", 0);
expect(result).toBe(80);
});
});
Loading