aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorJames Lal <james@lightsofapollo.com>2012-07-06 21:24:22 -0700
committerJames Lal <james@lightsofapollo.com>2012-07-06 21:24:22 -0700
commitd67f226d11b84f39c2bda7b9a6ef95ea7dafa337 (patch)
tree39028d73304f54b05d879f3adbcea3680f1a3316 /test
parent619241a08ec736303324211e36ea7dafc4f46c51 (diff)
downloadjsCalDAV-d67f226d11b84f39c2bda7b9a6ef95ea7dafa337.tar.gz
Initial mocking support for mock requests
Diffstat (limited to 'test')
-rw-r--r--test/caldav/request/calendar_home_test.js43
-rw-r--r--test/support/mock_request.js72
-rw-r--r--test/support/propfind.js0
3 files changed, 112 insertions, 3 deletions
diff --git a/test/caldav/request/calendar_home_test.js b/test/caldav/request/calendar_home_test.js
index 525d407..6df65df 100644
--- a/test/caldav/request/calendar_home_test.js
+++ b/test/caldav/request/calendar_home_test.js
@@ -2,10 +2,12 @@ requireRequest();
testSupport.lib('request/propfind');
testSupport.lib('request/calendar_home');
+testSupport.helper('mock_request');
suite('caldav/request/propfind', function() {
- var Propfind;
var Connection;
+ var MockRequest;
+ var MockPropfind;
var Home;
var subject;
var con;
@@ -14,15 +16,22 @@ suite('caldav/request/propfind', function() {
subject;
suiteSetup(function() {
- Propfind = Caldav.require('request/propfind');
Connection = Caldav.require('connection');
Home = Caldav.require('request/calendar_home');
+ MockRequest = Caldav.require('support/mock_request');
+ });
+
+ suiteSetup(function() {
+ MockPropfind = MockRequest.create(['prop']);
});
setup(function() {
+ MockPropfind.reset();
+
con = new Connection();
subject = new Home(con, {
- url: url
+ url: url,
+ Propfind: MockPropfind
});
});
@@ -31,5 +40,33 @@ suite('caldav/request/propfind', function() {
assert.equal(subject.connection, con);
});
+ test('_findPrincipal', function() {
+ var err, data, response = {};
+
+ subject._findPrincipal(url, function() {
+ err = arguments[0];
+ data = arguments[1];
+ });
+
+ var req = MockPropfind.instances[0];
+ assert.equal(req.options.url, url);
+ assert.deepEqual(req.propCalls, [
+ ['current-user-principal'],
+ ['principal-URL']
+ ]);
+
+ response[url] = {
+ 'current-user-principal': {
+ status: '200',
+ value: 'foo.com/'
+ }
+ };
+
+ // respond to request
+ req.respond(null, response);
+
+ assert.equal(data, 'foo.com/');
+ });
+
});
diff --git a/test/support/mock_request.js b/test/support/mock_request.js
new file mode 100644
index 0000000..2141b3c
--- /dev/null
+++ b/test/support/mock_request.js
@@ -0,0 +1,72 @@
+(function(module, ns) {
+
+ function MockRequest(connection, options) {
+ this.connection = connection;
+ this.options = options;
+
+ var parent = this.constructor;
+
+ if (!parent.instances) {
+ parent.instances = [];
+ }
+
+ parent.instances.push(this);
+ }
+
+
+ MockRequest.prototype = {
+ send: function(callback) {
+ this.__sendCallback = callback;
+ },
+
+ respond: function() {
+ this.__sendCallback.apply(this, arguments);
+ }
+
+ };
+
+ MockRequest.reset = function() {
+ if (!this.instances) {
+ this.instances = [];
+ }
+ this.instances.length = 0;
+ }
+
+ MockRequest.create = function(methods) {
+ var self = this;
+
+ if (typeof(methods) === 'undefined') {
+ methods = [];
+ }
+
+ var child = function() {
+ self.apply(this, arguments);
+ };
+
+ child.prototype = Object.create(self.prototype);
+ child.prototype.constructor = child;
+
+ methods.forEach(function(method) {
+ child.prototype[method] = function() {
+ var savedName = method + 'Calls';
+ if (!(savedName in this)) {
+ this[savedName] = [];
+ }
+ this[savedName].push(arguments);
+ }
+ });
+
+ child.create = self.create;
+ child.reset = self.reset;
+
+ return child;
+ };
+
+ module.exports = MockRequest;
+
+}.apply(
+ this,
+ (this.Caldav) ?
+ [Caldav('support/mock_request'), Caldav] :
+ [module, require('../../lib/caldav/caldav')]
+));
diff --git a/test/support/propfind.js b/test/support/propfind.js
deleted file mode 100644
index e69de29..0000000
--- a/test/support/propfind.js
+++ /dev/null