Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert against get, not normalize #190

Merged
merged 1 commit into from
Jun 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`);
});
});