aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Lal <james@lightsofapollo.com>2012-07-07 09:24:12 -0700
committerJames Lal <james@lightsofapollo.com>2012-07-07 09:24:12 -0700
commit6f6a52f4307a97b61cc57c0ba272c5e61c13fb7a (patch)
tree21cc177ee997fde87825f72d96da210be0383e98
parenta3f59b6298d87c3a72c167142546a816b3f2e73d (diff)
downloadjsCalDAV-6f6a52f4307a97b61cc57c0ba272c5e61c13fb7a.tar.gz
Calendar resource creation
-rw-r--r--lib/caldav/resources/calendar.js84
-rw-r--r--test/caldav/resources/calendar_test.js90
2 files changed, 163 insertions, 11 deletions
diff --git a/lib/caldav/resources/calendar.js b/lib/caldav/resources/calendar.js
index fd921cb..a17c5c4 100644
--- a/lib/caldav/resources/calendar.js
+++ b/lib/caldav/resources/calendar.js
@@ -14,60 +14,122 @@
options = {};
}
+ if (options.url) {
+ this.url = options.url;
+ }
+
this.updateFromServer(options);
}
Calendar.prototype = {
+ _map: {
+ 'displayname': 'name',
+
+ 'calendar-color': 'color',
+
+ 'calendar-description': 'description',
+
+ 'getctag': 'ctag',
+
+ 'resourcetype': {
+ field: 'resourcetype',
+ defaults: []
+ },
+
+ 'current-user-privilege-set': {
+ field: 'privilegeSet',
+ defaults: []
+ }
+
+ },
+
/**
* location of calendar resource
*/
url: null,
/**
- * color of calendar as defined by ical spec
- */
- color: null,
-
- /**
* displayname as defined by webdav spec
+ * Maps to: displayname
*/
name: null,
/**
- * description of calendar as described by caldav spec
+ * color of calendar as defined by ical spec
+ * Maps to: calendar-color
*/
- description: null,
+ color: null,
/**
- * Available ical components (like VEVENT)
- * @type Array
+ * description of calendar as described by caldav spec
+ * Maps to: calendar-description
*/
- componentSet: null,
+ description: null,
/**
* change tag (as defined by calendarserver spec)
* used to determine if a change has occurred to this
* calendar resource.
+ *
+ * Maps to: getctag
*/
ctag: null,
/**
* Resource types of this resource will
* always contain 'calendar'
+ *
+ * Maps to: resourcetype
+ *
* @type Array
*/
resourcetype: null,
/**
* Set of privileges available to the user.
+ *
+ * Maps to: current-user-privilege-set
*/
privilegeSet: null,
/**
* Updates calendar details from server.
*/
- updateFromServer: function() {
+ updateFromServer: function(options) {
+ var key;
+ var defaultTo;
+ var mapName;
+ var value;
+ var descriptor;
+
+ if (typeof(options) === 'undefined') {
+ options = {};
+ }
+
+ for (key in options) {
+ if (options.hasOwnProperty(key)) {
+ if (key in this._map) {
+ descriptor = this._map[key];
+ value = options[key];
+
+ if (typeof(descriptor) === 'object') {
+ defaultTo = descriptor.defaults;
+ mapName = descriptor.field;
+ } else {
+ defaultTo = '';
+ mapName = descriptor;
+ }
+
+ if (value.status !== '200') {
+ this[mapName] = defaultTo;
+ } else {
+ this[mapName] = value.value;
+ }
+
+ }
+ }
+ }
},
/**
diff --git a/test/caldav/resources/calendar_test.js b/test/caldav/resources/calendar_test.js
index 312d8f7..aa23918 100644
--- a/test/caldav/resources/calendar_test.js
+++ b/test/caldav/resources/calendar_test.js
@@ -1,11 +1,101 @@
testSupport.lib('resources/calendar'),
+testSupport.lib('xhr');
+testSupport.lib('connection');
suite('caldav/resources/calendar', function() {
var Calendar;
+ var Connection;
+ var con;
+ var url = 'foobar.com';
+ var subject;
suiteSetup(function() {
Calendar = Caldav.require('resources/calendar');
+ Connection = Caldav.require('connection');
});
+ setup(function() {
+ con = new Connection();
+ subject = new Calendar(con, {
+ url: url
+ });
+ });
+
+ suite('initialization', function() {
+
+ test('without calendar data', function() {
+ assert.equal(subject.url, url);
+ });
+
+ test('with calendar data', function() {
+ return;
+ var calledWith, data = { value: 'wow'};
+ subject.updateFromServer = function() {
+ calledWith = arguments;
+ }
+
+ subject.constructor.call(this, con, data);
+
+ assert.equal(calledWith[0], data);
+ });
+
+ });
+
+ suite('#updateFromServer', function() {
+
+ function status(value, status) {
+ if (typeof(status) === 'undefined') {
+ status = '200';
+ }
+
+ return { status: status, value: value };
+ }
+
+ var input = {
+ displayname: status('name'),
+ 'calendar-color': status('#FFF'),
+ 'calendar-description': status('desc'),
+ 'getctag': status('17'),
+ 'resourcetype': status(['calendar']),
+ 'current-user-privilege-set': status(null, 404)
+ };
+
+ var expected = {
+ name: 'name',
+ color: '#FFF',
+ description: 'desc',
+ ctag: '17',
+ resourcetype: ['calendar'],
+ privilegeSet: []
+ };
+
+ test('full set', function() {
+ var key;
+ subject.updateFromServer(input);
+
+ for (key in expected) {
+ if (expected.hasOwnProperty(key)) {
+ assert.deepEqual(
+ subject[key], expected[key],
+ key + ' was not set'
+ );
+ }
+ }
+ });
+
+ test('partial update', function() {
+ subject.updateFromServer(input);
+ subject.updateFromServer({
+ 'calendar-description': status('baz')
+ });
+
+ assert.equal(subject.color, '#FFF', 'should not clear old values');
+ assert.equal(subject.description, 'baz');
+ });
+
+ });
+
+
+
});