Created
September 28, 2016 22:10
-
-
Save nucleartide/3191ec93ea7efd45052f84f054be55b3 to your computer and use it in GitHub Desktop.
Choose a grouped option when testing ember-power-select dropdowns
This file contains hidden or 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
import Ember from 'ember'; | |
import run from 'ember-runloop'; | |
/** | |
* Copied from https://github.com/cibernox/ember-power-select/blob/master/test-support/helpers/ember-power-select.js | |
*/ | |
function fireNativeMouseEvent(eventType, selectorOrDomElement, options = {}) { | |
let event = new window.Event(eventType, { bubbles: true, cancelable: true, view: window }); | |
Object.keys(options).forEach((key) => event[key] = options[key]); | |
let target; | |
if (typeof selectorOrDomElement === 'string') { | |
target = $(selectorOrDomElement)[0]; | |
} else { | |
target = selectorOrDomElement; | |
} | |
run(() => target.dispatchEvent(event)); | |
} | |
/** | |
* Copied from https://github.com/cibernox/ember-power-select/blob/master/test-support/helpers/ember-power-select.js | |
*/ | |
export function nativeMouseUp(selectorOrDomElement, options) { | |
fireNativeMouseEvent('mouseup', selectorOrDomElement, options); | |
} | |
/** | |
* Copied from https://github.com/cibernox/ember-power-select/blob/master/test-support/helpers/ember-power-select.js | |
*/ | |
export function nativeMouseDown(selectorOrDomElement, options) { | |
fireNativeMouseEvent('mousedown', selectorOrDomElement, options); | |
} | |
export default Ember.Test.registerAsyncHelper('selectGroupChoose', function(app, cssPath, groupName, value) { | |
let $trigger = find(`${cssPath} .ember-power-select-trigger`); | |
if ($trigger === undefined || $trigger.length === 0) { | |
$trigger = find(cssPath); | |
} | |
let contentId = `${$trigger.attr('aria-controls')}`; | |
let $content = find(`#${contentId}`) | |
// If the dropdown is closed, open it | |
if ($content.length === 0) { | |
nativeMouseDown($trigger.get(0)); | |
wait(); | |
} | |
// Select the option with the given text | |
andThen(function() { | |
let potentialTargets = $(`#${contentId} | |
.ember-power-select-group:contains("${groupName}") | |
.ember-power-select-option:contains("${value}")`).toArray(); | |
let target; | |
if (potentialTargets.length > 1) { | |
target = potentialTargets.filter((t) => t.textContent.trim() === value)[0] || potentialTargets[0]; | |
} else { | |
target = potentialTargets[0]; | |
} | |
nativeMouseUp(target); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment