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 Jan 12, 2018
1 parent ba86a0b commit 86d6b55
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 @@ -5,6 +5,7 @@ import Container from './container';
import { DEBUG } from 'ember-env-flags';

const VALID_FULL_NAME_REGEXP = /^[^:]+:[^:]+$/;
const MODULE_UNIFICATION_VALID_FULL_NAME_REGEXP = /^[^:]+:[^:]+(::[^:]+)?$/;

/**
A registry used to store factory and option information keyed
Expand Down Expand Up @@ -539,7 +540,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 @@ -745,4 +745,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 86d6b55

Please sign in to comment.