Skip to content

Commit

Permalink
Allow “namespace::” in container & registry
Browse files Browse the repository at this point in the history
  • Loading branch information
iezer committed Dec 14, 2017
1 parent f432484 commit 7c1f5df
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/container/lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DEBUG } from 'ember-env-flags';
import { ENV } from 'ember-environment';

const VALID_FULL_NAME_REGEXP = /^[^:]+:[^:]+$/;
const MODULE_UNIFICATION_VALID_FULL_NAME_REGEXP = /^[^:]+:[^:]+(::[^:]+)?$/;
let missingResolverFunctionsDeprecation = 'Passing a `resolver` function into a Registry is deprecated. Please pass in a Resolver object with a `resolve` method.';

/**
Expand Down Expand Up @@ -548,7 +549,11 @@ export default class Registry {
}

isValidFullName(fullName) {
return VALID_FULL_NAME_REGEXP.test(fullName);
if(!EMBER_MODULE_UNIFICATION) {
return VALID_FULL_NAME_REGEXP.test(fullName);
}

return MODULE_UNIFICATION_VALID_FULL_NAME_REGEXP.test(fullName);
}

getInjections(fullName) {
Expand Down
37 changes: 37 additions & 0 deletions packages/container/tests/container_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,4 +683,41 @@ if (EMBER_MODULE_UNIFICATION) {
assert.ok(container.cache[`template:routes/application:component:my-input`] instanceof PrivateComponent,
'The correct factory was stored in the cache with the correct key which includes the source.');
});

QUnit.test('The container can pass a namespaced path to factoryFor', function(assert) {
let PrivateComponent = factory();
let lookup = 'template:components/my-addon::my-input';
let registry = new Registry();
let resolveCount = 0;
registry.resolve = function(fullName) {
resolveCount++;
if (fullName === lookup) {
return PrivateComponent;
}
};

let container = registry.container();

assert.strictEqual(container.factoryFor(lookup).class, PrivateComponent, 'The correct factory was provided');
assert.strictEqual(container.factoryFor(lookup).class, PrivateComponent, 'The correct factory was provided again');
assert.equal(resolveCount, 1, 'resolve called only once and a cached factory was returned the second time');
});

QUnit.test('The container can pass a namespaced to lookup', function(assert) {
let PrivateComponent = factory();
let lookup = 'template:components/my-addon::my-input';
let registry = new Registry();
registry.resolve = function(fullName) {
if (fullName === lookup) {
return PrivateComponent;
}
};

let container = registry.container();

let result = container.lookup(lookup);
assert.ok(result instanceof PrivateComponent, 'The correct factory was provided');
assert.ok(container.cache[`template:components/my-addon::my-input`] instanceof PrivateComponent,
'The correct factory was stored in the cache with the correct key which includes the source.');
});
}
22 changes: 22 additions & 0 deletions packages/container/tests/registry_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -775,4 +775,26 @@ if (EMBER_MODULE_UNIFICATION) {
assert.strictEqual(registry.resolve(lookup, { source }), PrivateComponent, 'The correct factory was provided again');
assert.equal(resolveCount, 1, 'resolve called only once and a cached factory was returned the second time');
});

QUnit.test('The registry can pass a namespaced lookup to the resolver', function(assert) {
let PrivateComponent = factory();
let lookup = 'template:components/my-addon::my-input';
let resolveCount = 0;
let resolver = {
resolve(fullName, src) {
resolveCount++;
if (fullName === lookup) {
return PrivateComponent;
}
}
};
let registry = new Registry({ resolver });
registry.normalize = function(name) {
return name;
};

assert.strictEqual(registry.resolve(lookup), PrivateComponent, 'The correct factory was provided');
assert.strictEqual(registry.resolve(lookup), PrivateComponent, 'The correct factory was provided again');
assert.equal(resolveCount, 1, 'resolve called only once and a cached factory was returned the second time');
});
}

0 comments on commit 7c1f5df

Please sign in to comment.