Skip to content

Commit

Permalink
fix #7
Browse files Browse the repository at this point in the history
  • Loading branch information
A committed Jun 14, 2015
1 parent 9c3d6c5 commit 1fb0a45
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,31 @@ var callbacks = [];
* Mock
*/
function mock(superagent) {

// The room for matched route
var state = { current: null };

// Patch superagent
patch(superagent, 'get', 'GET', state);
patch(superagent, 'post', 'POST', state);
patch(superagent, 'put', 'PUT', state);
patch(superagent, 'del', 'DELETE', state);

// Patch Request.end()
var oldEnd = superagent.Request.prototype.end;
superagent.Request.prototype.end = function(cb) {
var current = state.current;
setTimeout(function() {
current
? cb(null, current())
: oldEnd.call(this, cb);
}, 0);
if (current) {
setTimeout(function() {
cb && cb(null, current());
}, 0);
} else {
oldEnd.call(this, cb);
}
};

return mock; // chaining

}

/**
Expand Down

0 comments on commit 1fb0a45

Please sign in to comment.