Permalink
Browse files

fix(viewResolver): returns the mapped value of the namespace

  • Loading branch information...
bas080
bas080 committed Jun 3, 2016
1 parent a7b628f commit 893a0416d98981d8b6d9cb1399d332a6a3079249
Showing with 32 additions and 13 deletions.
  1. +6 −5 src/config.js
  2. +5 −3 src/view-manager.js
  3. +7 −4 test/unit/config.spec.js
  4. +14 −1 test/unit/view-manager.spec.js
@@ -5,7 +5,8 @@ export class Config {
/* these can be overwritten with the configureDefaults function */
defaults = {
location: '{{framework}}/{{view}}.html',
framework: 'bootstrap'
framework: 'bootstrap',
map: {}
};
/* stores the namespaced configs */
@@ -38,8 +39,8 @@ export class Config {
*
* @returns {Config}
*/
configureNamespace(name, configs = {}) {
let namespace = this.fetch(name);
configureNamespace(name, configs = {map: {}}) {
let namespace = Object.create(this.fetch(name));
extend(true, namespace, configs);
let config = {};
config[name] = namespace;
@@ -68,12 +69,12 @@ export class Config {
* @param {...string} properties when prop is falsy it returns the whole
* namespaces object
*
* @returns {*} the value of that property
* @returns {*} the value of that property or undefined
*/
fetch(properties) {
if (!this.namespaces[properties]) {
/* if namespace is not defined it creates a new object with proto defaults */
return Object.create(this.defaults);
return this.defaults;
}
let result = this.namespaces;
@@ -24,10 +24,12 @@ export class ViewManager {
);
}
let config = Object.create(this.config.fetch(namespace));
config.view = view;
let namespaceOrDefault = Object.create(this.config.fetch(namespace));
namespaceOrDefault.view = view;
return render(config.location, config);
let location = namespaceOrDefault.map[view] || namespaceOrDefault.location;
return render(location, namespaceOrDefault);
}
}
@@ -15,7 +15,8 @@ describe('config', () => {
it('can get defaults and has sane defaults', () => {
let defaults = {
location: '{{framework}}/{{view}}.html',
framework: 'bootstrap'
framework: 'bootstrap',
map: {}
};
expect(config.fetch('defaults')).toEqual(defaults);
});
@@ -32,12 +33,14 @@ describe('config', () => {
it('can set defaults', () => {
let defaults = {
boo: 'two',
location: '{{framework}}/{{view}}.html',
framework: 'bootstrap'
map: {},
framework: 'bootstrap',
boo: 'two'
};
expect(config.configureDefaults({boo: 'two'})).toEqual(config); //returns self
/* chainable */
expect(config.configureDefaults(defaults)).toEqual(config);
expect(config.fetch('defaults')).toEqual(defaults);
});
@@ -17,7 +17,8 @@ describe('ViewManager', () => {
// change the default namespace config and chain */
expect(config.configureDefaults({
location: 'my/views/{{view}}/{{framework}}.html',
framework: 'plmr'
framework: 'plmr',
map: {}
})).toBe(config);
/* check if new defaults are used when resolving view */
@@ -45,4 +46,16 @@ describe('ViewManager', () => {
'bootstrap/my-template.html'
);
});
it('resolves a mapped view', () => {
config.configureNamespace('namespace', {
map: {
text: 'input.html'
}
});
expect(viewManager.resolve('namespace', 'text')).toBe('input.html');
});
})

0 comments on commit 893a041

Please sign in to comment.