start-end-test.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. 'use strict';
  2. var sinon = require('sinon');
  3. var main = require('../package.json').main;
  4. var schedule = require('../' + main);
  5. var clock;
  6. module.exports = {
  7. setUp: function (cb) {
  8. clock = sinon.useFakeTimers();
  9. cb();
  10. },
  11. 'RecurrenceRule': {
  12. 'no endTime , startTime less than now': function (test) {
  13. test.expect(3);
  14. var job = new schedule.Job(function () {
  15. test.ok(true);
  16. });
  17. var rule = new schedule.RecurrenceRule();
  18. rule.second = null; // every second
  19. job.schedule({
  20. start: new Date(Date.now() - 2000),
  21. rule: rule
  22. });
  23. setTimeout(function () {
  24. test.done();
  25. }, 3250);
  26. clock.tick(3250);
  27. },
  28. 'no endTime , startTime greater than now': function (test) {
  29. test.expect(1);
  30. var job = new schedule.Job(function () {
  31. test.ok(true);
  32. });
  33. var rule = new schedule.RecurrenceRule();
  34. rule.second = null; // every second
  35. job.schedule({
  36. start: new Date(Date.now() + 2000),
  37. rule: rule
  38. });
  39. setTimeout(function () {
  40. test.done();
  41. }, 3250);
  42. clock.tick(3250);
  43. },
  44. 'no startTime , endTime less than now': function (test) {
  45. test.expect(0);
  46. var job = new schedule.Job(function () {
  47. test.ok(true);
  48. });
  49. var rule = new schedule.RecurrenceRule();
  50. rule.second = null; // every second
  51. job.schedule({
  52. end: new Date(Date.now() - 2000),
  53. rule: rule
  54. });
  55. setTimeout(function () {
  56. test.done();
  57. }, 3250);
  58. clock.tick(3250);
  59. },
  60. 'no startTime , endTime greater than now': function (test) {
  61. test.expect(2);
  62. var job = new schedule.Job(function () {
  63. test.ok(true);
  64. });
  65. var rule = new schedule.RecurrenceRule();
  66. rule.second = null; // every second
  67. job.schedule({
  68. end: new Date(Date.now() + 2000),
  69. rule: rule
  70. });
  71. setTimeout(function () {
  72. test.done();
  73. }, 3250);
  74. clock.tick(3250);
  75. },
  76. 'has startTime and endTime': function (test) {
  77. test.expect(1);
  78. var job = new schedule.Job(function () {
  79. test.ok(true);
  80. });
  81. var rule = new schedule.RecurrenceRule();
  82. rule.second = null; // every second
  83. job.schedule({
  84. start: new Date(Date.now() + 1000),
  85. end: new Date(Date.now() + 2000),
  86. rule: rule
  87. });
  88. setTimeout(function () {
  89. test.done();
  90. }, 3250);
  91. clock.tick(3250);
  92. }
  93. },
  94. 'Object Literal': {
  95. 'no endTime , startTime less than now': function (test) {
  96. test.expect(3);
  97. var job = new schedule.Job(function () {
  98. test.ok(true);
  99. });
  100. job.schedule({
  101. start: new Date(Date.now() - 2000),
  102. rule: { second: null }
  103. });
  104. setTimeout(function () {
  105. test.done();
  106. }, 3250);
  107. clock.tick(3250);
  108. },
  109. 'no endTime , startTime greater than now': function (test) {
  110. test.expect(1);
  111. var job = new schedule.Job(function () {
  112. test.ok(true);
  113. });
  114. job.schedule({
  115. start: new Date(Date.now() + 2000),
  116. rule: { second: null }
  117. });
  118. setTimeout(function () {
  119. test.done();
  120. }, 3250);
  121. clock.tick(3250);
  122. },
  123. 'no startTime , endTime less than now': function (test) {
  124. test.expect(0);
  125. var job = new schedule.Job(function () {
  126. test.ok(true);
  127. });
  128. job.schedule({
  129. end: new Date(Date.now() - 2000),
  130. rule: { second: null }
  131. });
  132. setTimeout(function () {
  133. test.done();
  134. }, 3250);
  135. clock.tick(3250);
  136. },
  137. 'no startTime , endTime greater than now': function (test) {
  138. test.expect(2);
  139. var job = new schedule.Job(function () {
  140. test.ok(true);
  141. });
  142. job.schedule({
  143. end: new Date(Date.now() + 2000),
  144. rule: { second: null }
  145. });
  146. setTimeout(function () {
  147. test.done();
  148. }, 3250);
  149. clock.tick(3250);
  150. },
  151. 'has startTime and endTime': function (test) {
  152. test.expect(1);
  153. var job = new schedule.Job(function () {
  154. test.ok(true);
  155. });
  156. job.schedule({
  157. start: new Date(Date.now() + 1000),
  158. end: new Date(Date.now() + 2000),
  159. rule: { second: null }
  160. });
  161. setTimeout(function () {
  162. test.done();
  163. }, 3250);
  164. clock.tick(3250);
  165. }
  166. },
  167. 'cron-style': {
  168. 'no endTime , startTime less than now': function (test) {
  169. test.expect(3);
  170. var job = new schedule.Job(function () {
  171. test.ok(true);
  172. });
  173. job.schedule({
  174. start: new Date(Date.now() - 2000),
  175. rule: '*/1 * * * * *'
  176. });
  177. setTimeout(function () {
  178. test.done();
  179. }, 3250);
  180. clock.tick(3250);
  181. },
  182. 'no endTime , startTime greater than now': function (test) {
  183. test.expect(1);
  184. var job = new schedule.Job(function () {
  185. test.ok(true);
  186. });
  187. job.schedule({
  188. start: new Date(Date.now() + 2000),
  189. rule: '*/1 * * * * *'
  190. });
  191. setTimeout(function () {
  192. test.done();
  193. }, 3250);
  194. clock.tick(3250);
  195. },
  196. 'no startTime , endTime less than now': function (test) {
  197. test.expect(0);
  198. var job = new schedule.Job(function () {
  199. test.ok(true);
  200. });
  201. job.schedule({
  202. end: new Date(Date.now() - 2000),
  203. rule: '*/1 * * * * *'
  204. });
  205. setTimeout(function () {
  206. test.done();
  207. }, 3250);
  208. clock.tick(3250);
  209. },
  210. 'no startTime , endTime greater than now': function (test) {
  211. test.expect(2);
  212. var job = new schedule.Job(function () {
  213. test.ok(true);
  214. });
  215. job.schedule({
  216. end: new Date(Date.now() + 2000),
  217. rule: '*/1 * * * * *'
  218. });
  219. setTimeout(function () {
  220. test.done();
  221. }, 3250);
  222. clock.tick(3250);
  223. },
  224. 'has startTime and endTime': function (test) {
  225. test.expect(1);
  226. var job = new schedule.Job(function () {
  227. test.ok(true);
  228. });
  229. job.schedule({
  230. start: new Date(Date.now() + 1000),
  231. end: new Date(Date.now() + 2000),
  232. rule: '*/1 * * * * *'
  233. });
  234. setTimeout(function () {
  235. test.done();
  236. }, 3250);
  237. clock.tick(3250);
  238. }
  239. },
  240. tearDown: function (cb) {
  241. clock.restore();
  242. cb();
  243. }
  244. };