From 6cc24d5d437a4a908668f89fc50156c67c9c6d1a Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Tue, 2 Aug 2016 18:55:01 -0400 Subject: [PATCH] Enable usage when jQuery is not present. --- lib/ember-test-helpers/-legacy-overrides.js | 2 ++ lib/ember-test-helpers/abstract-test-module.js | 9 ++++++--- lib/ember-test-helpers/test-module-for-component.js | 6 +++++- lib/ember-test-helpers/wait.js | 8 +++++++- tests/test-module-for-component-test.js | 12 ++++++++++++ 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/ember-test-helpers/-legacy-overrides.js b/lib/ember-test-helpers/-legacy-overrides.js index 1b7c85963..5650bb9cc 100644 --- a/lib/ember-test-helpers/-legacy-overrides.js +++ b/lib/ember-test-helpers/-legacy-overrides.js @@ -37,6 +37,8 @@ export function preGlimmerSetupIntegrationForComponent() { Ember.run(function() { module.component.appendTo('#ember-testing'); }); + + context._element = module.component.element; }; context.$ = function() { diff --git a/lib/ember-test-helpers/abstract-test-module.js b/lib/ember-test-helpers/abstract-test-module.js index b7d8a021f..18f7a6ebc 100644 --- a/lib/ember-test-helpers/abstract-test-module.js +++ b/lib/ember-test-helpers/abstract-test-module.js @@ -91,8 +91,11 @@ export default Klass.extend({ }, setupTestElements() { - if (Ember.$('#ember-testing').length === 0) { - Ember.$('
').appendTo(document.body); + if (!document.querySelector('#ember-testing')) { + let element = document.createElement('div'); + element.setAttribute('id', 'ember-testing'); + + document.body.appendChild(element); } }, @@ -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 diff --git a/lib/ember-test-helpers/test-module-for-component.js b/lib/ember-test-helpers/test-module-for-component.js index e51f4ec50..7a2b41f43 100644 --- a/lib/ember-test-helpers/test-module-for-component.js +++ b/lib/ember-test-helpers/test-module-for-component.js @@ -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; @@ -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'); @@ -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) { diff --git a/lib/ember-test-helpers/wait.js b/lib/ember-test-helpers/wait.js index 1421967c9..3757459f3 100644 --- a/lib/ember-test-helpers/wait.js +++ b/lib/ember-test-helpers/wait.js @@ -1,7 +1,9 @@ -/* globals jQuery, self */ +/* globals self */ import Ember from 'ember'; +const jQuery = Ember.$; + var requests; function incrementAjaxPendingRequests(_, xhr) { requests.push(xhr); @@ -16,6 +18,8 @@ function decrementAjaxPendingRequests(_, xhr) { } export function _teardownAJAXHooks() { + if (!jQuery) { return; } + jQuery(document).off('ajaxSend', incrementAjaxPendingRequests); jQuery(document).off('ajaxComplete', decrementAjaxPendingRequests); } @@ -23,6 +27,8 @@ export function _teardownAJAXHooks() { export function _setupAJAXHooks() { requests = []; + if (!jQuery) { return; } + jQuery(document).on('ajaxSend', incrementAjaxPendingRequests); jQuery(document).on('ajaxComplete', decrementAjaxPendingRequests); } diff --git a/tests/test-module-for-component-test.js b/tests/test-module-for-component-test.js index 9bf504ed8..bbeb15c60 100644 --- a/tests/test-module-for-component-test.js +++ b/tests/test-module-for-component-test.js @@ -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() { @@ -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("Hello"); + equal(this._element.textContent, 'Hello'); +}); + if (hasEmberVersion(1,11)) { test('it can render a link-to', function() { this.render("{{link-to 'Hi' 'index'}}");