Skip to content
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

remove jquery from ember-testing #16075

Merged
merged 1 commit into from
Jan 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ module.exports = function() {
'ember-runtime': { trees: null, vendorRequirements: ['rsvp'], requirements: ['container', 'ember-environment', 'ember-console', 'ember-metal'], requiresJQuery: false },
'ember-views': { trees: null, requirements: ['ember-runtime'], skipTests: true },
'ember-extension-support': { trees: null, requirements: ['ember-application'], requiresJQuery: false },
'ember-testing': { trees: null, requirements: ['ember-application', 'ember-routing'], testing: true },
'ember-testing': {
trees: null,
requiresJQuery: false,
requirements: ['ember-application', 'ember-routing'],
testing: true
},
'ember-template-compiler': {
trees: null,
templateCompilerOnly: true,
Expand Down
47 changes: 31 additions & 16 deletions packages/ember-testing/lib/events.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import { jQuery } from 'ember-views';
import { run } from 'ember-metal';
import { assign } from 'ember-utils';
import isFormControl from './helpers/-is-form-control';

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'];

export function focus(el) {
if (!el) { return; }
let $el = jQuery(el);
if ($el.is(':input, [contenteditable=true]')) {
let type = $el.prop('type');
if (el.isContentEditable || isFormControl(el)) {
let type = el.getAttribute('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 doesn't have focus just
// use trigger('focusin') instead.
let browserIsNotFocused = document.hasFocus && !document.hasFocus();

if (!document.hasFocus || document.hasFocus()) {
el.focus();
} else {
$el.trigger('focusin');
// makes `document.activeElement` be `element`. If the browser is focused, it also fires a focus event
el.focus();

// 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 (browserIsNotFocused) {
// if the browser is not focused the previous `el.focus()` didn't fire an event, so we simulate it
fireEvent(el, 'focus', {
bubbles: false,
});

fireEvent(el, 'focusin');
}
});
}
Expand All @@ -43,7 +50,7 @@ export function fireEvent(element, type, options = {}) {
clientX: x,
clientY: y
};
event = buildMouseEvent(type, jQuery.extend(simulatedCoordinates, options));
event = buildMouseEvent(type, assign(simulatedCoordinates, options));
} else {
event = buildBasicEvent(type, options);
}
Expand All @@ -52,16 +59,24 @@ export function fireEvent(element, type, options = {}) {

function buildBasicEvent(type, options = {}) {
let event = document.createEvent('Events');
event.initEvent(type, true, true);
jQuery.extend(event, options);

// Event.bubbles is read only
let bubbles = options.bubbles !== undefined ? options.bubbles : true;
let cancelable = options.cancelable !== undefined ? options.cancelable : true;

delete options.bubbles;
delete options.cancelable;

event.initEvent(type, bubbles, cancelable);
assign(event, options);
return event;
}

function buildMouseEvent(type, options = {}) {
let event;
try {
event = document.createEvent('MouseEvents');
let eventOpts = jQuery.extend({}, DEFAULT_EVENT_OPTIONS, options);
let eventOpts = assign({}, DEFAULT_EVENT_OPTIONS, options);
event.initMouseEvent(
type,
eventOpts.canBubble,
Expand All @@ -88,7 +103,7 @@ function buildKeyboardEvent(type, options = {}) {
let event;
try {
event = document.createEvent('KeyEvents');
let eventOpts = jQuery.extend({}, DEFAULT_EVENT_OPTIONS, options);
let eventOpts = assign({}, DEFAULT_EVENT_OPTIONS, options);
event.initKeyEvent(
type,
eventOpts.canBubble,
Expand Down
16 changes: 16 additions & 0 deletions packages/ember-testing/lib/helpers/-is-form-control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const FORM_CONTROL_TAGS = ['INPUT', 'BUTTON', 'SELECT', 'TEXTAREA'];

/**
@private
@param {Element} element the element to check
@returns {boolean} `true` when the element is a form control, `false` otherwise
*/
export default function isFormControl(element) {
let { tagName, type } = element;

if (type === 'hidden') {
return false;
}

return FORM_CONTROL_TAGS.indexOf(tagName) > -1;
}
8 changes: 7 additions & 1 deletion packages/ember-testing/lib/helpers/fill_in.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@module ember
*/
import { focus, fireEvent } from '../events';
import isFormControl from './-is-form-control';

/**
Fills in an input element with some text.
Expand Down Expand Up @@ -32,7 +33,12 @@ export default function fillIn(app, selector, contextOrText, text) {
el = $el[0];
focus(el);

$el.eq(0).val(text);
if (isFormControl(el)) {
el.value = text;
} else {
el.innerHTML = text;
}

fireEvent(el, 'input');
fireEvent(el, 'change');

Expand Down
9 changes: 7 additions & 2 deletions packages/ember-testing/lib/helpers/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
@module ember
*/
import { get } from 'ember-metal';
import { assert } from 'ember-debug';
import { jQueryDisabled } from 'ember-views';

/**
Finds an element in the context of the app's container element. A simple alias
Expand All @@ -20,13 +22,16 @@ import { get } from 'ember-metal';
```

@method find
@param {String} selector jQuery string selector for element lookup
@param {String} selector jQuery selector for element lookup
@param {String} [context] (optional) jQuery selector that will limit the selector
argument to find only within the context's children
@return {Object} jQuery object representing the results of the query
@return {Object} DOM element representing the results of the query
@public
*/
export default function find(app, selector, context) {
if (jQueryDisabled) {
assert('If jQuery is disabled, please import and use helpers from @ember/test-helpers [https://github.com/emberjs/ember-test-helpers]. Note: `find` is not an available helper.');
}
let $el;
context = context || get(app, 'rootElement');
$el = app.$(selector, context);
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-testing/lib/helpers/find_with_assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@param {String} [context] (optional) jQuery selector that will limit the
selector argument to find only within the context's children
@return {Object} jQuery object representing the results of the query
@throws {Error} throws error if jQuery object returned has a length of 0
@throws {Error} throws error if object returned has a length of 0
@public
*/
export default function findWithAssert(app, selector, context) {
Expand Down
13 changes: 5 additions & 8 deletions packages/ember-testing/lib/setup_for_testing.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* global self */

import { setTesting } from 'ember-debug';
import { jQuery } from 'ember-views';
import {
getAdapter,
setAdapter
Expand Down Expand Up @@ -35,13 +34,11 @@ export default function setupForTesting() {
setAdapter((typeof self.QUnit === 'undefined') ? new Adapter() : new QUnitAdapter());
}

if (jQuery) {
jQuery(document).off('ajaxSend', incrementPendingRequests);
jQuery(document).off('ajaxComplete', decrementPendingRequests);
document.removeEventListener('ajaxSend', incrementPendingRequests);
document.removeEventListener('ajaxComplete', decrementPendingRequests);

clearPendingRequests();
clearPendingRequests();

jQuery(document).on('ajaxSend', incrementPendingRequests);
jQuery(document).on('ajaxComplete', decrementPendingRequests);
}
document.addEventListener('ajaxSend', incrementPendingRequests);
document.addEventListener('ajaxComplete', decrementPendingRequests);
}
2 changes: 1 addition & 1 deletion packages/ember-testing/lib/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if (environment.hasDOM && !jQueryDisabled) {
$.event.special.click = {
// For checkbox, fire native event so checked state will be right
trigger() {
if ($.nodeName(this, 'input') && this.type === 'checkbox' && this.click) {
if (this.nodeName === 'INPUT' && this.type === 'checkbox' && this.click) {
this.click();
return false;
}
Expand Down
6 changes: 4 additions & 2 deletions packages/ember-testing/lib/test/pending_requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export function clearPendingRequests() {
requests.length = 0;
}

export function incrementPendingRequests(_, xhr) {
export function incrementPendingRequests({ detail } = { detail: { xhr: null }}) {
let xhr = detail.xhr;
requests.push(xhr);
}

export function decrementPendingRequests(_, xhr) {
export function decrementPendingRequests({ detail } = { detail: { xhr: null }}) {
let xhr = detail.xhr;
for (let i = 0; i < requests.length; i++) {
if (xhr === requests[i]) {
requests.splice(i, 1);
Expand Down
Loading