Skip to content

Commit 55392e6

Browse files
authored
Test Suite for notation.js (#4317)
* Create notation.test.js * Exporting modules for test
1 parent b9c73ae commit 55392e6

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

js/__tests__/notation.test.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
const Notation = require('../notation');
2+
const { durationToNoteValue, convertFactor } = require('../utils/musicutils');
3+
global.convertFactor = convertFactor;
4+
global.durationToNoteValue = durationToNoteValue;
5+
6+
jest.mock('../utils/musicutils.js', function() {
7+
return {
8+
durationToNoteValue: jest.fn().mockReturnValue([1, 1, 1, 1]),
9+
convertFactor: jest.fn().mockReturnValue(4),
10+
};
11+
});
12+
13+
describe('Notation Class', () => {
14+
let notation;
15+
16+
beforeEach(() => {
17+
const mockActivity = {
18+
turtles: {
19+
ithTurtle: jest.fn().mockReturnValue({
20+
singer: {
21+
staccato: []
22+
}
23+
})
24+
},
25+
logo: {
26+
updateNotation: jest.fn()
27+
}
28+
};
29+
notation = new Notation(mockActivity);
30+
notation._notationStaging = [[]];
31+
notation._notationStaging['turtle1'] = [];
32+
notation._notationDrumStaging['turtle1'] = [];
33+
notation._pickupPOW2['turtle1'] = false;
34+
notation._pickupPoint['turtle1'] = null;
35+
});
36+
37+
describe('Setters and Getters', () => {
38+
it('should correctly set and get notationStaging', () => {
39+
const turtle = 'turtle1';
40+
const staging = { 'turtle1': ['note1', 'note2'] };
41+
notation.notationStaging = staging;
42+
expect(notation.notationStaging).toEqual(staging);
43+
});
44+
45+
it('should correctly set and get notationDrumStaging', () => {
46+
const turtle = 'turtle1';
47+
const drumStaging = { 'turtle1': ['drum1', 'drum2'] };
48+
notation.notationDrumStaging = drumStaging;
49+
expect(notation.notationDrumStaging).toEqual(drumStaging);
50+
});
51+
52+
it('should correctly set and get notationMarkup', () => {
53+
const markup = { 'turtle1': ['markup1', 'markup2'] };
54+
notation.notationMarkup = markup;
55+
expect(notation.notationMarkup).toEqual(markup);
56+
});
57+
58+
it('should correctly return pickupPOW2 and pickupPoint', () => {
59+
expect(notation.pickupPOW2).toEqual({ turtle1: false });
60+
expect(notation.pickupPoint).toEqual({ turtle1: null });
61+
});
62+
});
63+
64+
describe('Notation Utility Methods', () => {
65+
it('should update notation correctly with doUpdateNotation', () => {
66+
const note = 'C4';
67+
const duration = 4;
68+
const turtle = 'turtle1';
69+
const insideChord = false;
70+
const drum = [];
71+
notation.doUpdateNotation(note, duration, turtle, insideChord, drum);
72+
expect(notation._notationStaging[turtle]).toContainEqual(expect.arrayContaining([note, expect.any(Number), expect.any(Number)]));
73+
});
74+
75+
it('should add notation markup using _notationMarkup', () => {
76+
const turtle = 'turtle1';
77+
const markup = 'staccato';
78+
const below = true;
79+
notation._notationMarkup(turtle, markup, below);
80+
expect(notation._notationStaging[turtle]).toEqual(["markdown", "staccato"]);
81+
});
82+
83+
it('should add a pickup using notationPickup', () => {
84+
const turtle = 'turtle1';
85+
const factor = 2;
86+
87+
notation.notationPickup(turtle, factor);
88+
expect(convertFactor).toHaveBeenCalledWith(factor);
89+
expect(notation._notationStaging[turtle]).toEqual(['pickup', 4]);
90+
});
91+
92+
it('should add a voice using notationVoices', () => {
93+
const turtle = 'turtle1';
94+
const voiceNumber = 2;
95+
notation.notationVoices(turtle, voiceNumber);
96+
expect(notation._notationStaging[turtle]).toContain('voice two');
97+
});
98+
99+
it('should set meter using notationMeter', () => {
100+
const turtle = 'turtle1';
101+
const count = 4;
102+
const value = 4;
103+
notation.notationMeter(turtle, count, value);
104+
expect(notation._notationStaging[turtle]).toEqual(['meter', count, value]);
105+
});
106+
107+
108+
it('should handle notationSwing', () => {
109+
const turtle = 'turtle1';
110+
notation.notationSwing(turtle);
111+
expect(notation._notationStaging[turtle]).toContain('swing');
112+
});
113+
114+
it('should handle notationTempo', () => {
115+
const turtle = 'turtle1';
116+
const bpm = 120;
117+
const beatValue = 4;
118+
notation.notationTempo(turtle, bpm, beatValue);
119+
expect(notation._notationStaging[turtle]).toEqual(['tempo', bpm, 4]);
120+
});
121+
});
122+
123+
describe('Edge Cases', () => {
124+
it('should handle empty or invalid input for notationPickup', () => {
125+
const turtle = 'turtle1';
126+
const factor = 0;
127+
notation.notationPickup(turtle, factor);
128+
expect(notation._notationStaging[turtle].length).toBe(0);
129+
});
130+
});
131+
});

js/notation.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,3 +502,6 @@ class Notation {
502502
this._pickupPoint[turtle] = null;
503503
}
504504
}
505+
if (typeof module !== "undefined" && module.exports) {
506+
module.exports = Notation;
507+
}

0 commit comments

Comments
 (0)