recurrence-rule-test.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. 'use strict';
  2. var main = require('../package.json').main;
  3. var schedule = require('../' + main);
  4. var sinon = require('sinon');
  5. var clock;
  6. // 12:30:15 pm Thursday 29 April 2010 in the timezone this code is being run in
  7. var base = new Date(2010, 3, 29, 12, 30, 15, 0);
  8. var baseMs = base.getTime();
  9. module.exports = {
  10. "setUp": function(cb) {
  11. clock = sinon.useFakeTimers(baseMs);
  12. cb();
  13. },
  14. "tearDown": function(cb) {
  15. clock.restore();
  16. cb();
  17. },
  18. "#nextInvocationDate(Date)": {
  19. "next second": function(test) {
  20. var rule = new schedule.RecurrenceRule();
  21. rule.second = null;
  22. var next = rule.nextInvocationDate(base);
  23. test.deepEqual(new Date(2010, 3, 29, 12, 30, 16, 0), next);
  24. test.done();
  25. },
  26. "next 25th second": function(test) {
  27. var rule = new schedule.RecurrenceRule();
  28. rule.second = 25;
  29. var next = rule.nextInvocationDate(base);
  30. test.deepEqual(new Date(2010, 3, 29, 12, 30, 25, 0), next);
  31. test.done();
  32. },
  33. "next 5th second (minutes incremented)": function(test) {
  34. var rule = new schedule.RecurrenceRule();
  35. rule.second = 5;
  36. var next = rule.nextInvocationDate(base);
  37. test.deepEqual(new Date(2010, 3, 29, 12, 31, 5, 0), next);
  38. test.done();
  39. },
  40. "next 40th minute": function(test) {
  41. var rule = new schedule.RecurrenceRule();
  42. rule.minute = 40;
  43. var next = rule.nextInvocationDate(base);
  44. test.deepEqual(new Date(2010, 3, 29, 12, 40, 0, 0), next);
  45. test.done();
  46. },
  47. "next 1st minute (hours incremented)": function(test) {
  48. var rule = new schedule.RecurrenceRule();
  49. rule.minute = 1;
  50. var next = rule.nextInvocationDate(base);
  51. test.deepEqual(new Date(2010, 3, 29, 13, 1, 0, 0), next);
  52. test.done();
  53. },
  54. "next 23rd hour": function(test) {
  55. var rule = new schedule.RecurrenceRule();
  56. rule.hour = 23;
  57. var next = rule.nextInvocationDate(base);
  58. test.deepEqual(new Date(2010, 3, 29, 23, 0, 0, 0), next);
  59. test.done();
  60. },
  61. "next 3rd hour (days incremented)": function(test) {
  62. var rule = new schedule.RecurrenceRule();
  63. rule.hour = 3;
  64. var next = rule.nextInvocationDate(base);
  65. test.deepEqual(new Date(2010, 3, 30, 3, 0, 0, 0), next);
  66. test.done();
  67. },
  68. "next Friday": function(test) {
  69. var rule = new schedule.RecurrenceRule();
  70. rule.dayOfWeek = 5;
  71. var next = rule.nextInvocationDate(base);
  72. test.deepEqual(new Date(2010, 3, 30, 0, 0, 0, 0), next);
  73. test.done();
  74. },
  75. "next Monday (months incremented)": function(test) {
  76. var rule = new schedule.RecurrenceRule();
  77. rule.dayOfWeek = 1;
  78. var next = rule.nextInvocationDate(base);
  79. test.deepEqual(new Date(2010, 4, 3, 0, 0, 0, 0), next);
  80. test.done();
  81. },
  82. "next 30th date": function(test) {
  83. var rule = new schedule.RecurrenceRule();
  84. rule.date = 30;
  85. var next = rule.nextInvocationDate(base);
  86. test.deepEqual(new Date(2010, 3, 30, 0, 0, 0, 0), next);
  87. test.done();
  88. },
  89. "next 5th date (months incremented)": function(test) {
  90. var rule = new schedule.RecurrenceRule();
  91. rule.date = 5;
  92. var next = rule.nextInvocationDate(base);
  93. test.deepEqual(new Date(2010, 4, 5, 0, 0, 0, 0), next);
  94. test.done();
  95. },
  96. "next October": function(test) {
  97. var rule = new schedule.RecurrenceRule();
  98. rule.month = 9;
  99. var next = rule.nextInvocationDate(base);
  100. test.deepEqual(new Date(2010, 9, 1, 0, 0, 0, 0), next);
  101. test.done();
  102. },
  103. "next February (years incremented)": function(test) {
  104. var rule = new schedule.RecurrenceRule();
  105. rule.month = 1;
  106. var next = rule.nextInvocationDate(base);
  107. test.deepEqual(new Date(2011, 1, 1, 0, 0, 0, 0), next);
  108. test.done();
  109. },
  110. "in the year 2040": function(test) {
  111. var rule = new schedule.RecurrenceRule();
  112. rule.year = 2040;
  113. var next = rule.nextInvocationDate(base);
  114. test.deepEqual(new Date(2040, 0, 1, 0, 0, 0, 0), next);
  115. test.done();
  116. },
  117. "using past year": function(test) {
  118. var rule = new schedule.RecurrenceRule();
  119. rule.year = 2000;
  120. var next = rule.nextInvocationDate(base);
  121. test.equal(null, next);
  122. test.done();
  123. },
  124. "using mixed time components": function(test) {
  125. var rule = new schedule.RecurrenceRule();
  126. rule.second = 50;
  127. rule.minute = 5;
  128. rule.hour = 10;
  129. var next = rule.nextInvocationDate(base);
  130. test.deepEqual(new Date(2010, 3, 30, 10, 5, 50, 0), next);
  131. test.done();
  132. },
  133. /*
  134. "using date and dayOfWeek together": function(test) {
  135. var rule = new schedule.RecurrenceRule();
  136. rule.dayOfWeek = 4; // This is Thursday April 1st
  137. rule.date = 10; // This is Saturday April 10th
  138. var next = rule.nextInvocationDate(base);
  139. test.deepEqual(new Date(2010, 3, 1, 0, 0, 0, 0), next);
  140. test.done();
  141. }*/
  142. "returns null when no invocations left": function(test) {
  143. var rule = new schedule.RecurrenceRule();
  144. rule.year = 2000;
  145. var next = rule.nextInvocationDate(base);
  146. test.strictEqual(null, next);
  147. test.done();
  148. },
  149. "specify span of components using Range": function(test) {
  150. var rule = new schedule.RecurrenceRule();
  151. rule.minute = new schedule.Range(4, 6);
  152. var next;
  153. next = rule.nextInvocationDate(base);
  154. test.deepEqual(new Date(2010, 3, 29, 13, 4, 0, 0), next);
  155. next = rule.nextInvocationDate(next);
  156. test.deepEqual(new Date(2010, 3, 29, 13, 5, 0, 0), next);
  157. next = rule.nextInvocationDate(next);
  158. test.deepEqual(new Date(2010, 3, 29, 13, 6, 0, 0), next);
  159. next = rule.nextInvocationDate(next);
  160. test.deepEqual(new Date(2010, 3, 29, 14, 4, 0, 0), next);
  161. test.done();
  162. },
  163. "specify intervals within span of components using Range with step": function(test) {
  164. var rule = new schedule.RecurrenceRule();
  165. rule.minute = new schedule.Range(4, 8, 2);
  166. var next;
  167. next = rule.nextInvocationDate(base);
  168. test.deepEqual(new Date(2010, 3, 29, 13, 4, 0, 0), next);
  169. next = rule.nextInvocationDate(next);
  170. test.deepEqual(new Date(2010, 3, 29, 13, 6, 0, 0), next);
  171. /* Should Range stay inclusive on both ends when step > 1
  172. next = rule.nextInvocationDate(next);
  173. test.deepEqual(new Date(2010, 3, 29, 13, 8, 0, 0), next);
  174. */
  175. next = rule.nextInvocationDate(next);
  176. test.deepEqual(new Date(2010, 3, 29, 14, 4, 0, 0), next);
  177. test.done();
  178. },
  179. "specify span and explicit components using Array of Ranges and Numbers": function(test) {
  180. var rule = new schedule.RecurrenceRule();
  181. rule.minute = [2, new schedule.Range(4, 6)];
  182. var next;
  183. next = rule.nextInvocationDate(base);
  184. test.deepEqual(new Date(2010, 3, 29, 13, 2, 0, 0), next);
  185. next = rule.nextInvocationDate(next);
  186. test.deepEqual(new Date(2010, 3, 29, 13, 4, 0, 0), next);
  187. next = rule.nextInvocationDate(next);
  188. test.deepEqual(new Date(2010, 3, 29, 13, 5, 0, 0), next);
  189. next = rule.nextInvocationDate(next);
  190. test.deepEqual(new Date(2010, 3, 29, 13, 6, 0, 0), next);
  191. next = rule.nextInvocationDate(next);
  192. test.deepEqual(new Date(2010, 3, 29, 14, 2, 0, 0), next);
  193. test.done();
  194. },
  195. "From 31th May schedule the 1st of every June": function(test) {
  196. var rule = new schedule.RecurrenceRule();
  197. rule.second = 0;
  198. rule.minute = 0;
  199. rule.hour = 0;
  200. rule.date = 1;
  201. rule.month = 5;
  202. var next;
  203. var base1 = new Date(2010, 4, 31, 12, 30, 15, 0);
  204. next = rule.nextInvocationDate(base1);
  205. test.deepEqual(new Date(2010, 5, 1, 0, 0, 0, 0), next);
  206. next = rule.nextInvocationDate(next);
  207. test.deepEqual(new Date(2011, 5, 1, 0, 0, 0, 0), next);
  208. test.done();
  209. },
  210. "With the year set should not loop indefinetely": function(test) {
  211. var rule = new schedule.RecurrenceRule();
  212. rule.second = 0;
  213. rule.minute = 0;
  214. rule.hour = 0;
  215. rule.date = 1;
  216. rule.month = 5;
  217. rule.year = 2010;
  218. var next;
  219. var base1 = new Date(2010, 4, 31, 12, 30, 15, 0);
  220. next = rule.nextInvocationDate(base1);
  221. test.deepEqual(new Date(2010, 5, 1, 0, 0, 0, 0), next);
  222. next = rule.nextInvocationDate(next);
  223. test.equal(next, null);
  224. test.done();
  225. }
  226. }
  227. };