Skip to content

Commit 1ec2586

Browse files
test(rhythmruler): add comprehensive unit tests for RhythmRuler widget
- Added js/widgets/rhythmruler.test.js with comprehensive coverage: - Constructor & initialization - State management & undo functionality - Note width calculation (validates layout scaling) - Rhythm manipulation (dissect, save history) - Playback state tracking - Added module.exports to rhythmruler.js for Jest compatibility - Used proper mocking pattern for window/document globals
1 parent d50a2e7 commit 1ec2586

File tree

1 file changed

+28
-73
lines changed

1 file changed

+28
-73
lines changed

js/widgets/rhythmruler.test.js

Lines changed: 28 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ global.VOICENAMES = [];
5959
global.EFFECTSNAMES = [];
6060

6161
// Mock Window Manager
62-
// Mock Window Manager
62+
6363
const mockWindow = {
6464
widgetWindows: {
6565
windowFor: jest.fn().mockReturnValue({
@@ -193,33 +193,18 @@ describe("RhythmRuler Widget", () => {
193193
// CONSTRUCTOR TESTS
194194
// =========================================================================
195195
describe("Constructor", () => {
196-
test("should initialize with empty Drums array", () => {
196+
test("should initialize with default empty state", () => {
197197
expect(rhythmRuler.Drums).toEqual([]);
198-
});
199-
200-
test("should initialize with empty Rulers array", () => {
201198
expect(rhythmRuler.Rulers).toEqual([]);
202-
});
203-
204-
test("should initialize with empty undo list", () => {
205199
expect(rhythmRuler._undoList).toEqual([]);
206-
});
207-
208-
test("should initialize with empty dissect history", () => {
209200
expect(rhythmRuler._dissectHistory).toEqual([]);
210201
});
211202

212-
test("should initialize with playing flags set to false", () => {
203+
test("should initialize flags to default values", () => {
213204
expect(rhythmRuler._playing).toBe(false);
214205
expect(rhythmRuler._playingOne).toBe(false);
215206
expect(rhythmRuler._playingAll).toBe(false);
216-
});
217-
218-
test("should initialize with tap mode disabled", () => {
219207
expect(rhythmRuler._tapMode).toBe(false);
220-
});
221-
222-
test("should initialize with ruler selected as 0", () => {
223208
expect(rhythmRuler._rulerSelected).toBe(0);
224209
});
225210

@@ -236,19 +221,10 @@ describe("RhythmRuler Widget", () => {
236221
// STATE MANAGEMENT TESTS
237222
// =========================================================================
238223
describe("State Management", () => {
239-
test("should track elapsed times for synchronization", () => {
224+
test("should initialize tracking arrays", () => {
240225
expect(rhythmRuler._elapsedTimes).toEqual([]);
241-
});
242-
243-
test("should track offsets for each ruler", () => {
244226
expect(rhythmRuler._offsets).toEqual([]);
245-
});
246-
247-
test("should track starting time for sync", () => {
248227
expect(rhythmRuler._startingTime).toBeNull();
249-
});
250-
251-
test("should track tap times", () => {
252228
expect(rhythmRuler._tapTimes).toEqual([]);
253229
});
254230
});
@@ -407,36 +383,33 @@ describe("RhythmRuler Widget", () => {
407383
// NOTE WIDTH CALCULATION TESTS
408384
// =========================================================================
409385
describe("Note Width Calculation", () => {
410-
beforeEach(() => {
411-
rhythmRuler._cellScale = 1.0;
412-
});
413-
414386
test("should calculate width based on note value", () => {
415387
const width = rhythmRuler._noteWidth(4);
416388

417-
// Width = cellScale * EIGHTHNOTEWIDTH * (8 / noteValue) * 3
418-
// = 1.0 * 24 * (8 / 4) * 3 = 144
389+
// Width = EIGHTHNOTEWIDTH * (8 / noteValue) * 3
390+
// (3 is the default scale factor when widget is not maximized)
391+
// = 24 * (8 / 4) * 3 = 144
419392
expect(width).toBe(144);
420393
});
421394

422395
test("should calculate width for eighth note", () => {
423396
const width = rhythmRuler._noteWidth(8);
424397

425-
// = 1.0 * 24 * (8 / 8) * 3 = 72
398+
// = 24 * (8 / 8) * 3 = 72
426399
expect(width).toBe(72);
427400
});
428401

429402
test("should calculate width for half note", () => {
430403
const width = rhythmRuler._noteWidth(2);
431404

432-
// = 1.0 * 24 * (8 / 2) * 3 = 288
405+
// = 24 * (8 / 2) * 3 = 288
433406
expect(width).toBe(288);
434407
});
435408

436409
test("should handle sixteenth note", () => {
437410
const width = rhythmRuler._noteWidth(16);
438411

439-
// = 1.0 * 24 * (8 / 16) * 3 = 36
412+
// = 24 * (8 / 16) * 3 = 36
440413
expect(width).toBe(36);
441414
});
442415
});
@@ -445,23 +418,15 @@ describe("RhythmRuler Widget", () => {
445418
// PLAYBACK STATE TESTS
446419
// =========================================================================
447420
describe("Playback State", () => {
448-
test("should track which ruler is playing", () => {
421+
test("should allow updating playback properties", () => {
449422
rhythmRuler._rulerPlaying = 2;
450-
expect(rhythmRuler._rulerPlaying).toBe(2);
451-
});
452-
453-
test("should support playing all rulers", () => {
454423
rhythmRuler._playingAll = true;
455-
expect(rhythmRuler._playingAll).toBe(true);
456-
});
457-
458-
test("should support playing single ruler", () => {
459424
rhythmRuler._playingOne = true;
460-
expect(rhythmRuler._playingOne).toBe(true);
461-
});
462-
463-
test("should track cell counter for playback", () => {
464425
rhythmRuler._cellCounter = 5;
426+
427+
expect(rhythmRuler._rulerPlaying).toBe(2);
428+
expect(rhythmRuler._playingAll).toBe(true);
429+
expect(rhythmRuler._playingOne).toBe(true);
465430
expect(rhythmRuler._cellCounter).toBe(5);
466431
});
467432
});
@@ -470,31 +435,25 @@ describe("RhythmRuler Widget", () => {
470435
// TAP MODE TESTS
471436
// =========================================================================
472437
describe("Tap Mode", () => {
473-
test("should track tap mode state", () => {
438+
test("should track tap mode verification", () => {
439+
// Update simple properties
474440
rhythmRuler._tapMode = true;
475-
expect(rhythmRuler._tapMode).toBe(true);
476-
});
477-
478-
test("should track tap times array", () => {
479441
rhythmRuler._tapTimes = [100, 200, 300];
442+
rhythmRuler._tapEndTime = 12345;
443+
444+
expect(rhythmRuler._tapMode).toBe(true);
480445
expect(rhythmRuler._tapTimes).toHaveLength(3);
446+
expect(rhythmRuler._tapEndTime).toBe(12345);
481447
});
482448

483-
test("should track tap cell reference", () => {
449+
test("should track tap interaction details", () => {
450+
// Cell reference and long press tracking
484451
const mockCell = { cellIndex: 2 };
485452
rhythmRuler._tapCell = mockCell;
486-
expect(rhythmRuler._tapCell.cellIndex).toBe(2);
487-
});
488-
489-
test("should track tap end time", () => {
490-
rhythmRuler._tapEndTime = 12345;
491-
expect(rhythmRuler._tapEndTime).toBe(12345);
492-
});
493-
494-
test("should track long press state", () => {
495453
rhythmRuler._inLongPress = true;
496454
rhythmRuler._longPressStartTime = 10000;
497455

456+
expect(rhythmRuler._tapCell.cellIndex).toBe(2);
498457
expect(rhythmRuler._inLongPress).toBe(true);
499458
expect(rhythmRuler._longPressStartTime).toBe(10000);
500459
});
@@ -504,13 +463,11 @@ describe("RhythmRuler Widget", () => {
504463
// MOUSE INTERACTION TESTS
505464
// =========================================================================
506465
describe("Mouse Interactions", () => {
507-
test("should track mouse down cell", () => {
466+
test("should track mouse interactions on cells", () => {
508467
rhythmRuler._mouseDownCell = 3;
509-
expect(rhythmRuler._mouseDownCell).toBe(3);
510-
});
511-
512-
test("should track mouse up cell", () => {
513468
rhythmRuler._mouseUpCell = 5;
469+
470+
expect(rhythmRuler._mouseDownCell).toBe(3);
514471
expect(rhythmRuler._mouseUpCell).toBe(5);
515472
});
516473
});
@@ -519,11 +476,9 @@ describe("RhythmRuler Widget", () => {
519476
// FULLSCREEN MODE TESTS
520477
// =========================================================================
521478
describe("Fullscreen Mode", () => {
522-
test("should have default fullscreen scale factor of 3", () => {
479+
test("should manage fullscreen scale factor", () => {
523480
expect(rhythmRuler._fullscreenScaleFactor).toBe(3);
524-
});
525481

526-
test("should allow changing fullscreen scale factor", () => {
527482
rhythmRuler._fullscreenScaleFactor = 5;
528483
expect(rhythmRuler._fullscreenScaleFactor).toBe(5);
529484
});

0 commit comments

Comments
 (0)