-
-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add typeIn
helper to trigger keyup, keypress and keyup events when filling inputs
#397
Merged
rwjblue
merged 2 commits into
emberjs:master
from
mfeckie:feature/add-fill-in-with-keyboard-events
Sep 27, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,91 @@ | ||
import { nextTickPromise } from '../-utils'; | ||
import settled from '../settled'; | ||
import getElement from './-get-element'; | ||
import isFormControl from './-is-form-control'; | ||
import { __focus__ } from './focus'; | ||
import { Promise } from 'rsvp'; | ||
import fireEvent from './fire-event'; | ||
|
||
/** | ||
* Mimics character by character entry into the target `input` or `textarea` element. | ||
* | ||
* Allows for simulation of slow entry by passing an optional millisecond delay | ||
* between key events. | ||
|
||
* The major difference between `typeIn` and `fillIn` is that `typeIn` triggers | ||
* keyboard events as well as `input` and `change`. | ||
* Typically this looks like `focus` -> `focusin` -> `keydown` -> `keypress` -> `keyup` -> `input` -> `change` | ||
* per character of the passed text (this may vary on some browsers). | ||
* | ||
* @public | ||
* @param {string|Element} target the element or selector to enter text into | ||
* @param {string} text the test to fill the element with | ||
* @param {Object} options {delay: x} (default 50) number of milliseconds to wait per keypress | ||
* @return {Promise<void>} resolves when the application is settled | ||
*/ | ||
export default function typeIn(target, text, options = { delay: 50 }) { | ||
return nextTickPromise().then(() => { | ||
if (!target) { | ||
throw new Error('Must pass an element or selector to `typeIn`.'); | ||
} | ||
|
||
const element = getElement(target); | ||
if (!element) { | ||
throw new Error(`Element not found when calling \`typeIn('${target}')\``); | ||
} | ||
let isControl = isFormControl(element); | ||
if (!isControl) { | ||
throw new Error('`typeIn` is only usable on form controls.'); | ||
} | ||
|
||
if (typeof text === 'undefined' || text === null) { | ||
throw new Error('Must provide `text` when calling `typeIn`.'); | ||
} | ||
|
||
__focus__(element); | ||
|
||
return fillOut(element, text, options.delay) | ||
.then(() => fireEvent(element, 'change')) | ||
.then(settled); | ||
}); | ||
} | ||
|
||
// eslint-disable-next-line require-jsdoc | ||
function fillOut(element, text, delay) { | ||
const inputFunctions = text.split('').map(character => keyEntry(element, character, delay)); | ||
return inputFunctions.reduce((currentPromise, func) => { | ||
return currentPromise.then(() => delayedExecute(func, delay)); | ||
}, Promise.resolve()); | ||
} | ||
|
||
// eslint-disable-next-line require-jsdoc | ||
function keyEntry(element, character) { | ||
const charCode = character.charCodeAt(); | ||
|
||
const eventOptions = { | ||
bubbles: true, | ||
cancellable: true, | ||
charCode, | ||
}; | ||
|
||
const keyEvents = { | ||
down: new KeyboardEvent('keydown', eventOptions), | ||
press: new KeyboardEvent('keypress', eventOptions), | ||
up: new KeyboardEvent('keyup', eventOptions), | ||
}; | ||
|
||
return function() { | ||
element.dispatchEvent(keyEvents.down); | ||
element.dispatchEvent(keyEvents.press); | ||
element.value = element.value + character; | ||
fireEvent(element, 'input'); | ||
element.dispatchEvent(keyEvents.up); | ||
}; | ||
} | ||
|
||
// eslint-disable-next-line require-jsdoc | ||
function delayedExecute(func, delay) { | ||
return new Promise(resolve => { | ||
setTimeout(resolve, delay); | ||
}).then(func); | ||
} |
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
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
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,135 @@ | ||
import { module, test } from 'qunit'; | ||
import { typeIn, setupContext, teardownContext } from '@ember/test-helpers'; | ||
import { buildInstrumentedElement } from '../../helpers/events'; | ||
import { isIE11 } from '../../helpers/browser-detect'; | ||
import hasEmberVersion from 'ember-test-helpers/has-ember-version'; | ||
|
||
/* | ||
* Event order based on https://jsbin.com/zitazuxabe/edit?html,js,console,output | ||
*/ | ||
|
||
let expectedEvents = [ | ||
'focus', | ||
'focusin', | ||
'keydown', | ||
'keypress', | ||
'input', | ||
'keyup', | ||
'keydown', | ||
'keypress', | ||
'input', | ||
'keyup', | ||
'keydown', | ||
'keypress', | ||
'input', | ||
'keyup', | ||
'change', | ||
]; | ||
|
||
if (isIE11) { | ||
expectedEvents = [ | ||
'focusin', | ||
'keydown', | ||
'keypress', | ||
'keyup', | ||
'keydown', | ||
'keypress', | ||
'keyup', | ||
'keydown', | ||
'keypress', | ||
'keyup', | ||
'input', | ||
'change', | ||
'focus', | ||
]; | ||
} | ||
|
||
module('DOM Helper: typeIn', function(hooks) { | ||
if (!hasEmberVersion(2, 4)) { | ||
return; | ||
} | ||
|
||
let context, element; | ||
|
||
hooks.beforeEach(function() { | ||
context = {}; | ||
}); | ||
|
||
hooks.afterEach(async function() { | ||
element.setAttribute('data-skip-steps', true); | ||
|
||
if (element) { | ||
element.parentNode.removeChild(element); | ||
} | ||
if (context.owner) { | ||
await teardownContext(context); | ||
} | ||
|
||
document.getElementById('ember-testing').innerHTML = ''; | ||
}); | ||
|
||
test('filling in an input', async function(assert) { | ||
element = buildInstrumentedElement('input'); | ||
await typeIn(element, 'foo'); | ||
|
||
assert.verifySteps(expectedEvents); | ||
assert.strictEqual(document.activeElement, element, 'activeElement updated'); | ||
assert.equal(element.value, 'foo'); | ||
}); | ||
|
||
test('filling in an input with a delay', async function(assert) { | ||
element = buildInstrumentedElement('input'); | ||
await typeIn(element, 'foo', { delay: 150 }); | ||
|
||
assert.verifySteps(expectedEvents); | ||
assert.strictEqual(document.activeElement, element, 'activeElement updated'); | ||
assert.equal(element.value, 'foo'); | ||
}); | ||
|
||
test('filling in a textarea', async function(assert) { | ||
element = buildInstrumentedElement('textarea'); | ||
await typeIn(element, 'foo'); | ||
|
||
assert.verifySteps(expectedEvents); | ||
assert.strictEqual(document.activeElement, element, 'activeElement updated'); | ||
assert.equal(element.value, 'foo'); | ||
}); | ||
|
||
test('filling in a non-fillable element', async function(assert) { | ||
element = buildInstrumentedElement('div'); | ||
|
||
await setupContext(context); | ||
assert.rejects(typeIn(`#${element.id}`, 'foo'), /`typeIn` is only usable on form controls/); | ||
}); | ||
|
||
test('rejects if selector is not found', async function(assert) { | ||
element = buildInstrumentedElement('div'); | ||
|
||
await setupContext(context); | ||
|
||
assert.rejects( | ||
typeIn(`#foo-bar-baz-not-here-ever-bye-bye`, 'foo'), | ||
/Element not found when calling `typeIn\('#foo-bar-baz-not-here-ever-bye-bye'\)`/ | ||
); | ||
}); | ||
|
||
test('rejects if text to fill in is not provided', async function(assert) { | ||
element = buildInstrumentedElement('input'); | ||
|
||
assert.rejects(typeIn(element), /Must provide `text` when calling `typeIn`/); | ||
}); | ||
|
||
test('does not run sync', async function(assert) { | ||
element = buildInstrumentedElement('input'); | ||
|
||
let promise = typeIn(element, 'foo'); | ||
|
||
assert.verifySteps([]); | ||
|
||
await promise; | ||
|
||
assert.verifySteps(expectedEvents); | ||
assert.strictEqual(document.activeElement, element, 'activeElement updated'); | ||
assert.equal(element.value, 'foo'); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
__focus__
return a promise that we need to chain?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is exactly the same as I found it in
fill-in.js
so I kept for consistency