From 38701688b89be6da802637dc2b724c4527b73f00 Mon Sep 17 00:00:00 2001 From: Michael Kauzmann Date: Tue, 25 Oct 2022 14:58:22 -0600 Subject: [PATCH] create KeyboardListenerTests.ts, https://github.com/phetsims/scenery/issues/1445 --- js/listeners/KeyboardListenerTests.ts | 52 +++++++++++++++++++++++++++ js/scenery-tests.js | 1 + 2 files changed, 53 insertions(+) create mode 100644 js/listeners/KeyboardListenerTests.ts diff --git a/js/listeners/KeyboardListenerTests.ts b/js/listeners/KeyboardListenerTests.ts new file mode 100644 index 000000000..1393d2ac9 --- /dev/null +++ b/js/listeners/KeyboardListenerTests.ts @@ -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(); +} ); \ No newline at end of file diff --git a/js/scenery-tests.js b/js/scenery-tests.js index 9670916e5..04615a518 100644 --- a/js/scenery-tests.js +++ b/js/scenery-tests.js @@ -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';