Skip to content

Commit

Permalink
Merge pull request #172 from rwjblue/make-work-without-jquery
Browse files Browse the repository at this point in the history
Enable usage when jQuery is not present.
  • Loading branch information
rwjblue authored Aug 3, 2016
2 parents 5754ca7 + 6cc24d5 commit 458dbd6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/ember-test-helpers/-legacy-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export function preGlimmerSetupIntegrationForComponent() {
Ember.run(function() {
module.component.appendTo('#ember-testing');
});

context._element = module.component.element;
};

context.$ = function() {
Expand Down
9 changes: 6 additions & 3 deletions lib/ember-test-helpers/abstract-test-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ export default Klass.extend({
},

setupTestElements() {
if (Ember.$('#ember-testing').length === 0) {
Ember.$('<div id="ember-testing"/>').appendTo(document.body);
if (!document.querySelector('#ember-testing')) {
let element = document.createElement('div');
element.setAttribute('id', 'ember-testing');

document.body.appendChild(element);
}
},

Expand All @@ -114,7 +117,7 @@ export default Klass.extend({
},

teardownTestElements() {
Ember.$('#ember-testing').empty();
document.getElementById('ember-testing').innerHTML = '';

// Ember 2.0.0 removed Ember.View as public API, so only do this when
// Ember.View is present
Expand Down
6 changes: 5 additions & 1 deletion lib/ember-test-helpers/test-module-for-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export default TestModule.extend({
context.dispatcher = this.container.lookup('event_dispatcher:main') || Ember.EventDispatcher.create();
context.dispatcher.setup({}, '#ember-testing');

context._element = null;

this.callbacks.render = function() {
var subject;

Expand All @@ -108,6 +110,8 @@ export default TestModule.extend({
subject.appendTo('#ember-testing');
});

context._element = subject.element;

_this.teardownSteps.unshift(function() {
Ember.run(function() {
Ember.tryInvoke(subject, 'destroy');
Expand Down Expand Up @@ -243,7 +247,7 @@ export function setupComponentIntegrationTest() {
// ensure the element is based on the wrapping toplevel view
// Ember still wraps the main application template with a
// normal tagged view
element = Ember.$('#ember-testing > .ember-view');
context._element = element = document.querySelector('#ember-testing > .ember-view');
};

context.$ = function(selector) {
Expand Down
8 changes: 7 additions & 1 deletion lib/ember-test-helpers/wait.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* globals jQuery, self */
/* globals self */

import Ember from 'ember';

const jQuery = Ember.$;

var requests;
function incrementAjaxPendingRequests(_, xhr) {
requests.push(xhr);
Expand All @@ -16,13 +18,17 @@ function decrementAjaxPendingRequests(_, xhr) {
}

export function _teardownAJAXHooks() {
if (!jQuery) { return; }

jQuery(document).off('ajaxSend', incrementAjaxPendingRequests);
jQuery(document).off('ajaxComplete', decrementAjaxPendingRequests);
}

export function _setupAJAXHooks() {
requests = [];

if (!jQuery) { return; }

jQuery(document).on('ajaxSend', incrementAjaxPendingRequests);
jQuery(document).on('ajaxComplete', decrementAjaxPendingRequests);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/test-module-for-component-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ test("$", function(){
equal($.trim(this.$().text()), 'Pretty Color: green');
});

test('it can access the element', function() {
this.subject({name: 'green'});
this.render();

equal(this._element.textContent, 'Pretty Color: green');
});

moduleForComponent('pretty-color', 'component:pretty-color -- this.render in setup', {
unit: true,
beforeSetup: function() {
Expand Down Expand Up @@ -347,6 +354,11 @@ test('it can render a template', function() {
equal(this.$('span').text(), 'Hello');
});

test('it can access the element', function() {
this.render("<span>Hello</span>");
equal(this._element.textContent, 'Hello');
});

if (hasEmberVersion(1,11)) {
test('it can render a link-to', function() {
this.render("{{link-to 'Hi' 'index'}}");
Expand Down

0 comments on commit 458dbd6

Please sign in to comment.