Skip to content

Commit

Permalink
Assert against get, not normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic committed Jun 5, 2017
1 parent d87ce8e commit 8d97d8b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
20 changes: 12 additions & 8 deletions mu-trees/addon/module-registries/requirejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import {

export default class RequireJSRegistry {

constructor(config, modulePrefix) {
constructor(config, modulePrefix, require=self.requirejs) {
this._config = config;
this._modulePrefix = modulePrefix;
this._require = require;
}

normalize(specifier) {
_normalize(specifier) {
let s = deserializeSpecifier(specifier);

// This is hacky solution to get around the fact that Ember
Expand All @@ -31,10 +32,10 @@ export default class RequireJSRegistry {
segments.push(group);
}

// Special case to handle definiteCollection for templates
// Special case to handle definitiveCollection for templates
// eventually want to find a better way to address.
// Dgeb wants to find a better way to handle these
// in config without needing definiteCollections.
// in config without needing definitiveCollection.
let ignoreCollection = s.type === 'template' &&
s.collection === 'routes' &&
s.namespace === 'components';
Expand All @@ -61,12 +62,15 @@ export default class RequireJSRegistry {
}

has(specifier) {
let path = this.normalize(specifier);
return path in requirejs.entries;
let path = this._normalize(specifier);
// Worth noting this does not confirm there is a default export,
// as would be expected with this simple implementation of the module
// registry.
return path in this._require.entries;
}

get(specifier) {
let path = this.normalize(specifier);
return require(path).default;
let path = this._normalize(specifier);
return this._require(path).default;
}
}
17 changes: 14 additions & 3 deletions mu-trees/tests/unit/module-registries/requirejs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ export let config = {
}
};

function buildMockRequire() {
let mockRequire = modulePath => mockRequire.entries[modulePath];
mockRequire.entries = {};
return mockRequire;
}

module('RequireJS Registry', {
beforeEach() {

this.mockRequire = buildMockRequire();
this.config = config;
this.registry = new RequireJSRegistry(this.config, 'src');
this.registry = new RequireJSRegistry(this.config, 'src', this.mockRequire);
}
});

Expand All @@ -66,6 +72,11 @@ test('Normalize', function(assert) {
[ 'service:/my-app/services/auth', 'my-app/src/services/auth/service' ]
]
.forEach(([ lookupString, expected ]) => {
assert.equal(this.registry.normalize(lookupString), expected, `normalize ${lookupString} -> ${expected}`);
let expectedModule = {};
this.mockRequire.entries = {
[expected]: {default: expectedModule}
};
let actualModule = this.registry.get(lookupString);
assert.equal(actualModule, expectedModule, `normalize ${lookupString} -> ${expected}`);
});
});

0 comments on commit 8d97d8b

Please sign in to comment.