Skip to content

Instantly share code, notes, and snippets.

@validkeys
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save validkeys/aabaef9b476c7b307da9 to your computer and use it in GitHub Desktop.

Select an option

Save validkeys/aabaef9b476c7b307da9 to your computer and use it in GitHub Desktop.
My Ember-CLI start-app.js
/* global require */
var Application = require('client/app')['default'];
var Router = require('client/router')['default'];
import Ember from 'ember';
// Because I want to see the final state of each
// test visually in the preview window, I only reset the app
// in the setup method of each module. Which ofcourse causes an issue because the last
// test run in a module doesn't destroy the app before the next module -- leading to an error
// when the next test is run because an app is already using the root element
//
// so when an app instance is created, I push it to this "cache" array.
// when the next test runs: App = startApp(), I ensure that the previous
// app is destroyed first. This also means that I don't have to have a teardown
// function in each module
var cache = [];
function clearCache(){
cache.forEach(function(app, i){
Ember.run(app, 'destroy');
cache.splice(i, 1);
});
console.info("Previous App Destroyed. Cache Count: " + cache.length);
}
export default function startApp(attrs) {
var App;
// Destroy the previous app (if there is one)
clearCache();
var attributes = Ember.merge({
// useful Test defaults
rootElement: '#ember-testing',
LOG_ACTIVE_GENERATION:false,
LOG_VIEW_LOOKUPS: false
}, attrs); // but you can override;
Router.reopen({
location: 'none'
});
Ember.Application.reopen({
// Instead of having to shutdown the pretender server
// in the teardown of each model, I just do it here for
// convenience and DRYness
destroy: function(){
if(App.server){
App.server.shutdown();
}
this._super();
}
});
Ember.run(function(){
App = Application.create(attributes);
App.setupForTesting();
App.injectTestHelpers();
// Because I add classes to the body tag for CSS menu hiding
// purposes, clear out any body classes here
Ember.run.scheduleOnce('afterRender', function(){
$('body').removeClass();
});
});
App.reset(); // this shouldn't be needed, i want to be able to "start an app at a specific URL"
if (App.server) App.server.shutdown();
// Add a pretender server instance onto App
App.server = new Pretender();
// Print out all requests
App.server.handledRequest = function(verb, path, request){
console.debug("SERVER REQEUST", verb, path, request);
};
// Now push the app into the cache
cache.push(App);
return App;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment