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

Fix Ember Inspector during tests for Ember >= 3 #855

Merged
merged 1 commit into from
Aug 3, 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: 1 addition & 6 deletions ember_debug/general-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ export default EmberObject.extend(PortMixin, {
*/
port: readOnly('namespace.port'),

/**
* @type {Application}
*/
application: readOnly('namespace.owner.application'),

/**
* Sends a reply back indicating if the app has been booted.
*
Expand All @@ -79,7 +74,7 @@ export default EmberObject.extend(PortMixin, {
*/
sendBooted() {
this.sendMessage('applicationBooted', {
booted: this.get('application.__inspector__booted')
booted: this.get('namespace.owner.__inspector__booted')
});
},

Expand Down
8 changes: 6 additions & 2 deletions ember_debug/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const EmberDebug = EmberObject.extend({
},

reset($keepAdapter) {
if (!this.get('isTesting')) {
if (!this.get('isTesting') && !this.get('owner')) {
this.set('owner', getOwner(this.get('_application')));
}
this.destroyContainer();
Expand Down Expand Up @@ -164,7 +164,11 @@ function getApplication() {
}

function getOwner(application) {
return application.__deprecatedInstance__;
if (application.autoboot) {
return application.__deprecatedInstance__;
} else if (application._applicationInstances /* Ember 3.1+ */) {
return application._applicationInstances[0];
}
}

export default EmberDebug;
80 changes: 50 additions & 30 deletions ember_debug/vendor/startup-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,37 @@ var EMBER_VERSIONS_SUPPORTED = {{EMBER_VERSIONS_SUPPORTED}};
window.EmberInspector = Ember.EmberInspectorDebugger = requireModule('ember-debug/main')['default'];
Ember.EmberInspectorDebugger.Adapter = requireModule('ember-debug/adapters/' + adapter)['default'];

onApplicationStart(function appStarted(app) {
var isFirstBoot = !('__inspector__booted' in app);
app.__inspector__booted = true;
Ember.EmberInspectorDebugger.set('application', app);
Ember.EmberInspectorDebugger.start(true);
if (isFirstBoot) {
onApplicationStart(function appStarted(instance) {
let app = instance.application;
if (!('__inspector__booted' in app)) {
app.__inspector__booted = true;
// Watch for app reset/destroy
app.reopen({
reset: function() {
this.__inspector__booted = false;
this._super.apply(this, arguments);
},
willDestroy: function() {
Ember.EmberInspectorDebugger.destroyContainer();
Ember.EmberInspectorDebugger.clear();
this._super.apply(this, arguments);
}
});
}

if (instance && !('__inspector__booted' in instance)) {
instance.__inspector__booted = true;

instance.reopen({
// Clean up on instance destruction
willDestroy() {
if (Ember.EmberInspectorDebugger.get('owner') === instance) {
Ember.EmberInspectorDebugger.destroyContainer();
Ember.EmberInspectorDebugger.clear();
}
return this._super.apply(this, arguments);
}
});
// Boot the inspector (or re-boot if already booted, for example in tests)
Ember.EmberInspectorDebugger.set('_application', app);
Ember.EmberInspectorDebugger.set('owner', instance);
Ember.EmberInspectorDebugger.start(true);
}
});
}
});
Expand Down Expand Up @@ -109,34 +121,42 @@ var EMBER_VERSIONS_SUPPORTED = {{EMBER_VERSIONS_SUPPORTED}};
var app;
for (var i = 0, l = apps.length; i < l; i++) {
app = apps[i];
// We check for the existance of an application instance because
// in Ember > 3 tests don't destroy the app when they're done but the app has no booted instances.
if (app._readinessDeferrals === 0) {
// App started
callback(app);
break;
let instance = app.__deprecatedInstance__ || (app._applicationInstances && app._applicationInstances[0]);
if (instance) {
// App started
setupInstanceInitializer(app, callback);
callback(instance);
break;
}
}
}
Ember.Application.initializer({
name: 'ember-inspector-booted',
initialize: function() {
// If 2 arguments are passed, we are on Ember < 2.1 (app is second arg)
// If 1 argument is passed, we are on Ember 2.1+ (app is only arg)
var app = arguments[1] || arguments[0];
if (!app.__inspector__setup) {
app.__inspector__setup = true;
app.reopen({
didBecomeReady: function() {
// _super will get reset when we reopen the app
// so we store it in this variable to call it later.
var _super = this._super;
callback(app);
return _super.apply(this, arguments);
}
});
}
initialize: function(app) {
setupInstanceInitializer(app, callback);
}
});
}

function setupInstanceInitializer(app, callback) {
if (!app.__inspector__setup) {
app.__inspector__setup = true;

// We include the app's guid in the initializer name because in Ember versions < 3
// registering an instance initializer with the same name, even if on a different app,
// triggers an error because instance initializers seem to be global instead of per app.
app.instanceInitializer({
name: 'ember-inspector-app-instance-booted-' + Ember.guidFor(app),
initialize: function(instance) {
callback(instance);
}
});
}
}

function getApplications() {
var namespaces = Ember.A(Ember.Namespace.NAMESPACES);

Expand Down