Skip to content

Commit

Permalink
Merge pull request #379 from Turbo87/key-warnings
Browse files Browse the repository at this point in the history
Improve `triggerKeyEvent()` warnings
  • Loading branch information
cibernox authored May 25, 2018
2 parents a2c4158 + 57e85d6 commit bdbc943
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
11 changes: 11 additions & 0 deletions addon-test-support/@ember/test-helpers/-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,14 @@ export function runDestroyablesFor(object, property) {

delete object[property];
}

/**
Returns whether the passed in string consists only of numeric characters.
@private
@param {string} n input string
@returns {boolean} whether the input string consists only of numeric characters
*/
export function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
25 changes: 19 additions & 6 deletions addon-test-support/@ember/test-helpers/dom/trigger-key-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import getElement from './-get-element';
import fireEvent from './fire-event';
import settled from '../settled';
import { KEYBOARD_EVENT_TYPES } from './fire-event';
import { nextTickPromise } from '../-utils';
import { nextTickPromise, isNumeric } from '../-utils';

const DEFAULT_MODIFIERS = Object.freeze({
ctrlKey: false,
Expand All @@ -22,7 +22,7 @@ const keyFromKeyCode = {
18: 'Alt',
20: 'CapsLock',
27: 'Escape',
32: '',
32: ' ',
37: 'ArrowLeft',
38: 'ArrowUp',
39: 'ArrowRight',
Expand Down Expand Up @@ -134,16 +134,29 @@ export default function triggerKeyEvent(target, eventType, key, modifiers = DEFA
);
}

if (typeof key !== 'number' && typeof key !== 'string') {
throw new Error(`Must provide a \`key\` or \`keyCode\` to \`triggerKeyEvent\``);
}
let props;
if (typeof key === 'number') {
props = { keyCode: key, which: key, key: keyFromKeyCodeAndModifiers(key, modifiers) };
} else {
} else if (typeof key === 'string' && key.length !== 0) {
let firstCharacter = key[0];
if (firstCharacter !== firstCharacter.toUpperCase()) {
throw new Error(
`Must provide a \`key\` to \`triggerKeyEvent\` that starts with an uppercase character but you passed \`${key}\`.`
);
}

if (isNumeric(key)) {
throw new Error(
`Must provide a numeric \`keyCode\` to \`triggerKeyEvent\` but you passed \`${key}\` as a string.`
);
}

let keyCode = keyCodeFromKey(key);
props = { keyCode, which: keyCode, key };
} else {
throw new Error(`Must provide a \`key\` or \`keyCode\` to \`triggerKeyEvent\``);
}

let options = merge(props, modifiers);

fireEvent(element, eventType, options);
Expand Down
41 changes: 37 additions & 4 deletions tests/unit/dom/trigger-key-event-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,39 @@ module('DOM Helper: triggerKeyEvent', function(hooks) {
);
});

test('rejects if empty string is passed in', async function(assert) {
element = buildInstrumentedElement('div');

await setupContext(context);

assert.rejects(
triggerKeyEvent(element, 'keypress', ''),
/Must provide a `key` or `keyCode` to `triggerKeyEvent`/
);
});

test('rejects if lower case key is passed in', async function(assert) {
element = buildInstrumentedElement('div');

await setupContext(context);

assert.rejects(
triggerKeyEvent(element, 'keypress', 'enter'),
/Must provide a `key` to `triggerKeyEvent` that starts with an uppercase character but you passed `enter`./
);
});

test('rejects if keyCode is passed as a string', async function(assert) {
element = buildInstrumentedElement('div');

await setupContext(context);

assert.rejects(
triggerKeyEvent(element, 'keypress', '13'),
/Must provide a numeric `keyCode` to `triggerKeyEvent` but you passed `13` as a string./
);
});

test('triggering via selector with context set', async function(assert) {
element = buildInstrumentedElement('div');

Expand Down Expand Up @@ -143,7 +176,7 @@ module('DOM Helper: triggerKeyEvent', function(hooks) {
await checkKey(18, 'Alt');
await checkKey(20, 'CapsLock');
await checkKey(27, 'Escape');
await checkKey(32, '');
await checkKey(32, ' ');
await checkKey(37, 'ArrowLeft');
await checkKey(38, 'ArrowUp');
await checkKey(39, 'ArrowRight');
Expand Down Expand Up @@ -177,15 +210,15 @@ module('DOM Helper: triggerKeyEvent', function(hooks) {
await checkKeyCode('Alt', 18);
await checkKeyCode('CapsLock', 20);
await checkKeyCode('Escape', 27);
await checkKeyCode('', 32);
await checkKeyCode(' ', 32);
await checkKeyCode('ArrowLeft', 37);
await checkKeyCode('ArrowUp', 38);
await checkKeyCode('ArrowRight', 39);
await checkKeyCode('ArrowDown', 40);
await checkKeyCode('Meta', 91);
await checkKeyCode('=', 187);
await checkKeyCode('-', 189);
await checkKeyCode('a', 65);
await checkKeyCode('z', 90);
await checkKeyCode('A', 65);
await checkKeyCode('Z', 90);
});
});

0 comments on commit bdbc943

Please sign in to comment.