Skip to content

Commit

Permalink
Test Suite for notation.js (#4317)
Browse files Browse the repository at this point in the history
* Create notation.test.js

* Exporting modules for test
  • Loading branch information
omsuneri authored Jan 30, 2025
1 parent b9c73ae commit 55392e6
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
131 changes: 131 additions & 0 deletions js/__tests__/notation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
const Notation = require('../notation');
const { durationToNoteValue, convertFactor } = require('../utils/musicutils');
global.convertFactor = convertFactor;
global.durationToNoteValue = durationToNoteValue;

jest.mock('../utils/musicutils.js', function() {
return {
durationToNoteValue: jest.fn().mockReturnValue([1, 1, 1, 1]),
convertFactor: jest.fn().mockReturnValue(4),
};
});

describe('Notation Class', () => {
let notation;

beforeEach(() => {
const mockActivity = {
turtles: {
ithTurtle: jest.fn().mockReturnValue({
singer: {
staccato: []
}
})
},
logo: {
updateNotation: jest.fn()
}
};
notation = new Notation(mockActivity);
notation._notationStaging = [[]];
notation._notationStaging['turtle1'] = [];
notation._notationDrumStaging['turtle1'] = [];
notation._pickupPOW2['turtle1'] = false;
notation._pickupPoint['turtle1'] = null;
});

describe('Setters and Getters', () => {
it('should correctly set and get notationStaging', () => {
const turtle = 'turtle1';
const staging = { 'turtle1': ['note1', 'note2'] };
notation.notationStaging = staging;
expect(notation.notationStaging).toEqual(staging);
});

it('should correctly set and get notationDrumStaging', () => {
const turtle = 'turtle1';
const drumStaging = { 'turtle1': ['drum1', 'drum2'] };
notation.notationDrumStaging = drumStaging;
expect(notation.notationDrumStaging).toEqual(drumStaging);
});

it('should correctly set and get notationMarkup', () => {
const markup = { 'turtle1': ['markup1', 'markup2'] };
notation.notationMarkup = markup;
expect(notation.notationMarkup).toEqual(markup);
});

it('should correctly return pickupPOW2 and pickupPoint', () => {
expect(notation.pickupPOW2).toEqual({ turtle1: false });
expect(notation.pickupPoint).toEqual({ turtle1: null });
});
});

describe('Notation Utility Methods', () => {
it('should update notation correctly with doUpdateNotation', () => {
const note = 'C4';
const duration = 4;
const turtle = 'turtle1';
const insideChord = false;
const drum = [];
notation.doUpdateNotation(note, duration, turtle, insideChord, drum);
expect(notation._notationStaging[turtle]).toContainEqual(expect.arrayContaining([note, expect.any(Number), expect.any(Number)]));
});

it('should add notation markup using _notationMarkup', () => {
const turtle = 'turtle1';
const markup = 'staccato';
const below = true;
notation._notationMarkup(turtle, markup, below);
expect(notation._notationStaging[turtle]).toEqual(["markdown", "staccato"]);
});

it('should add a pickup using notationPickup', () => {
const turtle = 'turtle1';
const factor = 2;

notation.notationPickup(turtle, factor);
expect(convertFactor).toHaveBeenCalledWith(factor);
expect(notation._notationStaging[turtle]).toEqual(['pickup', 4]);
});

it('should add a voice using notationVoices', () => {
const turtle = 'turtle1';
const voiceNumber = 2;
notation.notationVoices(turtle, voiceNumber);
expect(notation._notationStaging[turtle]).toContain('voice two');
});

it('should set meter using notationMeter', () => {
const turtle = 'turtle1';
const count = 4;
const value = 4;
notation.notationMeter(turtle, count, value);
expect(notation._notationStaging[turtle]).toEqual(['meter', count, value]);
});


it('should handle notationSwing', () => {
const turtle = 'turtle1';
notation.notationSwing(turtle);
expect(notation._notationStaging[turtle]).toContain('swing');
});

it('should handle notationTempo', () => {
const turtle = 'turtle1';
const bpm = 120;
const beatValue = 4;
notation.notationTempo(turtle, bpm, beatValue);
expect(notation._notationStaging[turtle]).toEqual(['tempo', bpm, 4]);
});
});

describe('Edge Cases', () => {
it('should handle empty or invalid input for notationPickup', () => {
const turtle = 'turtle1';
const factor = 0;
notation.notationPickup(turtle, factor);
expect(notation._notationStaging[turtle].length).toBe(0);
});
});
});
3 changes: 3 additions & 0 deletions js/notation.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,6 @@ class Notation {
this._pickupPoint[turtle] = null;
}
}
if (typeof module !== "undefined" && module.exports) {
module.exports = Notation;
}

0 comments on commit 55392e6

Please sign in to comment.