test.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. var chai = require('chai'),
  2. expect = chai.expect,
  3. cb = require('../cron-builder.js');
  4. describe('cron-builder', function () {
  5. var cron;
  6. it('defaults to "* * * * *" when initialized without arguments', function () {
  7. cron = new cb();
  8. expect(cron.get('minute')).to.equal('*');
  9. expect(cron.get('hour')).to.equal('*');
  10. expect(cron.get('dayOfTheMonth')).to.equal('*');
  11. expect(cron.get('month')).to.equal('*');
  12. expect(cron.get('dayOfTheWeek')).to.equal('*');
  13. });
  14. it('protects against the user accessing the expression directly', function () {
  15. cron = new cb();
  16. expect(cron.minute).to.not.eql(['*']);
  17. expect(cron.hour).to.be.undefined;
  18. });
  19. it('protects against the user manipulating the expression directly', function () {
  20. cron = new cb();
  21. cron.minute = ['5'];
  22. expect(cron.get('minute')).to.not.equal('5');
  23. expect(cron.get('minute')).to.equal('*');
  24. });
  25. it('returns a working cron expression when calling .build()', function () {
  26. expect(cron.build()).to.equal('* * * * *');
  27. });
  28. it('sets a single value', function () {
  29. cron = new cb();
  30. expect(cron.set('hour', ['5'])).to.equal('5');
  31. expect(cron.build()).to.equal('* 5 * * *');
  32. });
  33. it('sets multiple values at once', function () {
  34. cron = new cb();
  35. expect(cron.set('minute', ['0', '10', '20', '30', '40', '50'])).to.equal('0,10,20,30,40,50');
  36. expect(cron.build()).to.equal('0,10,20,30,40,50 * * * *');
  37. });
  38. it('sets a range', function () {
  39. cron = new cb();
  40. expect(cron.set('dayOfTheWeek', ['5-7'])).to.equal('5-7');
  41. expect(cron.build()).to.equal('* * * * 5-7');
  42. });
  43. it('multiple sets build the cron string accurately', function () {
  44. cron = new cb();
  45. cron.set('minute', ['10', '30', '50']);
  46. cron.set('hour', ['6', '18']);
  47. cron.set('dayOfTheMonth', ['1', '15']);
  48. cron.set('dayOfTheWeek', ['1-5']);
  49. expect(cron.build()).to.equal('10,30,50 6,18 1,15 * 1-5');
  50. });
  51. it('validates against setting an incorrect measureOfTime', function () {
  52. cron = new cb();
  53. expect(function () { cron.set(['5'], 'minutes') }).to.throw(Error);
  54. });
  55. it('validates against setting a value that is not an Array', function () {
  56. cron = new cb();
  57. expect(function () { cron.set('10', 'hour') }).to.throw(Error);
  58. });
  59. it('validates against setting a value that is not a number or range of numbers', function () {
  60. cron = new cb();
  61. expect(function () { cron.set(['!'], 'hour') }).to.throw(Error);
  62. });
  63. describe('validates against setting values outside the valid range', function () {
  64. it('validates against values too low', function () {
  65. cron = new cb();
  66. expect(function () { cron.set(['0'], 'dayOfTheWeek') }).to.throw(Error);
  67. });
  68. it('validates against values too high', function () {
  69. cron = new cb();
  70. expect(function () { cron.set(['100'], 'hour') }).to.throw(Error);
  71. });
  72. it('validates against setting a range that is out of bounds', function () {
  73. cron = new cb();
  74. expect(function () { cron.set(['20-60'], 'minute') }).to.throw(Error);
  75. expect(function () { cron.set(['12', '22-26', '15'], 'hour') }).to.throw(Error);
  76. });
  77. });
  78. it('gets a single value', function () {
  79. cron = new cb();
  80. cron.set('minute', ['30']);
  81. expect(cron.get('minute')).to.equal('30');
  82. });
  83. it('validates against getting with an invalid measureOfTime', function () {
  84. cron = new cb();
  85. expect(function () { cron.get('hours'); }).to.throw(Error);
  86. });
  87. it('returns the entire expression object when getAll is called', function () {
  88. cron = new cb();
  89. var getAllResponse = cron.getAll();
  90. expect(getAllResponse).to.be.an('object');
  91. expect(getAllResponse).to.have.property('minute').that.is.an('array').with.deep.property('[0]').that.deep.equals('*');
  92. expect(getAllResponse).to.have.property('hour').that.is.an('array').with.deep.property('[0]').that.deep.equals('*');
  93. expect(getAllResponse).to.have.property('dayOfTheMonth').that.is.an('array').with.deep.property('[0]').that.deep.equals('*');
  94. expect(getAllResponse).to.have.property('month').that.is.an('array').with.deep.property('[0]').that.deep.equals('*');
  95. expect(getAllResponse).to.have.property('dayOfTheWeek').that.is.an('array').with.deep.property('[0]').that.deep.equals('*');
  96. });
  97. it('sets the entire object when setAll is called', function () {
  98. cron = new cb();
  99. var getAllResponse = cron.getAll();
  100. getAllResponse.hour = ['13'];
  101. getAllResponse.month = ['1-6'];
  102. getAllResponse.dayOfTheWeek = ['1,3,5,7'];
  103. cron.setAll(getAllResponse);
  104. expect(cron.build()).to.equal('* 13 * 1-6 1,3,5,7');
  105. });
  106. it('validates setting all with too many keys in the expression object', function () {
  107. cron = new cb();
  108. var getAllResponse = cron.getAll();
  109. getAllResponse.tooManyMeasuresOfTime = ['13'];
  110. expect(function () { cron.setAll(getAllResponse) }).to.throw(Error);
  111. });
  112. it('validates setting with an incorrect value with setAll', function () {
  113. cron = new cb();
  114. var getAllResponse = cron.getAll();
  115. getAllResponse.hour = ['28'];
  116. expect(function () { cron.setAll(getAllResponse) }).to.throw(Error);
  117. });
  118. it('adds a value to a measureOfTime that is set to "*"', function () {
  119. cron = new cb();
  120. cron.addValue('minute', '5');
  121. expect(cron.get('minute')).to.equal('5');
  122. expect(cron.build()).to.equal('5 * * * *');
  123. });
  124. it('adds a value to a measure of time that has been set to a number', function () {
  125. cron = new cb();
  126. cron.addValue('hour', '5');
  127. cron.addValue('hour', '10');
  128. expect(cron.get('hour')).to.equal('5,10');
  129. });
  130. it('validates duplicate values', function () {
  131. cron = new cb();
  132. cron.addValue('dayOfTheMonth', '5');
  133. cron.addValue('dayOfTheMonth', '15');
  134. cron.addValue('dayOfTheMonth', '5');
  135. expect(cron.get('dayOfTheMonth')).to.equal('5,15');
  136. });
  137. it('validates an invalid value when adding', function () {
  138. cron = new cb();
  139. expect(function () { cron.addValue('62', 'minute') }).to.throw(Error);
  140. });
  141. it('removes a value that exists with other values', function () {
  142. cron = new cb();
  143. cron.set('dayOfTheWeek', ['2', '4']);
  144. cron.removeValue('dayOfTheWeek', '4');
  145. expect(cron.get('dayOfTheWeek')).to.equal('2');
  146. });
  147. it('resets the value to the default "*" when removing the only value', function () {
  148. cron = new cb();
  149. cron.set('minute', ['7']);
  150. expect(cron.get('minute')).to.equal('7');
  151. cron.removeValue('minute', '7');
  152. expect(cron.get('minute')).to.equal('*');
  153. });
  154. it('validates an invalid measure of time when removing a value', function () {
  155. cron = new cb();
  156. expect(function () { cron.removeValue('ninute') }).to.throw(Error);
  157. });
  158. it('accepts a cron expression when instantiating', function () {
  159. cron = new cb('30 0-6 * * 1-5');
  160. expect(cron.build()).to.equal('30 0-6 * * 1-5');
  161. });
  162. it('validates bad values when instantiating with an explicit expression', function () {
  163. expect(function () { cron = new cb('30 0-6 * * 1-10') }).to.throw(Error);
  164. });
  165. it('validates an expression that is too long when instantiating with an explicit expression', function () {
  166. expect(function () { cron = new cb('30 0-6 * * 1-5 * *') }).to.throw(Error);
  167. });
  168. });