convenience-method-test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. ".scheduleJob": {
  12. "Returns Job instance": function(test) {
  13. var job = schedule.scheduleJob(new Date(Date.now() + 1000), function() {});
  14. test.ok(job instanceof schedule.Job);
  15. job.cancel();
  16. test.done();
  17. }
  18. },
  19. ".scheduleJob(Date, fn)": {
  20. "Runs job once at some date": function(test) {
  21. test.expect(1);
  22. schedule.scheduleJob(new Date(Date.now() + 3000), function() {
  23. test.ok(true);
  24. });
  25. setTimeout(function() {
  26. test.done();
  27. }, 3250);
  28. clock.tick(3250);
  29. },
  30. "Job doesn't emit initial 'scheduled' event": function(test) {
  31. var job = schedule.scheduleJob(new Date(Date.now() + 1000), function() {});
  32. job.on('scheduled', function() {
  33. test.ok(false);
  34. });
  35. setTimeout(function() {
  36. test.done();
  37. }, 1250);
  38. clock.tick(1250);
  39. },
  40. "Won't run job if scheduled in the past": function(test) {
  41. test.expect(1);
  42. var job = schedule.scheduleJob(new Date(Date.now() - 3000), function() {
  43. test.ok(false);
  44. });
  45. test.equal(job, null);
  46. setTimeout(function() {
  47. test.done();
  48. }, 1000);
  49. clock.tick(1000);
  50. }
  51. },
  52. ".scheduleJob(RecurrenceRule, fn)": {
  53. "Runs job at interval based on recur rule, repeating indefinitely": function(test) {
  54. test.expect(3);
  55. var rule = new schedule.RecurrenceRule();
  56. rule.second = null; // fire every second
  57. var job = schedule.scheduleJob(rule, function() {
  58. test.ok(true);
  59. });
  60. setTimeout(function() {
  61. job.cancel();
  62. test.done();
  63. }, 3250);
  64. clock.tick(3250);
  65. },
  66. "Job doesn't emit initial 'scheduled' event": function(test) {
  67. /*
  68. * If this was Job#schedule it'd fire 4 times.
  69. */
  70. test.expect(3);
  71. var rule = new schedule.RecurrenceRule();
  72. rule.second = null; // fire every second
  73. var job = new schedule.scheduleJob(rule, function() {});
  74. job.on('scheduled', function(runOnDate) {
  75. test.ok(true);
  76. });
  77. setTimeout(function() {
  78. job.cancel();
  79. test.done();
  80. }, 3250);
  81. clock.tick(3250);
  82. },
  83. "Doesn't invoke job if recur rule schedules it in the past": function(test) {
  84. test.expect(1);
  85. var rule = new schedule.RecurrenceRule();
  86. rule.year = 1960;
  87. var job = schedule.scheduleJob(rule, function() {
  88. test.ok(false);
  89. });
  90. test.equal(job, null);
  91. setTimeout(function() {
  92. test.done();
  93. }, 1000);
  94. clock.tick(1000);
  95. }
  96. },
  97. ".scheduleJob({...}, fn)": {
  98. "Runs job at interval based on object, repeating indefinitely": function(test) {
  99. test.expect(3);
  100. var job = new schedule.scheduleJob({
  101. second: null // Fire every second
  102. }, function() {
  103. test.ok(true);
  104. });
  105. setTimeout(function() {
  106. job.cancel();
  107. test.done();
  108. }, 3250);
  109. clock.tick(3250);
  110. },
  111. "Job doesn't emit initial 'scheduled' event": function(test) {
  112. /*
  113. * With Job#schedule this would be 3:
  114. * scheduled at time 0
  115. * scheduled at time 1000
  116. * scheduled at time 2000
  117. */
  118. test.expect(2);
  119. var job = schedule.scheduleJob({
  120. second: null // fire every second
  121. }, function() {});
  122. job.on('scheduled', function() {
  123. test.ok(true);
  124. });
  125. setTimeout(function() {
  126. job.cancel();
  127. test.done();
  128. }, 2250);
  129. clock.tick(2250);
  130. },
  131. "Doesn't invoke job if object schedules it in the past": function(test) {
  132. test.expect(1);
  133. var job = schedule.scheduleJob({
  134. year: 1960
  135. }, function() {
  136. test.ok(false);
  137. });
  138. test.equal(job, null);
  139. setTimeout(function() {
  140. test.done();
  141. }, 1000);
  142. clock.tick(1000);
  143. }
  144. },
  145. ".scheduleJob({...}, {...}, fn)": {
  146. "Callback called for each job if callback is provided": function(test) {
  147. test.expect(3);
  148. var job = new schedule.scheduleJob({
  149. second: null // Fire every second
  150. }, function() {}, function() {
  151. test.ok(true);
  152. });
  153. setTimeout(function() {
  154. job.cancel();
  155. test.done();
  156. }, 3250);
  157. clock.tick(3250);
  158. }
  159. },
  160. ".rescheduleJob(job, {...})": {
  161. "Reschedule jobs from object based to object based": function(test) {
  162. test.expect(3);
  163. var job = new schedule.scheduleJob({
  164. second: null
  165. }, function() {
  166. test.ok(true);
  167. });
  168. setTimeout(function() {
  169. schedule.rescheduleJob(job, {
  170. minute: null
  171. });
  172. }, 3250);
  173. setTimeout(function() {
  174. job.cancel();
  175. test.done();
  176. }, 5000);
  177. clock.tick(5000);
  178. },
  179. "Reschedule jobs from every minutes to every second": function(test) {
  180. test.expect(3);
  181. var timeout = 60 * 1000;
  182. var job = new schedule.scheduleJob({
  183. minute: null
  184. }, function() {
  185. test.ok(true);
  186. });
  187. setTimeout(function() {
  188. schedule.rescheduleJob(job, {
  189. second: null
  190. });
  191. }, timeout);
  192. setTimeout(function() {
  193. job.cancel();
  194. test.done();
  195. }, timeout + 2250);
  196. clock.tick(timeout + 2250);
  197. }
  198. },
  199. ".rescheduleJob(job, Date)": {
  200. "Reschedule jobs from Date to Date": function(test) {
  201. test.expect(1);
  202. var job = new schedule.scheduleJob(new Date(Date.now() + 3000), function() {
  203. test.ok(true);
  204. });
  205. setTimeout(function() {
  206. schedule.rescheduleJob(job, new Date(Date.now() + 5000));
  207. }, 1000);
  208. setTimeout(function() {
  209. test.done();
  210. }, 6150);
  211. clock.tick(6150);
  212. },
  213. "Reschedule jobs that has been executed": function(test) {
  214. test.expect(2);
  215. var job = new schedule.scheduleJob(new Date(Date.now() + 1000), function() {
  216. test.ok(true);
  217. });
  218. setTimeout(function() {
  219. schedule.rescheduleJob(job, new Date(Date.now() + 2000));
  220. }, 2000);
  221. setTimeout(function() {
  222. test.done();
  223. }, 5150);
  224. clock.tick(5150);
  225. }
  226. },
  227. ".rescheduleJob(job, RecurrenceRule)": {
  228. "Reschedule jobs from RecurrenceRule to RecurrenceRule": function(test) {
  229. test.expect(3);
  230. var timeout = 60 * 1000;
  231. var rule = new schedule.RecurrenceRule();
  232. rule.second = null; // fire every second
  233. var job = schedule.scheduleJob(rule, function() {
  234. test.ok(true);
  235. });
  236. var newRule = new schedule.RecurrenceRule();
  237. newRule.minute = null;
  238. setTimeout(function() {
  239. schedule.rescheduleJob(job, newRule);
  240. }, 2250);
  241. setTimeout(function() {
  242. job.cancel();
  243. test.done();
  244. }, timeout + 2250);
  245. clock.tick(timeout + 2250);
  246. },
  247. "Reschedule jobs from RecurrenceRule to Date": function(test) {
  248. test.expect(3);
  249. var rule = new schedule.RecurrenceRule();
  250. rule.second = null; // fire every second
  251. var job = schedule.scheduleJob(rule, function() {
  252. test.ok(true);
  253. });
  254. setTimeout(function() {
  255. schedule.rescheduleJob(job, new Date(Date.now() + 2000));
  256. }, 2150);
  257. setTimeout(function() {
  258. test.done();
  259. }, 4250);
  260. clock.tick(4250);
  261. },
  262. "Reschedule jobs from RecurrenceRule to {...}": function(test) {
  263. test.expect(3);
  264. var timeout = 60 * 1000;
  265. var rule = new schedule.RecurrenceRule();
  266. rule.second = null; // fire every second
  267. var job = schedule.scheduleJob(rule, function() {
  268. test.ok(true);
  269. });
  270. setTimeout(function() {
  271. schedule.rescheduleJob(job, {
  272. minute: null
  273. });
  274. }, 2150);
  275. setTimeout(function() {
  276. job.cancel();
  277. test.done();
  278. }, timeout + 2150);
  279. clock.tick(timeout + 2150);
  280. },
  281. "Reschedule jobs that is not available": function(test) {
  282. test.expect(4);
  283. var rule = new schedule.RecurrenceRule();
  284. rule.second = null; // fire every second
  285. var job = schedule.scheduleJob(rule, function() {
  286. test.ok(true);
  287. });
  288. setTimeout(function() {
  289. schedule.rescheduleJob(null, new Date(Date.now() + 2000));
  290. }, 2150);
  291. setTimeout(function() {
  292. job.cancel();
  293. test.done();
  294. }, 4250);
  295. clock.tick(4250);
  296. }
  297. },
  298. '.rescheduleJob("job name", {...})': {
  299. "Reschedule jobs from object based to object based": function(test) {
  300. test.expect(3);
  301. var job = new schedule.scheduleJob({
  302. second: null
  303. }, function() {
  304. test.ok(true);
  305. });
  306. setTimeout(function() {
  307. schedule.rescheduleJob(job.name, {
  308. minute: null
  309. });
  310. }, 3250);
  311. setTimeout(function() {
  312. job.cancel();
  313. test.done();
  314. }, 5000);
  315. clock.tick(5000);
  316. },
  317. "Reschedule jobs from every minutes to every second": function(test) {
  318. test.expect(3);
  319. var timeout = 60 * 1000;
  320. var job = new schedule.scheduleJob({
  321. minute: null
  322. }, function() {
  323. test.ok(true);
  324. });
  325. setTimeout(function() {
  326. schedule.rescheduleJob(job.name, {
  327. second: null
  328. });
  329. }, timeout);
  330. setTimeout(function() {
  331. job.cancel();
  332. test.done();
  333. }, timeout + 2250);
  334. clock.tick(timeout + 2250);
  335. }
  336. },
  337. '.rescheduleJob("job name", Date)': {
  338. "Reschedule jobs from Date to Date": function(test) {
  339. test.expect(1);
  340. var job = new schedule.scheduleJob(new Date(Date.now() + 3000), function() {
  341. test.ok(true);
  342. });
  343. setTimeout(function() {
  344. schedule.rescheduleJob(job.name, new Date(Date.now() + 5000));
  345. }, 1000);
  346. setTimeout(function() {
  347. test.done();
  348. }, 6150);
  349. clock.tick(6150);
  350. },
  351. "Reschedule jobs that has been executed": function(test) {
  352. test.expect(2);
  353. var job = new schedule.scheduleJob(new Date(Date.now() + 1000), function() {
  354. test.ok(true);
  355. });
  356. setTimeout(function() {
  357. schedule.rescheduleJob(job.name, new Date(Date.now() + 2000));
  358. }, 2000);
  359. setTimeout(function() {
  360. test.done();
  361. }, 5150);
  362. clock.tick(5150);
  363. }
  364. },
  365. '.rescheduleJob("job name", RecurrenceRule)': {
  366. "Reschedule jobs from RecurrenceRule to RecurrenceRule": function(test) {
  367. test.expect(3);
  368. var timeout = 60 * 1000;
  369. var rule = new schedule.RecurrenceRule();
  370. rule.second = null; // fire every second
  371. var job = schedule.scheduleJob(rule, function() {
  372. test.ok(true);
  373. });
  374. var newRule = new schedule.RecurrenceRule();
  375. newRule.minute = null;
  376. setTimeout(function() {
  377. schedule.rescheduleJob(job.name, newRule);
  378. }, 2250);
  379. setTimeout(function() {
  380. job.cancel();
  381. test.done();
  382. }, timeout + 2250);
  383. clock.tick(timeout + 2250);
  384. },
  385. "Reschedule jobs from RecurrenceRule to Date": function(test) {
  386. test.expect(3);
  387. var rule = new schedule.RecurrenceRule();
  388. rule.second = null; // fire every second
  389. var job = schedule.scheduleJob(rule, function() {
  390. test.ok(true);
  391. });
  392. setTimeout(function() {
  393. schedule.rescheduleJob(job.name, new Date(Date.now() + 2000));
  394. }, 2150);
  395. setTimeout(function() {
  396. test.done();
  397. }, 4250);
  398. clock.tick(4250);
  399. },
  400. "Reschedule jobs from RecurrenceRule to {...}": function(test) {
  401. test.expect(3);
  402. var timeout = 60 * 1000;
  403. var rule = new schedule.RecurrenceRule();
  404. rule.second = null; // fire every second
  405. var job = schedule.scheduleJob(rule, function() {
  406. test.ok(true);
  407. });
  408. setTimeout(function() {
  409. schedule.rescheduleJob(job.name, {
  410. minute: null
  411. });
  412. }, 2150);
  413. setTimeout(function() {
  414. job.cancel();
  415. test.done();
  416. }, timeout + 2150);
  417. clock.tick(timeout + 2150);
  418. },
  419. "Reschedule jobs that is not available": function(test) {
  420. test.expect(4);
  421. var rule = new schedule.RecurrenceRule();
  422. rule.second = null; // fire every second
  423. var job = schedule.scheduleJob(rule, function() {
  424. test.ok(true);
  425. });
  426. setTimeout(function() {
  427. schedule.rescheduleJob("Blah", new Date(Date.now() + 2000));
  428. }, 2150);
  429. setTimeout(function() {
  430. job.cancel();
  431. test.done();
  432. }, 4250);
  433. clock.tick(4250);
  434. }
  435. },
  436. ".cancelJob(Job)": {
  437. "Prevents all future invocations of Job passed in": function(test) {
  438. test.expect(2);
  439. var job = schedule.scheduleJob({
  440. second: null
  441. }, function() {
  442. test.ok(true);
  443. });
  444. setTimeout(function() {
  445. schedule.cancelJob(job);
  446. }, 2250);
  447. setTimeout(function() {
  448. test.done();
  449. }, 3250);
  450. clock.tick(3250);
  451. },
  452. "Can cancel Jobs scheduled with Job#schedule": function(test) {
  453. test.expect(2);
  454. var job = new schedule.Job(function() {
  455. test.ok(true);
  456. });
  457. job.schedule({
  458. second: null
  459. });
  460. setTimeout(function() {
  461. schedule.cancelJob(job);
  462. }, 2250);
  463. setTimeout(function() {
  464. test.done();
  465. }, 3250);
  466. clock.tick(3250);
  467. },
  468. "Job emits 'canceled' event": function(test) {
  469. test.expect(1);
  470. var job = schedule.scheduleJob({
  471. second: null
  472. }, function() {});
  473. job.on('canceled', function() {
  474. test.ok(true);
  475. });
  476. setTimeout(function() {
  477. schedule.cancelJob(job);
  478. test.done();
  479. }, 1250);
  480. clock.tick(1250);
  481. }
  482. },
  483. '.cancelJob("job name")': {
  484. "Prevents all future invocations of Job identified by name": function(test) {
  485. test.expect(2);
  486. var job = schedule.scheduleJob({
  487. second: null
  488. }, function() {
  489. test.ok(true);
  490. });
  491. setTimeout(function() {
  492. schedule.cancelJob(job.name);
  493. }, 2250);
  494. setTimeout(function() {
  495. test.done();
  496. }, 3250);
  497. clock.tick(3250);
  498. },
  499. /*
  500. "Can cancel Jobs scheduled with Job#schedule": function(test) {
  501. test.expect(2);
  502. var job = new schedule.Job(function() {
  503. test.ok(true);
  504. });
  505. job.schedule({
  506. second: null
  507. });
  508. setTimeout(function() {
  509. schedule.cancelJob(job.name);
  510. }, 2250);
  511. setTimeout(function() {
  512. test.done();
  513. }, 3250);
  514. },*/
  515. "Job emits 'canceled' event": function(test) {
  516. test.expect(1);
  517. var job = schedule.scheduleJob({
  518. second: null
  519. }, function() {});
  520. job.on('canceled', function() {
  521. test.ok(true);
  522. });
  523. setTimeout(function() {
  524. schedule.cancelJob(job.name);
  525. test.done();
  526. }, 1250);
  527. clock.tick(1250);
  528. },
  529. "Does nothing if no job found by that name": function(test) {
  530. test.expect(3);
  531. var job = schedule.scheduleJob({
  532. second: null
  533. }, function() {
  534. test.ok(true);
  535. });
  536. setTimeout(function() {
  537. // This cancel should not affect anything
  538. schedule.cancelJob('blah');
  539. }, 2250);
  540. setTimeout(function() {
  541. job.cancel(); // prevent tests from hanging
  542. test.done();
  543. }, 3250);
  544. clock.tick(3250);
  545. }
  546. },
  547. '.pendingInvocations()': {
  548. "Retrieves pendingInvocations of the job": function(test) {
  549. var job = schedule.scheduleJob(new Date(Date.now() + 1000), function() {});
  550. test.ok(job instanceof schedule.Job);
  551. test.ok(job.pendingInvocations()[0].job);
  552. job.cancel();
  553. test.done();
  554. }
  555. },
  556. tearDown: function(cb) {
  557. clock.restore();
  558. cb();
  559. }
  560. };