events-gcal.js
12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
describe('Google Calendar plugin', function() {
var API_KEY = 'AIzaSyDcnW6WejpTOCffshGDDb4neIrXVUA1EAE';
var options;
var currentRequest;
var currentWarnArgs;
var oldConsoleWarn;
beforeEach(function() {
affix('#cal');
options = {
defaultView: 'month',
defaultDate: '2014-11-01'
};
// Mockjax is bad with JSONP (https://github.com/jakerella/jquery-mockjax/issues/136)
// Workaround. Wanted to use mockedAjaxCalls(), but JSONP requests get mangled later on.
currentRequest = null;
$.mockjaxSettings.log = function(mockHandler, request) {
currentRequest = currentRequest || $.extend({}, request); // copy
};
// Will cause all requests to go through $.mockjaxSettings.log, but will not actually handle
// any of the requests due to the JSONP bug mentioned above.
// THE REAL REQUESTS WILL GO THROUGH TO THE GOOGLE CALENDAR API!
$.mockjax({
url: '*',
responseText: {}
});
// Intercept calls to console.warn
currentWarnArgs = null;
oldConsoleWarn = console.warn;
console.warn = function() {
currentWarnArgs = arguments;
};
});
afterEach(function() {
$.mockjaxClear();
$.mockjaxSettings.log = function() { };
console.warn = oldConsoleWarn;
});
it('request/receives correctly when local timezone', function(done) {
options.googleCalendarApiKey = API_KEY;
options.events = { googleCalendarId: 'usa__en@holiday.calendar.google.com' };
options.timezone = 'local';
options.eventAfterAllRender = function() {
var events = $('#cal').fullCalendar('clientEvents');
var i;
expect(currentRequest.data.timeMin).toEqual('2014-10-25T00:00:00+00:00'); // one day before, by design
expect(currentRequest.data.timeMax).toEqual('2014-12-08T00:00:00+00:00'); // one day after, by design
expect(currentRequest.data.timeZone).toBeUndefined();
expect(events.length).toBe(4);
for (i = 0; i < events.length; i++) {
expect(events[i].url).not.toMatch('ctz=');
}
done();
};
$('#cal').fullCalendar(options);
});
it('request/receives correctly when UTC timezone', function(done) {
options.googleCalendarApiKey = API_KEY;
options.events = { googleCalendarId: 'usa__en@holiday.calendar.google.com' };
options.timezone = 'UTC';
options.eventAfterAllRender = function() {
var events = $('#cal').fullCalendar('clientEvents');
var i;
expect(currentRequest.data.timeMin).toEqual('2014-10-25T00:00:00+00:00'); // one day before, by design
expect(currentRequest.data.timeMax).toEqual('2014-12-08T00:00:00+00:00'); // one day after, by design
expect(currentRequest.data.timeZone).toEqual('UTC');
expect(events.length).toBe(4);
for (i = 0; i < events.length; i++) {
expect(events[i].url).toMatch('ctz=UTC');
}
done();
};
$('#cal').fullCalendar(options);
});
it('request/receives correctly when custom timezone', function(done) {
options.googleCalendarApiKey = API_KEY;
options.events = { googleCalendarId: 'usa__en@holiday.calendar.google.com' };
options.timezone = 'America/New York';
options.eventAfterAllRender = function() {
var events = $('#cal').fullCalendar('clientEvents');
var i;
expect(currentRequest.data.timeMin).toEqual('2014-10-25T00:00:00+00:00'); // one day before, by design
expect(currentRequest.data.timeMax).toEqual('2014-12-08T00:00:00+00:00'); // one day after, by design
expect(currentRequest.data.timeZone).toEqual('America/New_York'); // space should be escaped
expect(events.length).toBe(4);
for (i = 0; i < events.length; i++) {
expect(events[i].url).toMatch('ctz=America/New_York');
}
done();
};
$('#cal').fullCalendar(options);
});
it('requests/receives correctly when no timezone, defaults to not editable', function(done) {
options.googleCalendarApiKey = API_KEY;
options.events = { googleCalendarId: 'usa__en@holiday.calendar.google.com' };
options.eventAfterAllRender = function() {
var events = $('#cal').fullCalendar('clientEvents');
var eventEls = $('.fc-event');
var i;
expect(currentRequest.data.timeMin).toEqual('2014-10-25T00:00:00+00:00'); // one day before, by design
expect(currentRequest.data.timeMax).toEqual('2014-12-08T00:00:00+00:00'); // one day after, by design
expect(currentRequest.data.timeZone).toBeUndefined();
expect(events.length).toBe(4); // 4 holidays in November 2014
for (i = 0; i < events.length; i++) {
expect(events[i].url).not.toMatch('ctz=');
}
expect(eventEls.length).toBe(4);
expect(eventEls.find('.fc-resizer').length).toBe(0); // not editable
done();
};
$('#cal').fullCalendar(options);
});
it('allows editable to explicitly be set to true', function(done) {
options.googleCalendarApiKey = API_KEY;
options.events = {
googleCalendarId: 'usa__en@holiday.calendar.google.com',
editable: true
};
options.eventAfterAllRender = function() {
var eventEls = $('.fc-event');
expect(eventEls.length).toBe(4);
expect(eventEls.find('.fc-resizer').length).toBeGreaterThan(0); // editable!
done();
};
$('#cal').fullCalendar(options);
});
it('fetches events correctly when API key is in the event source', function(done) {
options.events = {
googleCalendarId: 'usa__en@holiday.calendar.google.com',
googleCalendarApiKey: API_KEY
};
options.eventAfterAllRender = function() {
var events = $('#cal').fullCalendar('clientEvents');
expect(events.length).toBe(4); // 4 holidays in November 2014
done();
};
$('#cal').fullCalendar(options);
});
describe('when not given an API key', function() {
it('calls error handlers, raises warning, and receives no events', function(done) {
options.googleCalendarError = function(err) {
expect(typeof err).toBe('object');
};
options.events = {
googleCalendarError: function(err) {
expect(typeof err).toBe('object');
},
googleCalendarId: 'usa__en@holiday.calendar.google.com'
};
options.eventAfterAllRender = function() {
var events = $('#cal').fullCalendar('clientEvents');
expect(events.length).toBe(0);
expect(currentWarnArgs.length).toBeGreaterThan(0);
expect(options.googleCalendarError).toHaveBeenCalled();
expect(options.events.googleCalendarError).toHaveBeenCalled();
expect(currentRequest).toBeNull(); // AJAX request should have never been made!
done();
};
spyOn(options, 'googleCalendarError').and.callThrough();
spyOn(options.events, 'googleCalendarError').and.callThrough();
$('#cal').fullCalendar(options);
});
});
describe('when given a bad API key', function() {
it('calls error handlers, raises warning, and receives no event', function(done) {
options.googleCalendarApiKey = 'asdfasdfasdf';
options.googleCalendarError = function(err) {
expect(typeof err).toBe('object');
};
options.events = {
googleCalendarError: function(err) {
expect(typeof err).toBe('object');
},
googleCalendarId: 'usa__en@holiday.calendar.google.com'
};
options.eventAfterAllRender = function() {
var events = $('#cal').fullCalendar('clientEvents');
expect(events.length).toBe(0);
expect(currentWarnArgs.length).toBeGreaterThan(0);
expect(options.googleCalendarError).toHaveBeenCalled();
expect(options.events.googleCalendarError).toHaveBeenCalled();
expect(typeof currentRequest).toBe('object'); // request should have been sent
done();
};
spyOn(options, 'googleCalendarError').and.callThrough();
spyOn(options.events, 'googleCalendarError').and.callThrough();
$('#cal').fullCalendar(options);
});
});
it('works when `events` is the actual calendar ID', function(done) {
options.googleCalendarApiKey = API_KEY;
options.events = 'usa__en@holiday.calendar.google.com';
options.eventAfterAllRender = function() {
var events = $('#cal').fullCalendar('clientEvents');
expect(events.length).toBe(4); // 4 holidays in November 2014
done();
};
$('#cal').fullCalendar(options);
});
it('detects a gcal when `events` is the actual calendar ID, with complicated characters (1)', function(done) {
options.googleCalendarApiKey = API_KEY;
options.events = 'arshaw.com_jlr7e6hpcuiald27@whatever.import.calendar.google.com';
options.eventAfterAllRender = function() {
expect(currentWarnArgs.length).toBe(2);
expect(typeof currentWarnArgs[1]).toBe('object'); // sent the request to google, but not-found warning
done();
};
$('#cal').fullCalendar(options);
});
it('detects a gcal when `events` is the actual calendar ID, with complicated characters (2)', function(done) {
options.googleCalendarApiKey = API_KEY;
options.events = 'ar-shaw.com_jlr7e6hpcuiald27@calendar.google.com';
options.eventAfterAllRender = function() {
expect(currentWarnArgs.length).toBe(2);
expect(typeof currentWarnArgs[1]).toBe('object'); // sent the request to google, but not-found warning
done();
};
$('#cal').fullCalendar(options);
});
it('detects a gcal when `events` is the actual calendar ID, person gmail', function(done) {
options.googleCalendarApiKey = API_KEY;
options.events = 'arshaw.arshaw@gmail.com';
options.eventAfterAllRender = function() {
expect(currentWarnArgs.length).toBe(2);
expect(typeof currentWarnArgs[1]).toBe('object'); // sent the request to google, but not-found warning
done();
};
$('#cal').fullCalendar(options);
});
it('detects a gcal when `events` is the actual calendar ID, person googlemail', function(done) {
options.googleCalendarApiKey = API_KEY;
options.events = 'arshaw.arshaw@googlemail.com';
options.eventAfterAllRender = function() {
expect(currentWarnArgs.length).toBe(2);
expect(typeof currentWarnArgs[1]).toBe('object'); // sent the request to google, but not-found warning
done();
};
$('#cal').fullCalendar(options);
});
it('works with requesting an HTTP V1 API feed URL', function(done) {
options.googleCalendarApiKey = API_KEY;
options.events = 'http://www.google.com/calendar/feeds/usa__en%40holiday.calendar.google.com/public/basic';
options.eventAfterAllRender = function() {
var events = $('#cal').fullCalendar('clientEvents');
expect(events.length).toBe(4); // 4 holidays in November 2014
done();
};
$('#cal').fullCalendar(options);
});
it('works with requesting an HTTPS V1 API feed URL', function(done) {
options.googleCalendarApiKey = API_KEY;
options.events = 'https://www.google.com/calendar/feeds/usa__en%40holiday.calendar.google.com/public/basic';
options.eventAfterAllRender = function() {
var events = $('#cal').fullCalendar('clientEvents');
expect(events.length).toBe(4); // 4 holidays in November 2014
done();
};
$('#cal').fullCalendar(options);
});
it('works with requesting an V3 API feed URL', function(done) {
options.googleCalendarApiKey = API_KEY;
options.events =
'https://www.googleapis.com/calendar/v3/calendars/usa__en%40holiday.calendar.google.com/events';
options.eventAfterAllRender = function() {
var events = $('#cal').fullCalendar('clientEvents');
expect(events.length).toBe(4); // 4 holidays in November 2014
done();
};
$('#cal').fullCalendar(options);
});
describe('removeEventSource', function() {
it('works when specifying only the Google Calendar ID', function(done) {
var CALENDAR_ID = 'usa__en@holiday.calendar.google.com';
var called = false;
options.googleCalendarApiKey = API_KEY;
options.eventSources = [ { googleCalendarId: CALENDAR_ID } ];
options.eventAfterAllRender = function() {
var events;
if (called) { return; } // only the first time
called = true;
events = $('#cal').fullCalendar('clientEvents');
expect(events.length).toBe(4); // 4 holidays in November 2014
setTimeout(function() {
$('#cal').fullCalendar('removeEventSource', CALENDAR_ID);
events = $('#cal').fullCalendar('clientEvents');
expect(events.length).toBe(0);
done();
}, 0);
};
$('#cal').fullCalendar(options);
});
it('works when specifying a raw Google Calendar source object', function(done) {
var CALENDAR_ID = 'usa__en@holiday.calendar.google.com';
var googleSource = { googleCalendarId: CALENDAR_ID };
var called = false;
options.googleCalendarApiKey = API_KEY;
options.eventSources = [ googleSource ];
options.eventAfterAllRender = function() {
var events;
if (called) { return; } // only the first time
called = true;
events = $('#cal').fullCalendar('clientEvents');
expect(events.length).toBe(4); // 4 holidays in November 2014
setTimeout(function() {
$('#cal').fullCalendar('removeEventSource', googleSource);
events = $('#cal').fullCalendar('clientEvents');
expect(events.length).toBe(0);
done();
}, 0);
};
$('#cal').fullCalendar(options);
});
});
});