recurrence-rule-test.js
11.2 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
365
366
367
368
369
370
371
372
373
374
375
'use strict';
var main = require('../package.json').main;
var schedule = require('../' + main);
var sinon = require('sinon');
var clock;
// 12:30:15 pm Thursday 29 April 2010 in the timezone this code is being run in
var base = new Date(2010, 3, 29, 12, 30, 15, 0);
var baseMs = base.getTime();
module.exports = {
"setUp": function(cb) {
clock = sinon.useFakeTimers(baseMs);
cb();
},
"tearDown": function(cb) {
clock.restore();
cb();
},
"#nextInvocationDate(Date)": {
"next second": function(test) {
var rule = new schedule.RecurrenceRule();
rule.second = null;
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 3, 29, 12, 30, 16, 0), next);
test.done();
},
"next 25th second": function(test) {
var rule = new schedule.RecurrenceRule();
rule.second = 25;
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 3, 29, 12, 30, 25, 0), next);
test.done();
},
"next 5th second (minutes incremented)": function(test) {
var rule = new schedule.RecurrenceRule();
rule.second = 5;
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 3, 29, 12, 31, 5, 0), next);
test.done();
},
"next 40th minute": function(test) {
var rule = new schedule.RecurrenceRule();
rule.minute = 40;
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 3, 29, 12, 40, 0, 0), next);
test.done();
},
"next 1st minute (hours incremented)": function(test) {
var rule = new schedule.RecurrenceRule();
rule.minute = 1;
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 3, 29, 13, 1, 0, 0), next);
test.done();
},
"next 23rd hour": function(test) {
var rule = new schedule.RecurrenceRule();
rule.hour = 23;
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 3, 29, 23, 0, 0, 0), next);
test.done();
},
"next 3rd hour (days incremented)": function(test) {
var rule = new schedule.RecurrenceRule();
rule.hour = 3;
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 3, 30, 3, 0, 0, 0), next);
test.done();
},
"next Friday": function(test) {
var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = 5;
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 3, 30, 0, 0, 0, 0), next);
test.done();
},
"next Monday (months incremented)": function(test) {
var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = 1;
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 4, 3, 0, 0, 0, 0), next);
test.done();
},
"next 30th date": function(test) {
var rule = new schedule.RecurrenceRule();
rule.date = 30;
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 3, 30, 0, 0, 0, 0), next);
test.done();
},
"next 5th date (months incremented)": function(test) {
var rule = new schedule.RecurrenceRule();
rule.date = 5;
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 4, 5, 0, 0, 0, 0), next);
test.done();
},
"next October": function(test) {
var rule = new schedule.RecurrenceRule();
rule.month = 9;
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 9, 1, 0, 0, 0, 0), next);
test.done();
},
"next February (years incremented)": function(test) {
var rule = new schedule.RecurrenceRule();
rule.month = 1;
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2011, 1, 1, 0, 0, 0, 0), next);
test.done();
},
"in the year 2040": function(test) {
var rule = new schedule.RecurrenceRule();
rule.year = 2040;
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2040, 0, 1, 0, 0, 0, 0), next);
test.done();
},
"using past year": function(test) {
var rule = new schedule.RecurrenceRule();
rule.year = 2000;
var next = rule.nextInvocationDate(base);
test.equal(null, next);
test.done();
},
"using mixed time components": function(test) {
var rule = new schedule.RecurrenceRule();
rule.second = 50;
rule.minute = 5;
rule.hour = 10;
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 3, 30, 10, 5, 50, 0), next);
test.done();
},
/*
"using date and dayOfWeek together": function(test) {
var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = 4; // This is Thursday April 1st
rule.date = 10; // This is Saturday April 10th
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 3, 1, 0, 0, 0, 0), next);
test.done();
}*/
"returns null when no invocations left": function(test) {
var rule = new schedule.RecurrenceRule();
rule.year = 2000;
var next = rule.nextInvocationDate(base);
test.strictEqual(null, next);
test.done();
},
"specify span of components using Range": function(test) {
var rule = new schedule.RecurrenceRule();
rule.minute = new schedule.Range(4, 6);
var next;
next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 3, 29, 13, 4, 0, 0), next);
next = rule.nextInvocationDate(next);
test.deepEqual(new Date(2010, 3, 29, 13, 5, 0, 0), next);
next = rule.nextInvocationDate(next);
test.deepEqual(new Date(2010, 3, 29, 13, 6, 0, 0), next);
next = rule.nextInvocationDate(next);
test.deepEqual(new Date(2010, 3, 29, 14, 4, 0, 0), next);
test.done();
},
"specify intervals within span of components using Range with step": function(test) {
var rule = new schedule.RecurrenceRule();
rule.minute = new schedule.Range(4, 8, 2);
var next;
next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 3, 29, 13, 4, 0, 0), next);
next = rule.nextInvocationDate(next);
test.deepEqual(new Date(2010, 3, 29, 13, 6, 0, 0), next);
/* Should Range stay inclusive on both ends when step > 1
next = rule.nextInvocationDate(next);
test.deepEqual(new Date(2010, 3, 29, 13, 8, 0, 0), next);
*/
next = rule.nextInvocationDate(next);
test.deepEqual(new Date(2010, 3, 29, 14, 4, 0, 0), next);
test.done();
},
"specify span and explicit components using Array of Ranges and Numbers": function(test) {
var rule = new schedule.RecurrenceRule();
rule.minute = [2, new schedule.Range(4, 6)];
var next;
next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 3, 29, 13, 2, 0, 0), next);
next = rule.nextInvocationDate(next);
test.deepEqual(new Date(2010, 3, 29, 13, 4, 0, 0), next);
next = rule.nextInvocationDate(next);
test.deepEqual(new Date(2010, 3, 29, 13, 5, 0, 0), next);
next = rule.nextInvocationDate(next);
test.deepEqual(new Date(2010, 3, 29, 13, 6, 0, 0), next);
next = rule.nextInvocationDate(next);
test.deepEqual(new Date(2010, 3, 29, 14, 2, 0, 0), next);
test.done();
},
"From 31th May schedule the 1st of every June": function(test) {
var rule = new schedule.RecurrenceRule();
rule.second = 0;
rule.minute = 0;
rule.hour = 0;
rule.date = 1;
rule.month = 5;
var next;
var base1 = new Date(2010, 4, 31, 12, 30, 15, 0);
next = rule.nextInvocationDate(base1);
test.deepEqual(new Date(2010, 5, 1, 0, 0, 0, 0), next);
next = rule.nextInvocationDate(next);
test.deepEqual(new Date(2011, 5, 1, 0, 0, 0, 0), next);
test.done();
},
"With the year set should not loop indefinetely": function(test) {
var rule = new schedule.RecurrenceRule();
rule.second = 0;
rule.minute = 0;
rule.hour = 0;
rule.date = 1;
rule.month = 5;
rule.year = 2010;
var next;
var base1 = new Date(2010, 4, 31, 12, 30, 15, 0);
next = rule.nextInvocationDate(base1);
test.deepEqual(new Date(2010, 5, 1, 0, 0, 0, 0), next);
next = rule.nextInvocationDate(next);
test.equal(next, null);
test.done();
},
"using rule with string properties": function(test) {
var rule = new schedule.RecurrenceRule();
rule.second = '50';
rule.minute = '5';
rule.hour = '10';
var next = rule.nextInvocationDate(base);
test.deepEqual(new Date(2010, 3, 30, 10, 5, 50, 0), next);
test.done();
},
"nextInvocationDate on an invalid month should return null": function(test) {
var rule = new schedule.RecurrenceRule();
rule.month = 12;
var next = rule.nextInvocationDate();
test.equal(next, null);
var rule2 = new schedule.RecurrenceRule();
rule2.month = 'asdfasdf';
var next2 = rule2.nextInvocationDate(next);
test.equal(next2, null);
test.done();
},
"nextInvocationDate on an invalid second should return null": function(test) {
var rule = new schedule.RecurrenceRule();
rule.second = 60;
var next = rule.nextInvocationDate();
test.equal(next, null);
var rule2 = new schedule.RecurrenceRule();
rule2.second = 'asdfasdf';
var next2 = rule2.nextInvocationDate();
test.equal(next2, null);
test.done();
},
"nextInvocationDate on an invalid hour should return null": function(test) {
var rule = new schedule.RecurrenceRule();
rule.hour = 24;
var next = rule.nextInvocationDate();
test.equal(next, null);
var rule2 = new schedule.RecurrenceRule();
rule2.hour = 'asdfasdf';
var next2 = rule2.nextInvocationDate();
test.equal(next2, null);
test.done();
},
"nextInvocationDate on an invalid date should return null": function(test) {
var rule = new schedule.RecurrenceRule();
rule.date = 90;
var next = rule.nextInvocationDate();
test.equal(next, null);
// Test February
var rule2 = new schedule.RecurrenceRule();
rule2.month = 1;
rule2.date = 30;
var next2 = rule2.nextInvocationDate();
test.equal(next2, null);
var rule3 = new schedule.RecurrenceRule();
rule3.date = 'asdfasdf';
var next3 = rule3.nextInvocationDate();
test.equal(next3, null);
test.done();
},
"nextInvocationDate on an invalid dayOfWeek should return null": function(test) {
var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = 90;
var next = rule.nextInvocationDate();
test.equal(next, null);
var rule2 = new schedule.RecurrenceRule();
rule2.dayOfWeek = 'asdfasdf';
var next2 = rule.nextInvocationDate();
test.equal(next2, null);
test.done();
}
}
};