From 009b300eae2f9e90c763cde829d257a9393e3e8d Mon Sep 17 00:00:00 2001 From: Justin Charles <143245796+justin212407@users.noreply.github.com> Date: Sat, 4 Jan 2025 23:02:27 +0530 Subject: [PATCH] Added test files for GraphicBlocksAPI, VolumeBlocksAPI, OrnamentBlocksAPI (#4209) Signed-off-by: Justin Charles --- js/js-export/API/GraphicsBlocksAPI.js | 1 + js/js-export/API/OrnamentBlocksAPI.js | 1 + js/js-export/API/VolumeBlocksAPI.js | 1 + .../API/__tests__/GraphicsBlocksAPI.test.js | 148 ++++++++++++++++++ .../API/__tests__/OrnamentBlocksAPI.test.js | 62 ++++++++ .../API/__tests__/VolumeBlocksAPI.test.js | 85 ++++++++++ 6 files changed, 298 insertions(+) create mode 100644 js/js-export/API/__tests__/GraphicsBlocksAPI.test.js create mode 100644 js/js-export/API/__tests__/OrnamentBlocksAPI.test.js create mode 100644 js/js-export/API/__tests__/VolumeBlocksAPI.test.js diff --git a/js/js-export/API/GraphicsBlocksAPI.js b/js/js-export/API/GraphicsBlocksAPI.js index 07266f47f7..5ef523d182 100644 --- a/js/js-export/API/GraphicsBlocksAPI.js +++ b/js/js-export/API/GraphicsBlocksAPI.js @@ -86,3 +86,4 @@ class GraphicsBlocksAPI { return this.runCommand("doScrollXY", [args[0], args[1]]); } } +module.exports = GraphicsBlocksAPI; \ No newline at end of file diff --git a/js/js-export/API/OrnamentBlocksAPI.js b/js/js-export/API/OrnamentBlocksAPI.js index 40daca6e0b..1fad1f7468 100644 --- a/js/js-export/API/OrnamentBlocksAPI.js +++ b/js/js-export/API/OrnamentBlocksAPI.js @@ -48,3 +48,4 @@ class OrnamentBlocksAPI { return this.ENDFLOWCOMMAND; } } +module.exports = OrnamentBlocksAPI; \ No newline at end of file diff --git a/js/js-export/API/VolumeBlocksAPI.js b/js/js-export/API/VolumeBlocksAPI.js index cf29bfc7b3..072ff8bea9 100644 --- a/js/js-export/API/VolumeBlocksAPI.js +++ b/js/js-export/API/VolumeBlocksAPI.js @@ -58,3 +58,4 @@ class VolumeBlocksAPI { return Singer.VolumeActions.getSynthVolume(args[0], this.turIndex); } } +module.exports = VolumeBlocksAPI; \ No newline at end of file diff --git a/js/js-export/API/__tests__/GraphicsBlocksAPI.test.js b/js/js-export/API/__tests__/GraphicsBlocksAPI.test.js new file mode 100644 index 0000000000..af16340b04 --- /dev/null +++ b/js/js-export/API/__tests__/GraphicsBlocksAPI.test.js @@ -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]); + }); + }); \ No newline at end of file diff --git a/js/js-export/API/__tests__/OrnamentBlocksAPI.test.js b/js/js-export/API/__tests__/OrnamentBlocksAPI.test.js new file mode 100644 index 0000000000..d2034f9d09 --- /dev/null +++ b/js/js-export/API/__tests__/OrnamentBlocksAPI.test.js @@ -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"); + }); +}); diff --git a/js/js-export/API/__tests__/VolumeBlocksAPI.test.js b/js/js-export/API/__tests__/VolumeBlocksAPI.test.js new file mode 100644 index 0000000000..4045c0370b --- /dev/null +++ b/js/js-export/API/__tests__/VolumeBlocksAPI.test.js @@ -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); + }); +});