Skip to content

Commit

Permalink
Creating test for the IntervalsBlocksAPI.js
Browse files Browse the repository at this point in the history
  • Loading branch information
omsuneri authored Dec 29, 2024
1 parent 48479c1 commit 339e799
Showing 1 changed file with 60 additions and 0 deletions.
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]);
});
});

0 comments on commit 339e799

Please sign in to comment.