From 1fb0a4524c0a5c432787b01a4d7d9f9aab43f5f8 Mon Sep 17 00:00:00 2001 From: Shuvalov Anton Date: Sun, 14 Jun 2015 17:47:24 +0300 Subject: [PATCH] fix #7 --- index.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 90afd9d..18fc3bc 100644 --- a/index.js +++ b/index.js @@ -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 + } /**