-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from pixelhandler/two-find-helpers
find and findAll helpers, two types of queries
- Loading branch information
Showing
15 changed files
with
457 additions
and
308 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
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,24 @@ | ||
import Ember from 'ember'; | ||
import { findWithAssert } from 'ember-native-dom-helpers/test-support/find-with-assert'; | ||
import { fireEvent } from 'ember-native-dom-helpers/test-support/fire-event'; | ||
import { focus } from 'ember-native-dom-helpers/test-support/focus'; | ||
import wait from 'ember-test-helpers/wait'; | ||
|
||
const { run } = Ember; | ||
|
||
|
||
/* | ||
@method click | ||
@param {String} selector | ||
@param {Object} options | ||
@return {RSVP.Promise} | ||
@public | ||
*/ | ||
export function click(selector, options = {}) { | ||
let el = findWithAssert(selector); | ||
run(() => fireEvent(el, 'mousedown', options)); | ||
focus(el); | ||
run(() => fireEvent(el, 'mouseup', options)); | ||
run(() => fireEvent(el, 'click', options)); | ||
return wait(); | ||
} |
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,23 @@ | ||
import Ember from 'ember'; | ||
import { findWithAssert } from 'ember-native-dom-helpers/test-support/find-with-assert'; | ||
import { focus } from 'ember-native-dom-helpers/test-support/focus'; | ||
import { fireEvent } from 'ember-native-dom-helpers/test-support/fire-event'; | ||
import wait from 'ember-test-helpers/wait'; | ||
|
||
const { run } = Ember; | ||
|
||
/* | ||
@method fillIn | ||
@param {String} selector | ||
@param {String} text | ||
@return {RSVP.Promise} | ||
@public | ||
*/ | ||
export function fillIn(selector, text) { | ||
let el = findWithAssert(selector); | ||
run(() => focus(el)); | ||
run(() => el.value = text); | ||
run(() => fireEvent(el, 'input')); | ||
run(() => fireEvent(el, 'change')); | ||
return wait(); | ||
} |
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,24 @@ | ||
import settings from 'ember-native-dom-helpers/test-support/settings'; | ||
|
||
/* | ||
The findAll test helper uses `querySelectorAll` to search inside the test | ||
DOM (based on app configuration for the rootElement). | ||
Alternalively, a second argument may be passed which is an element as the | ||
DOM context to search within. | ||
@method findAll | ||
@param {String} CSS selector to find elements in the test DOM | ||
@param {HTMLElement} contextEl to query within, query from its contained DOM | ||
@return {NodeList} A (non-live) list of zero or more HTMLElement objects | ||
@public | ||
*/ | ||
export function findAll(selector, contextEl) { | ||
let result; | ||
if (contextEl instanceof HTMLElement) { | ||
result = contextEl.querySelectorAll(selector); | ||
} else { | ||
result = document.querySelectorAll(`${settings.rootElement} ${selector}`); | ||
} | ||
return result; | ||
} |
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,17 @@ | ||
import { find } from './find'; | ||
|
||
/* | ||
@method findWithAssert | ||
@param {String} CSS selector to find elements in the test DOM | ||
@param {HTMLElement} contextEl to query within, query from its contained DOM | ||
@return {Error|HTMLElement} element if found, or raises an error | ||
@public | ||
*/ | ||
export function findWithAssert(selector, contextEl) { | ||
let el = find(selector, contextEl); | ||
if (el === null) { | ||
throw new Error(`Element ${selector} not found.`); | ||
} else { | ||
return el; | ||
} | ||
} |
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,24 @@ | ||
import settings from 'ember-native-dom-helpers/test-support/settings'; | ||
|
||
/* | ||
The find test helper uses `querySelector` to search inside the test | ||
DOM (based on app configuration for the rootElement). | ||
Alternalively, a second argument may be passed which is an element as the | ||
DOM context to search within. | ||
@method find | ||
@param {String} CSS selector to find one or more elements in the test DOM | ||
@param {HTMLElement} contextEl to query within, query from its contained DOM | ||
@return {null|HTMLElement} null or an element | ||
@public | ||
*/ | ||
export function find(selector, contextEl) { | ||
let result; | ||
if (contextEl instanceof HTMLElement) { | ||
result = contextEl.querySelector(selector); | ||
} else { | ||
result = document.querySelector(`${settings.rootElement} ${selector}`); | ||
} | ||
return result; | ||
} |
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,118 @@ | ||
import Ember from 'ember'; | ||
|
||
const { merge } = Ember; | ||
const DEFAULT_EVENT_OPTIONS = { canBubble: true, cancelable: true }; | ||
const KEYBOARD_EVENT_TYPES = ['keydown', 'keypress', 'keyup']; | ||
const MOUSE_EVENT_TYPES = ['click', 'mousedown', 'mouseup', 'dblclick', 'mouseenter', 'mouseleave', 'mousemove', 'mouseout', 'mouseover']; | ||
|
||
|
||
/* | ||
@method fireEvent | ||
@param {HTMLElement} element | ||
@param {String} type | ||
@param {Object} (optional) options | ||
@private | ||
*/ | ||
export function fireEvent(element, type, options = {}) { | ||
if (!element) { | ||
return; | ||
} | ||
let event; | ||
if (KEYBOARD_EVENT_TYPES.indexOf(type) > -1) { | ||
event = buildKeyboardEvent(type, options); | ||
} else if (MOUSE_EVENT_TYPES.indexOf(type) > -1) { | ||
let rect = element.getBoundingClientRect(); | ||
let x = rect.left + 1; | ||
let y = rect.top + 1; | ||
let simulatedCoordinates = { | ||
screenX: x + 5, | ||
screenY: y + 95, | ||
clientX: x, | ||
clientY: y | ||
}; | ||
event = buildMouseEvent(type, merge(simulatedCoordinates, options)); | ||
} else { | ||
event = buildBasicEvent(type, options); | ||
} | ||
element.dispatchEvent(event); | ||
} | ||
|
||
/* | ||
@method buildBasicEvent | ||
@param {String} type | ||
@param {Object} (optional) options | ||
@param {Boolean} (optional) bubbles | ||
@param {Boolean} (optional) cancelable | ||
@return {Event} | ||
@private | ||
*/ | ||
function buildBasicEvent(type, options = {}, bubbles = true, cancelable = true) { | ||
let event = document.createEvent('Events'); | ||
event.initEvent(type, bubbles, cancelable); | ||
merge(event, options); | ||
return event; | ||
} | ||
|
||
/* | ||
@method buildMouseEvent | ||
@param {String} type | ||
@param {Object} (optional) options | ||
@return {Event} | ||
@private | ||
*/ | ||
function buildMouseEvent(type, options = {}) { | ||
let event; | ||
try { | ||
event = document.createEvent('MouseEvents'); | ||
let eventOpts = merge(merge({}, DEFAULT_EVENT_OPTIONS), options); | ||
event.initMouseEvent( | ||
type, | ||
eventOpts.canBubble, | ||
eventOpts.cancelable, | ||
window, | ||
eventOpts.detail, | ||
eventOpts.screenX, | ||
eventOpts.screenY, | ||
eventOpts.clientX, | ||
eventOpts.clientY, | ||
eventOpts.ctrlKey, | ||
eventOpts.altKey, | ||
eventOpts.shiftKey, | ||
eventOpts.metaKey, | ||
eventOpts.button, | ||
eventOpts.relatedTarget); | ||
} catch (e) { | ||
event = buildBasicEvent(type, options); | ||
} | ||
return event; | ||
} | ||
|
||
/* | ||
@method buildKeyboardEvent | ||
@param {String} type | ||
@param {Object} (optional) options | ||
@return {Event} | ||
@private | ||
*/ | ||
function buildKeyboardEvent(type, options = {}) { | ||
let event; | ||
try { | ||
event = document.createEvent('KeyEvents'); | ||
let eventOpts = merge(merge({}, DEFAULT_EVENT_OPTIONS), options); | ||
event.initKeyEvent( | ||
type, | ||
eventOpts.canBubble, | ||
eventOpts.cancelable, | ||
window, | ||
eventOpts.ctrlKey, | ||
eventOpts.altKey, | ||
eventOpts.shiftKey, | ||
eventOpts.metaKey, | ||
eventOpts.keyCode, | ||
eventOpts.charCode | ||
); | ||
} catch (e) { | ||
event = buildBasicEvent(type, options); | ||
} | ||
return event; | ||
} |
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,32 @@ | ||
import Ember from 'ember'; | ||
import { fireEvent } from 'ember-native-dom-helpers/test-support/fire-event'; | ||
import wait from 'ember-test-helpers/wait'; | ||
|
||
const { run } = Ember; | ||
|
||
/* | ||
@method focus | ||
@param {HTMLElement} el | ||
@private | ||
*/ | ||
export function focus(el) { | ||
if (!el) { return; } | ||
|
||
if (el.tagName === 'INPUT' || el.contentEditable || el.tagName === 'A') { | ||
let type = el.type; | ||
if (type !== 'checkbox' && type !== 'radio' && type !== 'hidden') { | ||
run(null, function() { | ||
// Firefox does not trigger the `focusin` event if the window | ||
// does not have focus. If the document does not have focus then | ||
// fire `focusin` event as well. | ||
if (document.hasFocus && !document.hasFocus()) { | ||
fireEvent(el, 'focusin'); | ||
fireEvent(el, 'focus', null, false); // focus does not bubble | ||
} else { | ||
el.focus(); | ||
} | ||
}); | ||
} | ||
} | ||
return wait(); | ||
} |
Oops, something went wrong.