Permalink
Browse files

UT fixes and the corresponding adjustments based on the failures

  • Loading branch information...
1 parent 37f7d67 commit 012315df08da98a756387384c548c45f3c1bb21b @caridy committed Dec 7, 2012
@@ -54,7 +54,7 @@ YUI.add('mojito-partial-addon', function(Y, NAME) {
mojitView = instance.views[view];
data = data || {}; // default null data to empty view template
- renderer = Y.mojito.ViewRenderer(mojitView.engine,
+ renderer = new Y.mojito.ViewRenderer(mojitView.engine,
this.ac.staticAppConfig.viewEngine);
Y.log('Rendering "' + view + '" view for "' + (instance.id || '@' +
@@ -177,5 +177,6 @@ YUI.add('mojito-hb', function(Y, NAME) {
}, '0.1.0', {requires: [
'io-base',
+ 'parallel',
'handlebars'
]});
@@ -193,4 +193,4 @@ YUI.add('mojito-hb', function (Y, NAME) {
Y.namespace('mojito.addons.viewEngines').hb = HandleBarsAdapter;
-}, '0.1.0', {requires: ['parallel']});
+}, '0.1.0', {requires: ['mojito', 'parallel']});
@@ -534,9 +534,10 @@ YUI.add('mojito-action-context', function(Y, NAME) {
// Y.log('Rendering "' + meta.view.name + '" view for "' +
// (instance.id || '@' + instance.type) + '"', 'info', NAME);
- // we use the view renderer factory
- // because it provides caching capabilities for better performance
- renderer = Y.mojito.ViewRenderer(mojitView.engine, this.staticAppConfig.viewEngine);
+ // TODO: we might want to use a view renderer factory
+ // that can provide caching capabilities for better performance
+ // instead of creating objects over and over again per mojit instance
+ renderer = new Y.mojito.ViewRenderer(mojitView.engine, this.staticAppConfig.viewEngine);
renderer.render(data, instance, mojitView, adapter, meta, more);
} else {
@@ -11,21 +11,21 @@ YUI.add('mojito-view-renderer', function (Y) {
'use strict';
+ var cache = {};
+
/*
- * Mojito's view renderer abstraction. Will craete a rendering
- * engine object depending on the 'type' specified.
+ * Mojito's view renderer abstraction. Will plugin in the specified view
+ * plugin to do the rendering, depending on the 'type' specified.
* @class ViewRenderer
* @namespace Y.mojito
* @constructor
* @param {String} type view engine addon type to use
- * @param {String} viewId
- * @param {Object} options
+ * @param {Object} options View engines configuration.
*/
- function Renderer(type, viewId, options) {
+ function Renderer(type, options) {
this._type = type || 'hb';
- this._viewId = viewId;
this._options = options;
- this._renderer = null;
+ this._renderer = cache[type];
}
@@ -44,7 +44,7 @@ YUI.add('mojito-view-renderer', function (Y) {
var my = this,
viewEngines = Y.mojito.addons.viewEngines,
fn = function () {
- callback(null, new (viewEngines[my._type])(my._viewId, my._options));
+ callback(null, new (viewEngines[my._type])(my._options));
};
// some perf optimization to avoid calling Y.use when
@@ -92,6 +92,7 @@ YUI.add('mojito-view-renderer', function (Y) {
return;
}
my._renderer = engine;
+ cache[my._type] = engine; // caching renderer instance
my._renderer.render(data, mojitType, tmpl, adapter, meta, more);
});
} else {
@@ -7,35 +7,31 @@
/*jslint anon:true, nomen:true*/
/*global YUI*/
-YUI.add('mojito-view-renderer', function(Y) {
+YUI.add('mojito-view-renderer', function (Y) {
'use strict';
var cache = {};
/*
* Mojito's view renderer abstraction. Will plugin in the specified view
- * plugin to do the rendering, depending on the 'type' specified. This Class
- * can also be used as a factory to avoid creating renderer objects, and to
- * do so, you just need to call `Y.mojito.ViewRenderer('hb', {});` and it will
- * return the cached engine instead of creating a facade for it.
+ * plugin to do the rendering, depending on the 'type' specified.
* @class ViewRenderer
* @namespace Y.mojito
* @constructor
* @param {String} type view engine addon type to use
* @param {Object} options View engines configuration.
*/
function Renderer(type, options) {
- type = type || 'hb';
- if (!cache[type]) {
- this._renderer = cache[type] = new (Y.mojito.addons.viewEngines[type])(options);
- } else {
- this._renderer = cache[type];
+ this._type = type || 'hb';
+ this._options = options;
+ if (!cache[this._type]) {
+ // caching renderer instance
+ cache[this._type] = new (Y.mojito.addons.viewEngines[this._type])(options);
}
- return cache[type];
+ this._renderer = cache[type];
}
-
Renderer.prototype = {
/*
@@ -50,7 +46,7 @@ YUI.add('mojito-view-renderer', function(Y) {
* @param {boolean} more Whether there will be more data to render
* later. (streaming)
*/
- render: function(data, mojitType, tmpl, adapter, meta, more) {
+ render: function (data, mojitType, tmpl, adapter, meta, more) {
this._renderer.render(data, mojitType, tmpl, adapter, meta, more);
}
@@ -8,15 +8,23 @@ YUI().use('mojito-partial-addon', 'test', function (Y) {
var suite = new Y.Test.Suite('mojito-partial-addon tests'),
Assert = Y.Assert,
ObjectAssert = Y.ObjectAssert,
- Mock = Y.Mock;
+ Mock = Y.Mock,
+ ac;
suite.add(new Y.Test.Case({
name: 'render() tests',
+ 'setUp': function () {
+ ac = {
+ staticAppConfig: {
+ viewEngine: {}
+ }
+ };
+ },
+
'test callback called with error when view is not found': function() {
- var ac = {},
- command = {
+ var command = {
instance: {
views: {}
}
@@ -35,8 +43,7 @@ YUI().use('mojito-partial-addon', 'test', function (Y) {
},
'test correctly invokes named view engine': function() {
- var ac = {},
- data = { key: 'value' },
+ var data = { key: 'value' },
command = {
instance: {
type: 'myInstanceType',
@@ -60,7 +67,7 @@ YUI().use('mojito-partial-addon', 'test', function (Y) {
var mockYMojito = Mock();
Mock.expect(mockYMojito, {
method: 'ViewRenderer',
- args: ['myViewEngine'],
+ args: ['myViewEngine', ac.staticAppConfig.viewEngine],
returns: mockRenderer
});
@@ -76,8 +83,7 @@ YUI().use('mojito-partial-addon', 'test', function (Y) {
},
'test passes a valid adapter to the view engine': function() {
- var ac = {},
- command = {
+ var command = {
instance: {
views: {
myView: {}
@@ -138,11 +144,9 @@ YUI().use('mojito-partial-addon', 'test', function (Y) {
Mock.Value.Object
]
});
- var ac = {
- command: command,
- context: 'myContext',
- _dispatch: mockDispatch.dispatch
- };
+ ac.command = command;
+ ac.context = 'myContext';
+ ac._dispatch = mockDispatch.dispatch;
var addon = new Y.mojito.addons.ac.partial(command, null, ac);
addon.invoke('myAction', options, null);
@@ -467,12 +467,14 @@ YUI().use('mojito-action-context', 'test', function (Y) {
Y.mojito.ViewRenderer = function(engine) {
A.areSame('engine', engine, 'bad view engine');
return {
- render: function(d, type, v, a, m, more) {
+ render: function(d, mojitType, v, a, m, more) {
vrRendered = true;
A.areSame(data, d, 'bad data to view');
- A.areSame('t', type, 'bad mojitType to view');
+ A.isObject(mojitType, 'mojitType should be the expanded instance');
+ A.areSame('t', mojitType.type, 'bad mojitType to view');
A.areSame(meta, m, 'bad meta to view');
- A.areSame('path', v, 'bad view content path to view engine');
+ A.isObject(v, 'view object from store with the proper view info');
+ A.areSame('path', v['content-path'], 'bad view content path to view engine');
A.areSame(ac._adapter, a, 'bad adapter to view');
A.isFalse(more);
}
@@ -30,7 +30,7 @@ YUI.add('mojito-view-renderer-client-tests', function(Y, NAME) {
'test instantiating a renderer engine': function () {
var vr;
- vr = new Y.mojito.ViewRenderer('foo', 'someviewid', {z:99});
+ vr = new Y.mojito.ViewRenderer('foo', {z:99});
A.isObject(vr);
// less restrictive initialization
vr = new Y.mojito.ViewRenderer('bar');
@@ -40,15 +40,14 @@ YUI.add('mojito-view-renderer-client-tests', function(Y, NAME) {
'test render method with valid engine': function () {
var args;
var ve = Y.namespace('mojito.addons.viewEngines');
- ve.foo = function(viewId, options) {
- A.areSame('someviewid', viewId);
+ ve.foo = function(options) {
A.areSame(99, options.z);
};
ve.foo.prototype.render = function(data, mojitType, tmpl, adapter, meta, more) {
args = arguments;
};
- var vr = new Y.mojito.ViewRenderer('foo', 'someviewid', {z:99});
+ var vr = new Y.mojito.ViewRenderer('foo', {z:99});
vr.render(1,2,3,4,5,6);
A.areSame(6, args.length);
A.areSame(1, args[0]);
@@ -64,16 +63,15 @@ YUI.add('mojito-view-renderer-client-tests', function(Y, NAME) {
YUI.add('mojito-fakebarengine', function (Y) {
var ve = Y.namespace('mojito.addons.viewEngines');
- ve.fakebarengine = function(viewId, options) {
- A.areSame('someviewid', viewId);
+ ve.fakebarengine = function(options) {
A.areSame(99, options.z);
};
ve.fakebarengine.prototype.render = function(data, mojitType, tmpl, adapter, meta, more) {
args = arguments;
};
});
- var vr = new Y.mojito.ViewRenderer('fakebarengine', 'someviewid', {z:99});
+ var vr = new Y.mojito.ViewRenderer('fakebarengine', {z:99});
vr.render(1,2,3,4,5,6);
this.wait(function () {
A.areSame(6, args.length);
@@ -90,7 +88,7 @@ YUI.add('mojito-view-renderer-client-tests', function(Y, NAME) {
'test render method with invalid ondemand engine': function () {
var err;
- var vr = new Y.mojito.ViewRenderer('fakebazengine', 'someviewid', {z:99});
+ var vr = new Y.mojito.ViewRenderer('fakebazengine', {z:99});
vr.render(1,2,3, {
error: function () {
err = true;
@@ -21,8 +21,7 @@ YUI.add('mojito-view-renderer-server-tests', function(Y, NAME) {
setUp: function() {
var ve = Y.namespace('mojito.addons.viewEngines');
- ve.foo = function(viewId, options) {
- A.areSame('someviewid', viewId);
+ ve.foo = function(options) {
A.areSame(99, options.z);
};
@@ -43,11 +42,11 @@ YUI.add('mojito-view-renderer-server-tests', function(Y, NAME) {
A.isObject(Y.mojito.addons);
A.isFunction(Y.mojito.ViewRenderer);
- var vr = new Y.mojito.ViewRenderer('foo', 'someviewid', {z:99});
+ var vr = new Y.mojito.ViewRenderer('foo', {z:99});
},
'test mock render method': function () {
- var vr = new Y.mojito.ViewRenderer('foo', 'someviewid', {z:99});
+ var vr = new Y.mojito.ViewRenderer('foo', {z:99});
vr.render(1,2,3,4,5,6);
}

0 comments on commit 012315d

Please sign in to comment.