Ember.js Starter Kit Testing Setup
Based on the official Ember.js starter kit with QUnit testing setup.
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.1.2/handlebars.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/ember.js/1.3.1/ember.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/qunit/1.12.0/qunit.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/qunit/1.12.0/qunit.js"></script>
<script type="text/x-handlebars" data-template-name="application">
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
<p>{{text}}</p>
</script>
<script type="text/x-handlebars" data-template-name="global-loading">
LOADING DATA...
</script>
<div id="testing">
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<div id="ember-testing-container">
<div id="ember-testing"></div>
</div>
</div>
CSS
#testing {
display: none;
}
.testing #testing {
display: block;
}
#ember-testing-container {
position: absolute;
bottom: 0;
right: 0;
width: 200px;
height: 100px;
overflow: auto;
z-index: 9999;
border: 1px solid #ccc;
background: white;
}
#ember-testing {
/* zoom: 50%; */
}
JavaScript
// this config object is for demonstration purposes
var _config = {
// toggle this to see how the app and testing
// function with/without asynchronous behavior
async: true,
// toggle this to see how the app behaves
// with/without the testing setup
testing: false
};
// APP
var App = Ember.Application.create();
App.Router = Ember.Router.extend({});
App.ApplicationRoute = Ember.Route.extend({
actions: {
loading: function () {
// show loading view while waiting for asynchronous routes
var view = this.container.lookup('view:loading').append();
this.router.one('didTransition', view, 'destroy');
}
}
});
App.LoadingView = Ember.View.extend({
templateName: 'global-loading',
elementId: 'global-loading'
});
App.IndexRoute = Ember.Route.extend({
model: function () {
// asynchronous routing (return a promise from the model hook)
if (_config.async) {
return Ember.run(function () {
var url = '/echo/json/',
data = {
json: '{ "text": "Hello from Ember.js" }',
delay: 0.25
},
promise = Ember.$.post(url, data, null, 'json');
promise.then(function (response) {
return response;
});
return promise;
});
}
// synchronous routing (return an object immediately)
else {
return {
text: 'Hello from Ember.js'
};
}
}
});
// TESTING
if (_config.testing) {
$('body').addClass('testing');
App.rootElement = '#ember-testing';
App.setupForTesting();
App.injectTestHelpers();
module("Integration tests", {
setup: function () {
App.reset();
Ember.run(App, App.advanceReadiness);
},
});
test("/", function () {
...