Skip to content

Commit 6ff73f9

Browse files
Chris Garrettkategengler
authored andcommitted
[BUGFIX release] Fix ownerInjection when used to create services directly
Fixes `this.owner.ownerInjection()` when used to create Services, Controllers, and other Framework classes directly. This allows unit tests to be written for these classes, something that was more common in older tests. (cherry picked from commit 483613d)
1 parent 937fb74 commit 6ff73f9

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

packages/@ember/-internals/container/tests/container_test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { OWNER } from '@ember/-internals/owner';
22
import { assign } from '@ember/polyfills';
33
import { EMBER_MODULE_UNIFICATION } from '@ember/canary-features';
4+
import Service from '@ember/service';
45
import { DEBUG } from '@glimmer/env';
56
import { Registry } from '..';
67
import { factory, moduleFor, AbstractTestCase, runTask } from 'internal-test-helpers';
@@ -501,6 +502,18 @@ moduleFor(
501502
assert.equal(result[OWNER], owner, 'owner is properly included');
502503
}
503504

505+
['@test ownerInjection should be usable to create a service for testing'](assert) {
506+
assert.expect(0);
507+
508+
let owner = {};
509+
let registry = new Registry();
510+
let container = registry.container({ owner });
511+
512+
let result = container.ownerInjection();
513+
514+
Service.create(result);
515+
}
516+
504517
['@test lookup passes options through to expandlocallookup'](assert) {
505518
let registry = new Registry();
506519
let container = registry.container();

packages/@ember/-internals/runtime/lib/system/core_object.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ const reopen = Mixin.prototype.reopen;
3838
const wasApplied = new WeakSet();
3939

4040
const factoryMap = new WeakMap();
41+
let debugOwnerMap;
42+
43+
if (DEBUG) {
44+
debugOwnerMap = new WeakMap();
45+
}
4146

4247
const prototypeMixinMap = new WeakMap();
4348

@@ -283,19 +288,13 @@ class CoreObject {
283288
assert(
284289
`An EmberObject based class, ${this.constructor}, was not instantiated correctly. You may have either used \`new\` instead of \`.create()\`, or not passed arguments to your call to super in the constructor: \`super(...arguments)\`. If you are trying to use \`new\`, consider using native classes without extending from EmberObject.`,
285290
(() => {
286-
if (passedFromCreate === PASSED_FROM_CREATE) {
287-
return true;
288-
}
289-
290-
if (initFactory === undefined) {
291-
return false;
292-
}
291+
let owner = debugOwnerMap.get(this.constructor);
292+
debugOwnerMap.delete(this.constructor);
293293

294-
if (passedFromCreate === initFactory.owner) {
295-
return true;
296-
}
297-
298-
return false;
294+
return (
295+
passedFromCreate !== undefined &&
296+
(passedFromCreate === PASSED_FROM_CREATE || passedFromCreate === owner)
297+
);
299298
})()
300299
);
301300

@@ -762,11 +761,15 @@ class CoreObject {
762761
owner = getOwner(props);
763762
}
764763

765-
if (owner === undefined) {
766-
// fallback to passing the special PASSED_FROM_CREATE symbol
767-
// to avoid an error when folks call things like Controller.extend().create()
768-
// we should do a subsequent deprecation pass to ensure this isn't allowed
769-
owner = PASSED_FROM_CREATE;
764+
if (DEBUG) {
765+
if (owner === undefined) {
766+
// fallback to passing the special PASSED_FROM_CREATE symbol
767+
// to avoid an error when folks call things like Controller.extend().create()
768+
// we should do a subsequent deprecation pass to ensure this isn't allowed
769+
owner = PASSED_FROM_CREATE;
770+
} else {
771+
debugOwnerMap.set(this, owner);
772+
}
770773
}
771774

772775
instance = new C(owner);

0 commit comments

Comments
 (0)