current-date.js
11.3 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
describe('current date', function() {
var TITLE_FORMAT = 'MMMM D YYYY';
var options;
beforeEach(function() {
affix('#cal');
options = {
titleFormat: TITLE_FORMAT,
titleRangeSeparator: ' - ',
defaultDate: '2014-06-01'
};
});
describe('defaultDate & getDate', function() {
describeWhenInMonth(function() {
it('should initialize at the date', function() {
options.defaultDate = '2011-03-10';
$('#cal').fullCalendar(options);
expectViewDates('2011-02-27', '2011-04-10', '2011-03-01', '2011-04-01');
var currentDate = $('#cal').fullCalendar('getDate');
expect(moment.isMoment(currentDate)).toEqual(true); // test the type, but only here
expect(currentDate).toEqualMoment('2011-03-10');
});
});
describeWhenInWeek(function() {
it('should initialize at the date, given a date string', function() {
options.defaultDate = '2011-03-10';
$('#cal').fullCalendar(options);
expectViewDates('2011-03-06', '2011-03-13');
expect($('#cal').fullCalendar('getDate')).toEqualMoment('2011-03-10');
});
it('should initialize at the date, given a Moment object', function() {
options.defaultDate = $.fullCalendar.moment('2011-03-10');
$('#cal').fullCalendar(options);
expectViewDates('2011-03-06', '2011-03-13');
expect($('#cal').fullCalendar('getDate')).toEqualMoment('2011-03-10');
});
});
describeWhenInDay(function() {
it('should initialize at the date', function() {
options.defaultDate = '2011-03-10';
$('#cal').fullCalendar(options);
expectViewDates('2011-03-10');
expect($('#cal').fullCalendar('getDate')).toEqualMoment('2011-03-10');
});
});
});
describe('gotoDate', function() {
describeWhenInMonth(function() {
it('should go to a date when given a date string', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('gotoDate', '2015-04-01');
expectViewDates('2015-03-29', '2015-05-10', '2015-04-01', '2015-05-01');
});
});
describeWhenInWeek(function() {
it('should go to a date when given a date string', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('gotoDate', '2015-04-01');
expectViewDates('2015-03-29', '2015-04-05');
});
it('should go to a date when given a date string with a time', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('gotoDate', '2015-04-01T12:00:00');
expectViewDates('2015-03-29', '2015-04-05');
});
it('should go to a date when given a moment object', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('gotoDate', $.fullCalendar.moment('2015-04-01'));
expectViewDates('2015-03-29', '2015-04-05');
});
});
describeWhenInDay(function() {
it('should go to a date when given a date string', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('gotoDate', '2015-04-01');
expectViewDates('2015-04-01');
});
});
});
describe('incrementDate', function() {
describeWhenInMonth(function() {
it('should increment the date when given a Duration object', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('incrementDate', { months: -1 });
expectViewDates('2014-04-27', '2014-06-08', '2014-05-01', '2014-06-01');
});
});
describeWhenInWeek(function() {
it('should increment the date when given a Duration object', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('incrementDate', { weeks: -2 });
expectViewDates('2014-05-18', '2014-05-25');
});
});
describeWhenInDay(function() {
it('should increment the date when given a Duration object', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('incrementDate', { days: 2 });
expectViewDates('2014-06-03');
});
it('should increment the date when given a Duration string', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('incrementDate', '2.00:00:00');
expectViewDates('2014-06-03');
});
it('should increment the date when given a Duration string with a time', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('incrementDate', '2.05:30:00');
expectViewDates('2014-06-03');
});
});
});
describe('prev', function() {
describeWhenInMonth(function() {
it('should move the calendar back a month', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('prev');
expectViewDates('2014-04-27', '2014-06-08', '2014-05-01', '2014-06-01');
});
});
describeWhenInWeek(function() {
it('should move the calendar back a week', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('prev');
expectViewDates('2014-05-25', '2014-06-01');
});
});
describeWhenInDay(function() {
it('should move the calendar back a week', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('prev');
expectViewDates('2014-05-31');
});
});
});
describe('next', function() {
describeWhenInMonth(function() {
it('should move the calendar forward a month', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('next');
expectViewDates('2014-06-29', '2014-08-10', '2014-07-01', '2014-08-01');
});
});
describeWhenInWeek(function() {
it('should move the calendar forward a week', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('next');
expectViewDates('2014-06-08', '2014-06-15');
});
});
describeWhenInDay(function() {
it('should move the calendar forward a week', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('next');
expectViewDates('2014-06-02');
});
});
});
describe('prevYear', function() {
describeWhenInMonth(function() {
it('should move the calendar back a year', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('prevYear');
expectViewDates('2013-05-26', '2013-07-07', '2013-06-01', '2013-07-01');
});
});
describeWhenInWeek(function() {
it('should move the calendar back a year', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('prevYear');
expectViewDates('2013-05-26', '2013-06-02');
});
});
describeWhenInDay(function() {
it('should move the calendar back a year', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('prevYear');
expectViewDates('2013-06-01');
});
});
});
describe('nextYear', function() {
describeWhenInMonth(function() {
it('should move the calendar forward a year', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('nextYear');
expectViewDates('2015-05-31', '2015-07-12', '2015-06-01', '2015-07-01');
});
});
describeWhenInWeek(function() {
it('should move the calendar forward a year', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('nextYear');
expectViewDates('2015-05-31', '2015-06-07');
});
});
describeWhenInDay(function() {
it('should move the calendar forward a year', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('nextYear');
expectViewDates('2015-06-01');
});
});
});
describe('today', function() {
beforeEach(function() {
options.now = '2016-03-15'; // the "today" date
});
describeWhenInMonth(function() {
it('should move the calendar to now', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('today');
expectViewDates('2016-02-28', '2016-04-10', '2016-03-01', '2016-04-01');
});
});
describeWhenInWeek(function() {
it('should move the calendar to now', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('today');
expectViewDates('2016-03-13', '2016-03-20');
});
});
describeWhenInDay(function() {
it('should move the calendar to now', function() {
$('#cal').fullCalendar(options);
$('#cal').fullCalendar('today');
expectViewDates('2016-03-15');
});
});
});
describe('when current date is a hidden day', function() {
describeWhenInMonth(function() {
it('should display the current month even if first day of month', function() {
options.now = options.defaultDate = '2014-06-01'; // a Sunday
options.weekends = false;
$('#cal').fullCalendar(options);
var view = $('#cal').fullCalendar('getView');
expect(view.start).toEqualMoment('2014-06-02');
expect(view.end).toEqualMoment('2014-07-12');
expect(view.intervalStart).toEqualMoment('2014-06-01');
expect(view.intervalEnd).toEqualMoment('2014-07-01');
});
it('should display the current month', function() {
options.now = options.defaultDate = '2014-05-04'; // a Sunday
options.weekends = false;
$('#cal').fullCalendar(options);
var view = $('#cal').fullCalendar('getView');
expect(view.start).toEqualMoment('2014-04-28');
expect(view.end).toEqualMoment('2014-06-07');
expect(view.intervalStart).toEqualMoment('2014-05-01');
expect(view.intervalEnd).toEqualMoment('2014-06-01');
});
describe('when navigating back a month', function() {
it('should not skip months', function() {
options.defaultDate = '2014-07-07';
options.weekends = false;
$('#cal').fullCalendar(options);
var view = $('#cal').fullCalendar('getView');
expect(view.intervalStart).toEqualMoment('2014-07-01');
expect(view.intervalEnd).toEqualMoment('2014-08-01');
$('#cal').fullCalendar('prev'); // will move to Jun 1, which is a Sunday
view = $('#cal').fullCalendar('getView');
expect(view.intervalStart).toEqualMoment('2014-06-01');
expect(view.intervalEnd).toEqualMoment('2014-07-01');
});
});
});
describeWhenInDay(function() {
it('should display the next visible day', function() {
options.now = options.defaultDate = '2014-06-01'; // a Sunday
options.weekends = false;
$('#cal').fullCalendar(options);
var view = $('#cal').fullCalendar('getView');
expect(view.start).toEqualMoment('2014-06-02');
expect(view.end).toEqualMoment('2014-06-03');
expect(view.intervalStart).toEqualMoment('2014-06-02');
expect(view.intervalEnd).toEqualMoment('2014-06-03');
});
});
});
// UTILS
// -----
function describeWhenInMonth(func) {
describeWhenIn('month', func);
}
function describeWhenInWeek(func) {
describeWhenIn('basicWeek', func);
describeWhenIn('agendaWeek', func);
}
function describeWhenInDay(func) {
describeWhenIn('basicDay', func);
describeWhenIn('agendaDay', func);
}
function describeWhenIn(viewName, func) {
describe('when in ' + viewName, function() {
beforeEach(function() {
options.defaultView = viewName;
});
func();
});
}
function expectViewDates(start, end, titleStart, titleEnd) {
var view = $('#cal').fullCalendar('getView');
var calculatedEnd;
var title;
start = $.fullCalendar.moment(start);
calculatedEnd = end ? $.fullCalendar.moment(end) : start.clone().add(1, 'days');
expect(start).toEqualMoment(view.start);
expect(calculatedEnd).toEqualMoment(view.end);
titleStart = titleStart ? $.fullCalendar.moment(titleStart) : start;
titleEnd = titleEnd ? $.fullCalendar.moment(titleEnd) : calculatedEnd;
if (titleEnd) {
title = $.fullCalendar.formatRange(
titleStart,
titleEnd.clone().add(-1, 'ms'),
TITLE_FORMAT,
' - '
);
}
else {
title = titleStart.format(TITLE_FORMAT);
}
expect($('.fc-toolbar h2')).toContainText(title);
}
});