Permalink
Browse files

Fixed bug with deep template objects

The below wouldn't work because `t.aProperty.aFunction` didn't have a
`returnValue` method
````javascript
var t = pretendrObj.template({
    aProperty : {
        aFunction : function () {}
    }
});
t.aProperty.aFunction.returnValue(1);
````
  • Loading branch information...
1 parent 12f174f commit d51141efcd25c9a0f112fba875ede40f3032d26c @nathanmacinnes committed Aug 2, 2012
Showing with 20 additions and 10 deletions.
  1. +14 −9 lib/pretendr.js
  2. +6 −1 test/spec.js
View
@@ -2,9 +2,14 @@
"use strict";
-var makeTemplate,
+var enumerate,
+ makeTemplate,
pretendr;
+enumerate = function (o) {
+ return typeof o === 'object' || typeof o === 'function';
+};
+
makeTemplate = function (t) {
var fake,
i,
@@ -29,14 +34,10 @@ makeTemplate = function (t) {
var i,
p;
p = this.apply(pretendr(t, o));
- for (i in t) {
- if (t.hasOwnProperty(i) && typeof t[i] === 'function') {
- this[i].apply(p[i]);
- }
- }
return p;
},
apply : function (p) {
+ var i;
if (returnValue !== undefined) {
p.returnValue(returnValue);
}
@@ -46,6 +47,11 @@ makeTemplate = function (t) {
if (ownTemplate !== undefined) {
p.template(ownTemplate);
}
+ for (i in t) {
+ if (t.hasOwnProperty(i) && templateObj[i]) {
+ templateObj[i].apply(p[i]);
+ }
+ }
return p;
}
};
@@ -55,9 +61,8 @@ makeTemplate = function (t) {
templateObj.template = setTemplate;
}
for (i in t) {
- if (t.hasOwnProperty(i) && typeof t[i] === 'function') {
- subTemplate = makeTemplate(t[i]);
- templateObj[i] = subTemplate;
+ if (t.hasOwnProperty(i) && enumerate(t[i])) {
+ templateObj[i] = makeTemplate(t[i]);
}
}
return templateObj;
View
@@ -328,11 +328,16 @@ describe("pretendr", function () {
res,
t;
t = m.template({
- method : function () {}
+ method : function () {},
+ property : {
+ subMethod : function () {}
+ }
});
t.method.returnValue(4);
+ t.property.subMethod.returnValue(5);
res = m.mock();
expect(res.method()).to.equal(4);
+ expect(res.property.subMethod()).to.equal(5);
});
it("shouldn't have meaningless methods for objects", function () {
var m = this.pretendr(function () {}),

0 comments on commit d51141e

Please sign in to comment.