Skip to content

Commit

Permalink
Add computer keyboard tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zxqx committed Jun 18, 2018
1 parent fb23920 commit 2cd51e9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/helpers/keyboardTracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const NOTE_HOLD_DELAY_TIME = 500;
* @param {string} note
*/
export function triggerOnPressCallbacks(note) {
if (!note) return;

if (!Array.isArray(this.notesBeingHeld)) {
this.notesBeingHeld = [];
}
Expand Down
4 changes: 3 additions & 1 deletion src/modules/ComputerKeyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ export default class ComputerKeyboard {
if (octaveChangeDirection === OCTAVE_UP) {
if (this.octave === 8) return;
this.setOctave(this.octave + 1);
} else if (octaveChangeDirection === OCTAVE_DOWN) {
}

if (octaveChangeDirection === OCTAVE_DOWN) {
if (this.octave === 1) return;
this.setOctave(this.octave - 1);
}
Expand Down
18 changes: 18 additions & 0 deletions src/modules/ComputerKeyboard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,23 @@ describe('ComputerKeyboard', () => {
expect(kb.justReleased).toEqual(false);
});

it('should not re-trigger on press callbacks if note is being held', () => {
const kb = new ComputerKeyboard();

const callback = jest.fn();

kb.triggerOnPress([
callback,
]);

kb.triggerOnPressCallbacks('C#2');
kb.triggerOnPressCallbacks('C#2');
kb.triggerOnPressCallbacks('C#2');

expect(callback.mock.calls).toHaveLength(1);
});


it('should trigger on release callbacks', () => {
const kb = new ComputerKeyboard();

Expand Down Expand Up @@ -215,6 +232,7 @@ describe('ComputerKeyboard', () => {
expect(kb.octave).toEqual(2);

kb.triggerOctaveChange('octaveDown');
expect(kb.octave).toEqual(1);
kb.triggerOctaveChange('octaveDown');
kb.triggerOctaveChange('octaveDown');
kb.triggerOctaveChange('octaveDown');
Expand Down

0 comments on commit 2cd51e9

Please sign in to comment.