Skip to content

Commit fddb63a

Browse files
authored
Merge pull request #855 from teddyzeenny/fix-tests
Fix Ember Inspector during tests for Ember >= 3
2 parents 8890240 + 5bc8595 commit fddb63a

3 files changed

Lines changed: 57 additions & 38 deletions

File tree

ember_debug/general-debug.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ export default EmberObject.extend(PortMixin, {
6565
*/
6666
port: readOnly('namespace.port'),
6767

68-
/**
69-
* @type {Application}
70-
*/
71-
application: readOnly('namespace.owner.application'),
72-
7368
/**
7469
* Sends a reply back indicating if the app has been booted.
7570
*
@@ -79,7 +74,7 @@ export default EmberObject.extend(PortMixin, {
7974
*/
8075
sendBooted() {
8176
this.sendMessage('applicationBooted', {
82-
booted: this.get('application.__inspector__booted')
77+
booted: this.get('namespace.owner.__inspector__booted')
8378
});
8479
},
8580

ember_debug/main.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const EmberDebug = EmberObject.extend({
104104
},
105105

106106
reset($keepAdapter) {
107-
if (!this.get('isTesting')) {
107+
if (!this.get('isTesting') && !this.get('owner')) {
108108
this.set('owner', getOwner(this.get('_application')));
109109
}
110110
this.destroyContainer();
@@ -164,7 +164,11 @@ function getApplication() {
164164
}
165165

166166
function getOwner(application) {
167-
return application.__deprecatedInstance__;
167+
if (application.autoboot) {
168+
return application.__deprecatedInstance__;
169+
} else if (application._applicationInstances /* Ember 3.1+ */) {
170+
return application._applicationInstances[0];
171+
}
168172
}
169173

170174
export default EmberDebug;

ember_debug/vendor/startup-wrapper.js

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,37 @@ var EMBER_VERSIONS_SUPPORTED = {{EMBER_VERSIONS_SUPPORTED}};
4949
window.EmberInspector = Ember.EmberInspectorDebugger = requireModule('ember-debug/main')['default'];
5050
Ember.EmberInspectorDebugger.Adapter = requireModule('ember-debug/adapters/' + adapter)['default'];
5151

52-
onApplicationStart(function appStarted(app) {
53-
var isFirstBoot = !('__inspector__booted' in app);
54-
app.__inspector__booted = true;
55-
Ember.EmberInspectorDebugger.set('application', app);
56-
Ember.EmberInspectorDebugger.start(true);
57-
if (isFirstBoot) {
52+
onApplicationStart(function appStarted(instance) {
53+
let app = instance.application;
54+
if (!('__inspector__booted' in app)) {
55+
app.__inspector__booted = true;
5856
// Watch for app reset/destroy
5957
app.reopen({
6058
reset: function() {
6159
this.__inspector__booted = false;
6260
this._super.apply(this, arguments);
63-
},
64-
willDestroy: function() {
65-
Ember.EmberInspectorDebugger.destroyContainer();
66-
Ember.EmberInspectorDebugger.clear();
67-
this._super.apply(this, arguments);
6861
}
6962
});
7063
}
64+
65+
if (instance && !('__inspector__booted' in instance)) {
66+
instance.__inspector__booted = true;
67+
68+
instance.reopen({
69+
// Clean up on instance destruction
70+
willDestroy() {
71+
if (Ember.EmberInspectorDebugger.get('owner') === instance) {
72+
Ember.EmberInspectorDebugger.destroyContainer();
73+
Ember.EmberInspectorDebugger.clear();
74+
}
75+
return this._super.apply(this, arguments);
76+
}
77+
});
78+
// Boot the inspector (or re-boot if already booted, for example in tests)
79+
Ember.EmberInspectorDebugger.set('_application', app);
80+
Ember.EmberInspectorDebugger.set('owner', instance);
81+
Ember.EmberInspectorDebugger.start(true);
82+
}
7183
});
7284
}
7385
});
@@ -109,34 +121,42 @@ var EMBER_VERSIONS_SUPPORTED = {{EMBER_VERSIONS_SUPPORTED}};
109121
var app;
110122
for (var i = 0, l = apps.length; i < l; i++) {
111123
app = apps[i];
124+
// We check for the existance of an application instance because
125+
// in Ember > 3 tests don't destroy the app when they're done but the app has no booted instances.
112126
if (app._readinessDeferrals === 0) {
113-
// App started
114-
callback(app);
115-
break;
127+
let instance = app.__deprecatedInstance__ || (app._applicationInstances && app._applicationInstances[0]);
128+
if (instance) {
129+
// App started
130+
setupInstanceInitializer(app, callback);
131+
callback(instance);
132+
break;
133+
}
116134
}
117135
}
118136
Ember.Application.initializer({
119137
name: 'ember-inspector-booted',
120-
initialize: function() {
121-
// If 2 arguments are passed, we are on Ember < 2.1 (app is second arg)
122-
// If 1 argument is passed, we are on Ember 2.1+ (app is only arg)
123-
var app = arguments[1] || arguments[0];
124-
if (!app.__inspector__setup) {
125-
app.__inspector__setup = true;
126-
app.reopen({
127-
didBecomeReady: function() {
128-
// _super will get reset when we reopen the app
129-
// so we store it in this variable to call it later.
130-
var _super = this._super;
131-
callback(app);
132-
return _super.apply(this, arguments);
133-
}
134-
});
135-
}
138+
initialize: function(app) {
139+
setupInstanceInitializer(app, callback);
136140
}
137141
});
138142
}
139143

144+
function setupInstanceInitializer(app, callback) {
145+
if (!app.__inspector__setup) {
146+
app.__inspector__setup = true;
147+
148+
// We include the app's guid in the initializer name because in Ember versions < 3
149+
// registering an instance initializer with the same name, even if on a different app,
150+
// triggers an error because instance initializers seem to be global instead of per app.
151+
app.instanceInitializer({
152+
name: 'ember-inspector-app-instance-booted-' + Ember.guidFor(app),
153+
initialize: function(instance) {
154+
callback(instance);
155+
}
156+
});
157+
}
158+
}
159+
140160
function getApplications() {
141161
var namespaces = Ember.A(Ember.Namespace.NAMESPACES);
142162

0 commit comments

Comments
 (0)