Skip to content

Commit

Permalink
Merge pull request #14 from shuvalov-anton/refactor-tests
Browse files Browse the repository at this point in the history
Refactor tests
  • Loading branch information
A committed Sep 30, 2015
2 parents 92ad03a + 103aadf commit d8166a9
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 79 deletions.
95 changes: 48 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,6 @@
*/
var pathtoRegexp = require('path-to-regexp');

/**
* Helpers
*/

/**
* Simple object test
* @param any obj Variable to test
* @return bool True if variable is an object
*/
function isObject(obj) {
return null != obj && 'object' == typeof obj;
}

/**
* Exec function and return value, or just return arg
* @param {fn|any} val Value or fn to exec
*/
function value(val) {
return 'function' === typeof val
? val()
: val;
}

/**
* Object.assign replacement
* This will always create a new object which has all of the own
* properties of all objects passed. It will ignore non-objects without error.
* @param ...obj object variable number of objects to merge
*/
function mergeObjects() {
var out = {};
var p;

for(var index in arguments) {
var arg = arguments[index]
if(isObject(arg)) {
for(var prop in arg) {
if(arg.hasOwnProperty(prop)) {
out[prop] = arg[prop];
}
}
}
}

return out;
}

/**
* Expose public API
Expand All @@ -76,7 +30,7 @@ var routes = [];
* Unregister all routes
*/
mock.clearRoutes = function() {
routes.splice(0, routes.length)
routes.splice(0, routes.length)
}

/**
Expand Down Expand Up @@ -230,3 +184,50 @@ Route.prototype.match = function(method, url, body) {
};
};


/**
* Helpers
*/

/**
* Simple object test
* @param any obj Variable to test
* @return bool True if variable is an object
*/
function isObject(obj) {
return null != obj && 'object' == typeof obj;
}

/**
* Exec function and return value, or just return arg
* @param {fn|any} val Value or fn to exec
*/
function value(val) {
return 'function' === typeof val
? val()
: val;
}

/**
* Object.assign replacement
* This will always create a new object which has all of the own
* properties of all objects passed. It will ignore non-objects without error.
* @param ...obj object variable number of objects to merge
*/
function mergeObjects() {
var out = {};
var p;

for(var index in arguments) {
var arg = arguments[index]
if(isObject(arg)) {
for(var prop in arg) {
if(arg.hasOwnProperty(prop)) {
out[prop] = arg[prop];
}
}
}
}

return out;
}
102 changes: 70 additions & 32 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,40 @@
var request = require('superagent');
var mock = require('./')(request);
var should = require('should');
var noop = function() {};

describe('superagent mock', function() {

beforeEach(function() {
mock.clearRoutes();
mock.timeout = 0;
});

describe('API', function() {

it('should mock for get', function(done) {
mock.get('/topics/:id', function(req) {
req.params.id.should.be.equal('1');
req.headers['my-header'].should.be.equal('my-Value')
return {
id: req.params.id,
head: req.headers['my-header']
};
return { id: req.params.id };
});
request.get('/topics/1').end(function(_, data) {
data.should.have.property('id', '1');
done();
});
request.get('/topics/1')
.set('My-Header', 'my-Value')
.end(function(_, data) {
data.should.have.property('id', '1');
data.should.have.property('head', 'my-Value')
done();
})
;
});

it('should mock for post', function(done) {
mock.post('/topics/:id', function(req) {
return {
id: req.params.id,
content: req.body.content,
fromSend: req.body.fromSend,
head: req.headers['my-header']
content: req.body.content
};
});
request
.post('/topics/5', { content: 'Should not appear' })
.send({
fromSend: 'Hello universe',
content: 'Hello world'
})
.set('My-Header', 'my-Value')
.post('/topics/5', { content: 'Hello world' })
.end(function(_, data) {
data.should.have.property('id', '5');
data.should.have.property('content', 'Hello world');
data.should.have.property('fromSend', 'Hello universe');
data.should.have.property('head', 'my-Value');
done();
})
;
Expand Down Expand Up @@ -110,8 +99,9 @@ describe('superagent mock', function() {
it('should work with custom timeout', function(done) {
var startedAt = +new Date();
mock.timeout = 100;
mock.get('/timeout', noop);
request
.get('/async')
.get('/timeout')
.end(function(err, res) {
var finishedAt = +new Date();
var offset = finishedAt - startedAt;
Expand All @@ -122,9 +112,10 @@ describe('superagent mock', function() {

it('should work with custom timeout function', function(done) {
var startedAt = +new Date();
mock.get('/timeout', noop);
mock.timeout = function () { return 200; };
request
.get('/async')
.get('/timeout')
.end(function(err, res) {
var finishedAt = +new Date();
var offset = finishedAt - startedAt;
Expand All @@ -134,18 +125,18 @@ describe('superagent mock', function() {
});

it('should clear registered routes', function(done) {
mock.get('http://example.com', function(req){
throw Error('Route handler was called');
});
mock.get('/topics', noop);
mock.clearRoutes();
request
.get('http://example.com')
.get('/topics')
.end(function(err, res) {
done(err);
should.throws(function() {
should.ifError(err);
}, /ECONNREFUSED/);
done();
});
});


it('should provide error when method throws', function(done) {
var error = Error('This should be in the callback!');
mock.get('http://example.com', function(req) {
Expand All @@ -158,5 +149,52 @@ describe('superagent mock', function() {
done();
});
});

it('should support headers', function(done) {
mock.get('/topics/:id', function(req) {
return req.headers;
});
request.get('/topics/1')
.set('My-Header', 'my-Value')
.set('User-Agent', 'Opera Mini')
.end(function(_, data) {
// lowercase
data.should.have.property('my-header', 'my-Value')
data.should.have.property('user-agent', 'Opera Mini')
done();
})
;
});

it('should pass data from send()', function(done) {
mock.post('/topics/:id', function(req) {
return req.body;
});
request
.post('/topics/5')
.send({ content: 'Hello world' })
.end(function(_, data) {
data.should.have.property('content', 'Hello world');
done();
})
;
});

it('should rewrite post() data by send()', function(done) {
mock.post('/topics/:id', function(req) {
return req.body;
});
request
.post('/topics/5', { content: 'Hello Universe'})
.send({ content: 'Hello world', title: 'Yay!' })
.end(function(_, data) {
data.should.have.property('title', 'Yay!');
data.should.have.property('content', 'Hello world');
done();
})
;
});

});

});

0 comments on commit d8166a9

Please sign in to comment.