Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed deep templates

Quick, commit before it breaks again! Needs a bit more refactoring before I'm happy with it.
  • Loading branch information...
commit 7e82ff2628205fa100de9360ee2a431f6957fff3 1 parent b7ff337
Nathan MacInnes authored

Showing 1 changed file with 22 additions and 13 deletions. Show diff stats Hide diff stats

  1. +22 13 lib/pretendr.js
35 lib/pretendr.js
@@ -48,29 +48,38 @@ makeBaseObj = function (descriptor, mock) {
48 48 setReturnValue = function (value) {
49 49 attributes.returnValue = value;
50 50 };
51   - setTemplate = function (desc) {
52   - attributes.template = makeBaseObj(desc);
  51 + setTemplate = function (o) {
  52 + o = o || {};
  53 + if (o.hasOwnProperty('mock') && o.hasOwnProperty('clone')) {
  54 + attributes.template = o.clone();
  55 + } else {
  56 + attributes.template = makeBaseObj(o);
  57 + }
53 58 return attributes.template;
54 59 };
55   - makeCopy = function (obj) {
  60 + baseObj.clone = function (obj) {
56 61 var copy = makeBaseObj(descriptor, obj);
57   - if (attributes.hasOwnProperty('fake')) {
58   - copy.fake(attributes.fake);
59   - }
60   - if (attributes.hasOwnProperty('returnValue')) {
61   - copy.returnValue(attributes.returnValue);
  62 + this.applyAttributesTo(copy);
  63 + return copy;
  64 + };
  65 + baseObj.applyAttributesTo = function (other) {
  66 + var i;
  67 + for (i in attributes) {
  68 + if (attributes.hasOwnProperty(i)) {
  69 + other[i](attributes[i]);
  70 + }
62 71 }
63   - if (attributes.hasOwnProperty('template')) {
64   - copy.template(attributes.template.mock);
  72 + for (i in descriptor) {
  73 + if (descriptor.hasOwnProperty(i) && this.hasOwnProperty(i)) {
  74 + this[i].applyAttributesTo(other[i]);
  75 + }
65 76 }
66   - return copy;
67 77 };
68 78 if (typeof descriptor === 'function') {
69 79 baseObj.fake = setFake;
70 80 baseObj.returnValue = setReturnValue;
71 81 baseObj.template = setTemplate;
72 82 }
73   - baseObj.clone = makeCopy;
74 83 baseObj.mock = mock || makeMock(descriptor, baseObj, attributes);
75 84 if (nonPrimitive(descriptor)) {
76 85 for (i in descriptor) {
@@ -139,4 +148,4 @@ pretendrFunction = function (attributes, baseObject) {
139 148 // hide the extra arg of pretendr from the public
140 149 module.exports = function (o) {
141 150 return makeBaseObj(o);
142   -};
  151 +};

0 comments on commit 7e82ff2

Please sign in to comment.
Something went wrong with that request. Please try again.