-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create KeyboardListenerTests.ts, #1445
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters