Skip to content

Commit

Permalink
create KeyboardListenerTests.ts, #1445
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Oct 25, 2022
1 parent 18a1f09 commit 3870168
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
52 changes: 52 additions & 0 deletions js/listeners/KeyboardListenerTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2018-2021, University of Colorado Boulder

/**
* KeyboardListener tests.
*
* @author Jesse Greenberg (PhET Interactive Simulations)
* @author Michael Kauzmann (PhET Interactive Simulations)
* @author Agustín Vallejo (PhET Interactive Simulations)
*/

import { KeyboardListener, Node, Display, KeyboardUtils } from '../imports.js';

QUnit.module( 'KeyboardListener' );

QUnit.test( 'Basics', assert => {

let callbackFired = false;
const listener = new KeyboardListener( {
keys: [ 'enter' ],
callback: () => {
assert.ok( !callbackFired, 'callback cannot be fired' );
callbackFired = true;
}
} );

const rootNode = new Node( { tagName: 'div' } );
const display = new Display( rootNode );
display.initializeEvents();
document.body.appendChild( display.domElement );
const a = new Node( { tagName: 'div' } );
rootNode.addChild( a );
a.addInputListener( listener );

const domElement = a.pdomInstances[ 0 ].peer.primarySibling;
assert.ok( domElement, 'pdom element needed' );

domElement.dispatchEvent( new KeyboardEvent( 'keydown', {
code: KeyboardUtils.KEY_TAB,
bubbles: true
} ) );

assert.ok( !callbackFired, 'should not fire on tab' );

domElement.dispatchEvent( new KeyboardEvent( 'keydown', {
code: KeyboardUtils.KEY_ENTER,
bubbles: true
} ) );
assert.ok( callbackFired, 'should fire on enter' );

document.body.removeChild( display.domElement );
display.dispose();
} );
1 change: 1 addition & 0 deletions js/scenery-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import './layout/nodes/AlignBoxTests.js';
import './listeners/DragListenerTests.js';
import './listeners/FireListenerTests.js';
import './listeners/PressListenerTests.js';
import './listeners/KeyboardListenerTests.js';
import './nodes/NodeTests.js';
import './nodes/RichTextTests.js';
import './nodes/ShapeTests.js';
Expand Down

0 comments on commit 3870168

Please sign in to comment.